工厂方法模式(Factory)

前言;工厂方法模式又叫做工厂模式,它是23个设计模式中的一个,它解决的还是在软件设计中创建对象的问题,它可以更好的解决用户需求的变化.

问题;在简单工厂模式中,我们将实例化的对象全部放于Factory.cs(工厂类),在我们的预知下我们可以实例化对象,但是我们的预知是有限的,而客户的需求是无限的,这样就出现了问题,一但客户的需求过于复杂,我们就要修改源码了,这是设计模式不允许的.

定义:在工厂模式中,父类负责定义创建对象的接口,子类负责new具体对象。

现实中的例子:我们举一个例子,这个例子和简单工厂的例子有些不同。

  据说清朝有个皇帝,它非常奢侈,每个衣服都有一个宫女负责,这样一来,每增加一种衣服,就得多出一个宫女,但她们各付其职,互不影响。   

分析:实现的功能,可以根据皇帝的需求,创建宫女去拿所对应的衣服,如果皇帝太奢侈,这种衣服还没有,只需要添加一个宫女就可以满足他的需求了,每个宫女只要有一种衣服即可(高内聚),要增加衣服,对于原来宫女和衣服来说,谁都不影响谁。

商品系列

 商品接口ICoat.cs

public interface ICoat
{
        void ShowCoat();
}

具体商品:商务上衣

复制代码

 public class BusinessCoat : ICoat
    {
        public void ShowCoat()
        {
            Console.Write("这件是商务上衣");
            //throw new NotImplementedException();
        }
    }

复制代码

具体商品:时尚上衣

1

2

3

4

5

6

7

8

public class BusinessFactory : IFactory

   {

       public ICoat CreateCoat()

       {

           return new BusinessCoat();

           ///throw new NotImplementedException();

       }

   }

 

下面是工厂系列

总厂房:

public interface IFactory
    {
        ICoat CreateCoat();
    }

时尚上衣工厂

复制代码

public class FashionFactory : IFactory
    {
        public ICoat CreateCoat()
        {
            return new FashionCoat();
        }
    }

复制代码

商务上衣工厂

复制代码

public class BusinessFactory : IFactory
    {
        public ICoat CreateCoat()
        {
            return new BusinessCoat();
            ///throw new NotImplementedException();
        }
    }

复制代码

皇帝:

复制代码

static void Main(string[] args)
        {
            BusinessCoat coat = new BusinessCoat();
            coat.ShowCoat();
            Console.ReadLine();
        }

复制代码

架构图:

工厂方法模式使用继承自抽象工厂角色的多个子类来代替简单工厂模式中的“上帝类”。正如上面所说,这样便分担了对象承受的压力;而且这样使得结构变得灵活 起来——当有新的产品产生时,只要按照抽象产品角色、抽象工厂角色提供的合同来生成,那么就可以被客户使用,而不必去修改任何已有的代 码。可以看出工厂角色的结构也是符合开闭原则的!

缺点:添加一个产品就要添加一个工厂类,这使程序的架构非常负责,从而使系统阅读起开非常困难。

http://www.cgpwyj.cn/
http://news.cgpwyj.cn/
http://item.cgpwyj.cn/
http://www.peacemind.com.cn/
http://news.peacemind.com.cn/
http://item.peacemind.com.cn/
http://www.tasknet.com.cn/
http://news.tasknet.com.cn/
http://item.tasknet.com.cn/
http://www.ownbar.cn/
http://news.ownbar.cn/
http://item.ownbar.cn
http://www.shtarchao.net.cn/
http://news.shtarchao.net.cn/
http://item.shtarchao.net.cn/
http://www.metroworld.com.cn/
http://news.metroworld.com.cn/
http://www.cngodo.cn/
http://news.cngodo.cn/
http://item.cngodo.cn/
http://www.gzrdbp.cn/
http://news.gzrdbp.cn/
http://item.gzrdbp.cn/
http://www.dnapt.cn/
http://news.dnapt.cn/
http://item.dnapt.cn/
http://www.ncxlk.cn/
http://news.ncxlk.cn/
http://item.ncxlk.cn/
http://www.zgxxyp.cn/
http://news.zgxxyp.cn/
http://item.zgxxyp.cn/
http://www.sjjdvr.cn/
http://news.sjjdvr.cn/
http://item.sjjdvr.cn/
http://www.sujinkeji.cn/
http://news.sujinkeji.cn/
http://item.sujinkeji.cn/
http://www.zsjxbd.cn/
http://news.zsjxbd.cn/
http://item.zsjxbd.cn/
http://www.yesgas.cn/
http://news.yesgas.cn/
http://item.yesgas.cn/
http://www.quickpass.sh.cn/
http://news.quickpass.sh.cn/
http://item.quickpass.sh.cn/
http://www.jspcrm.cn/
http://news.jspcrm.cn/
http://item.jspcrm.cn/
http://www.yjdwpt.cn/
http://news.yjdwpt.cn/
http://item.yjdwpt.cn/
http://www.henanwulian.cn/
http://news.henanwulian.cn/
http://item.henanwulian.cn/
http://www.hhrshh.cn/
http://news.hhrshh.cn/
http://item.hhrshh.cn/
http://www.gpgold.cn/
http://news.gpgold.cn/
http://item.gpgold.cn/
http://www.jingzhuiyou.cn/
http://news.jingzhuiyou.cn/
http://item.jingzhuiyou.cn/ 

猜你喜欢

转载自blog.csdn.net/qq_38462207/article/details/82252636