亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

.NET工廠方法模式講解

 更新時(shí)間:2016年11月29日 11:47:45   作者:Yangyi.He  
這篇文章主要為大家詳細(xì)介紹了ASP.NET工廠方法模式的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

工廠方法模式介紹:

工廠方法(Factory Method)模式的意義是定義一個(gè)創(chuàng)建產(chǎn)品對(duì)象的工廠接口,將實(shí)際創(chuàng)建工作推遲到子類當(dāng)中。核心工廠類不再負(fù)責(zé)產(chǎn)品的創(chuàng)建,這樣核心類成為一個(gè)抽象工廠角色,僅負(fù)責(zé)具體工廠子類必須實(shí)現(xiàn)的接口,這樣進(jìn)一步抽象化的好處是使得工廠方法模式可以使系統(tǒng)在不修改具體工廠角色的情況下引進(jìn)新的產(chǎn)品。

工廠方法模式結(jié)構(gòu)圖:

角色分類:

抽象工廠角色:是工廠方法模式的核心,與應(yīng)用程序無關(guān)。任何在模式中創(chuàng)建的對(duì)象的工廠類必須實(shí)現(xiàn)這個(gè)接口。
具體工廠角色:這是實(shí)現(xiàn)抽象工廠接口的具體工廠類,包含與應(yīng)用程序密切相關(guān)的邏輯,并且受到應(yīng)用程序調(diào)用以創(chuàng)建產(chǎn)品對(duì)象
抽象產(chǎn)品角色:工廠方法模式所創(chuàng)建的對(duì)象的超類型,也就是產(chǎn)品對(duì)象的共同父類或共同擁有的接口。在上圖中,這個(gè)角色是Light。
具體產(chǎn)品角色:這個(gè)角色實(shí)現(xiàn)了抽象產(chǎn)品角色所定義的接口。某具體產(chǎn)品有專門的具體工廠創(chuàng)建,它們之間往往一一對(duì)應(yīng)。

引入實(shí)際例子:

在上一篇博文簡(jiǎn)單工廠模式中,使用簡(jiǎn)單工廠模式進(jìn)行了以下實(shí)現(xiàn):如果有一個(gè)住戶管理系統(tǒng),里面的住戶類型是可變的,每一種租戶類型的租金計(jì)算公式都存在差異例如:A類型的住戶租金額=天數(shù)*單價(jià)+績(jī)效*0.005;B類型的住戶租金額=月份*(每月價(jià)格+performance*0.001)這里我們雖然實(shí)現(xiàn)了客戶的需求,但是如果客戶后期需要增加了C類型商店和D類型商店,而它們的算法要求又不一樣,這個(gè)時(shí)候我們就需要進(jìn)行C,D類型商店的創(chuàng)建,并繼承Ishop接口,實(shí)現(xiàn)里面的方法,同時(shí)還得繼續(xù)修改工廠類在switc中增加case進(jìn)行捕捉創(chuàng)建相應(yīng)的商店對(duì)象,一旦出現(xiàn)這樣的情況,是不利于程序的擴(kuò)展性和項(xiàng)目后期的維護(hù)性的。

1.分析:商店有共同的行為特征,都要進(jìn)行店鋪?zhàn)饨鹩?jì)算行為,我們抽象了Ishop ,里面有待實(shí)現(xiàn)的計(jì)算商店租金方法行為。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FactoryEntiy
{
  public interface Ishop
  {
    double Getrent(int days, double dayprice, double performance);
  }
}

2.我們實(shí)現(xiàn)Isho接口里面的方法,創(chuàng)建A,B類型店鋪。

using FactoryEntiy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProductEnity
{
  /// <summary>
  /// 繼承商店接口,實(shí)現(xiàn)里面的行為方法,即算法
  /// </summary>
  public class Ashop:Ishop
  {
    /// <summary>
    /// /// A類型商店租金額,天數(shù)*單價(jià)+績(jī)效*0.005
    /// </summary>
    /// <param name="days">天數(shù)</param>
    /// <param name="dayprice">每天單價(jià)</param>
    /// <param name="performance">日平均績(jī)效</param>
    /// <returns></returns>
    public double Getrent(int days, double dayprice, double performance)
    {
      Console.WriteLine("A商店的租金算法");
      return days * dayprice + performance * 0.01;
    }
  }
}

