zoukankan      html  css  js  c++  java
  • 简单工厂

      工厂顾名思义就是生产商品的,造纸厂生产纸,鞋厂生产鞋,在面向对象的编程思想中,那么我们这里设计模式中的简单工厂就是为我们生产对象的。

      在介绍简单工厂模式之前,我们先来了解一下怎么生产一个商品,以生产笔记本为例,我们怎么让一个笔记本生产出来呢,最简单的方式,就是我们通过实例化的方式,就能产生一个笔记本对象,生产出来的笔记本我们想让它介绍一下自己,那么我们就调用它的Introduce方法。

      首先我们定义一个Leveno类,用于描述联想笔记本,代码如下:

    namespace SimpleComputer.Computer
    {
        public class Leveno
        {
            public override void Introduce()
            {
                Console.WriteLine("我是联想电脑");
            }
        }
    }
    Leveno

      在Main方法中实例化并调用它的Introduce方法,让它介绍一下自己,代码如下:

    class Program
        {
            static void Main(string[] args)
            {
                Leveno com = new Leveno();
                com.Introduce();
                Console.ReadKey();
            }
        }
    Main

      一个简单的产生笔记本的实例算是结束了,接下来我们来想这样一个问题,假如说我们想生产一个Dell的笔记本电脑,那我们又该怎样实现,加入我们采用客户端输入相应的品牌标识得到相应的对象的话,首先我们需要对Dell、Leveno提取一个父类,用于存储生产的笔记本电脑,此父类中必须包含所有的Introduce虚方法,用于让子类实现,具体父类及子类代码如下:

    namespace SimpleComputer.Computer
    {
        public class ComputerBase
        {
            public virtual void Introduce()
            {
                Console.WriteLine("我是一台电脑");
            }
        }
    }
    ComputerBase
    namespace SimpleComputer.Computer
    {
        public class Leveno:ComputerBase
        {
            public override void Introduce()
            {
                Console.WriteLine("我是联想电脑");
            }
        }
    }
    Leveno
    namespace SimpleComputer.Computer
    {
        public class Dell : ComputerBase
        {
            public override void Introduce()
            {
                Console.WriteLine("我是戴尔电脑");
            }
        }
    }
    Dell
    namespace SimpleComputer.Computer
    {
        public class Sony : ComputerBase
        {
            public override void Introduce()
            {
                Console.WriteLine("我是索尼电脑");
            }
        }
    }
    Sony

      所有的电脑父类,电脑子类都已经实现了,接下来要在Main方法中进行实现了,只需要在Main方法中调用就可以了,具体代码的实现如下:

    class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("请输入电脑的标志");
                string name = Console.ReadLine();
                ComputerBase computer = null;
                switch (name)
                {
                    case "Leveno":
                        computer = new Leveno();
                        break;
                    case "Dell":
                        computer = new Dell();
                        break;
                    case "Sony":
                        computer = new Sony();
                        break;
                    default:
                        computer = new ComputerBase();
                        break;
                }
                computer.Introduce();
                Console.ReadKey();
            }
        }
    Main

      根据封装、单一职责的原则,一个类只能做一件事情,Main方法中生产不同的电脑,显然违背了单一职责原则,代码重构一下,把生产电脑的方法封装在生产电脑类中作为它的方法,这个类叫做电脑工厂类,具体实现如下:

    namespace SimpleComputer
    {
        public class ComputerFactory
        {
            public static ComputerBase CreateComputer(string name)
            {
                ComputerBase computer = null;
                switch (name)
                {
                    case "Leveno":
                        computer = new Leveno();
                        break;
                    case "Dell":
                        computer = new Dell();
                        break;
                    case "Sony":
                        computer = new Sony();
                        break;
                    default:
                        computer = new ComputerBase();
                        break;
                }
                return computer;
            }
        }
    }
    ComputerFactory

      那我们现在的Main方法中就变得更加简单了,具体代码如下:

    namespace SimpleComputer
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("请输入电脑的标志");
                string name = Console.ReadLine();
                ComputerBase computer = ComputerFactory.CreateComputer(name);
                computer.Introduce();
                Console.ReadKey();
            }
        }
    }
    Main

      好了,一个简单工厂的实例就算结束了,那么让我们看一下简单工厂的UML图:

      

      以上就是简单工厂的使用以及UML图,其实简单工厂就是生产一件商品之前进行的一次判断,客户想要什么产品,就生产什么产品,同时也让整个系统的耦合度降低,假如业务需求更改,现在再增加一个笔记本品牌,只需实现ComputerBase类,在工厂类中再添加一个case判断就搞定了,不需要再像之前一样更改Main方法里面的内容,这样的设计是不是更加便于扩展。

      本篇文章简单工厂设计模式至此,谢谢您收看我的博客。

      

  • 相关阅读:
    yarn-cli 缓存
    html DOM 的继承关系
    JavaScript basics: 2 ways to get child elements with JavaScript
    svn merge 的区别
    virtualbox 设置windows 于ubuntu虚拟机共享文件夹
    angular 学习理解笔记
    e.which
    痛定思痛
    【转】反思 成长
    日语学习词汇量
  • 原文地址:https://www.cnblogs.com/mointor/p/5170487.html
Copyright © 2011-2022 走看看