using FactoryEntiy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProductEnity
{
  /// <summary>
  /// 繼承商店接口,實(shí)現(xiàn)里面的行為方法,即算法
  /// </summary>
  public class Bshop:Ishop
  {
    /// <summary>
    /// B類型商店的租金=月份*(每月價(jià)格+performance*0.001)
    /// </summary>
    /// <param name="month">月數(shù)</param>
    /// <param name="monthprice">月單價(jià)</param>
    /// <param name="performance">月平均績(jī)效</param>
    /// <returns></returns>
    public double Getrent(int month, double monthprice, double performance)
    {
      Console.WriteLine("B商店的租金算法");
      return month * (monthprice + performance * 0.001);
    }
  }
}

3.現(xiàn)在考慮在什么情況下創(chuàng)建商店的實(shí)體,對(duì)不同的商店進(jìn)行租金計(jì)算,并且方便以后的增加和修改。于是我們創(chuàng)建IFactroy接口,里面有待實(shí)現(xiàn)的創(chuàng)建商店對(duì)象的方法。

using FactoryEntiy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FactoryMethod
{
  /// <summary>
  /// 工廠類,創(chuàng)建商店類型實(shí)體
  /// </summary>
  public interface IFactory
  {
    Ishop CreateShop();
  }
}

4.現(xiàn)在我們就可以繼承自IFactory,實(shí)現(xiàn)里面創(chuàng)建對(duì)應(yīng)的商店對(duì)象了。

using FactoryEntiy;
using FactoryMethod;
using ProductEnity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProductEnity
{
  /// <summary>
  /// 繼承工廠類,創(chuàng)建A類型商店實(shí)體
  /// </summary>
  public class CreateBshop : IFactory
  {
    public Ishop CreateShop()
    {
      return new Ashop();
    }
  }
}

using FactoryEntiy;
using FactoryMethod;
using ProductEnity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProductEnity
{
  /// <summary>
  /// 繼承工廠類,創(chuàng)建B類型商店實(shí)體
  /// </summary>
  public class CreateAshop : IFactory
  {
    public Ishop CreateShop()
    {
      return new Bshop();
    }
  }
}

5.之后根據(jù)當(dāng)前的商店類型進(jìn)行判斷,該類型的商店應(yīng)該進(jìn)行哪一種算法:

using FactoryEntiy;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Reflection;
using System.Text;

namespace FactoryMethod.App
{
  class Program
  {
    static void Main(string[] args)
    {
      string shopname = ConfigurationManager.AppSettings["CreateShopClassName"];
      //shopname為創(chuàng)建商店類名稱,此處=CreateAshop
      IFactory af = (IFactory)Assembly.Load("ProductEnity").CreateInstance("ProductEnity." + shopname);
      //第一個(gè)ProductEnity是dll的名稱,第二個(gè)ProductEnity是項(xiàng)目的命名空間。
      Ishop As = af.CreateShop(); double total = As.Getrent(30, 300, 2000); //30 天/100元 日平均績(jī)效為2000 
      Console.WriteLine("該A類型商店的租金為:" + total); 
      
      Console.WriteLine("=============");
      IFactory bf = (IFactory)Assembly.Load("ProductEnity").CreateInstance("ProductEnity." + "CreateBshop");
      //CreateBshop可以保存為配置或者存在數(shù)據(jù)庫中,
      //注意該保存字符串應(yīng)該與項(xiàng)目中創(chuàng)建的類名一樣,
      //否則反射的方式會(huì)找不到該項(xiàng)目下的類。
      Ishop Bs = bf.CreateShop(); total = Bs.Getrent(30, 300, 2000); //30 天/100元 日平均績(jī)效為2000
      Console.WriteLine("該A類型商店的租金為:" + total);
    }
  }
}

這里我們使用反射的方式創(chuàng)建對(duì)象,替換了之前的工廠類通過switch創(chuàng)建對(duì)象的方式,有利于后面的新類型商店增加和算法修改增加和維護(hù)以后在項(xiàng)目需求在有變革,我們只需要重新在項(xiàng)目中增加C,D類型商店,繼承Ishop實(shí)現(xiàn)里面的方法,同時(shí),添加繼承IFactroy接口,創(chuàng)建對(duì)應(yīng)的商店對(duì)象編譯該項(xiàng)目的ProductEnity.dll,以后再進(jìn)行計(jì)算該C,D類型的商店算法就可以通過反射的方式進(jìn)行計(jì)算,不需要修改原來的工程類代碼。

整個(gè)項(xiàng)目的結(jié)構(gòu)圖如下:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論