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

    简单工厂模式是属于创建型模式,又叫做静态工厂方法(Static Factory Method)模式。
    优点
    工厂类是整个模式的关键.包含了必要的逻辑判断,根据外界给定的信息,决定究竟应该创建哪个具体类的对象.通过使用工厂类,外界可以从直接创建具体产品对象的尴尬局面摆脱出来,仅仅需要负责“消费”对象就可以了。而不必管这些对象究竟如何创建及如何组织的.明确了各自的职责和权利,有利于整个软件体系结构的优化。
    缺点
    由于工厂类集中了所有实例的创建逻辑,违反了高内聚责任分配原则,将全部创建逻辑集中到了一个工厂类中;它所能创建的类只能是事先考虑到的,如果需要添加新的类,则就需要改变工厂类了。
    当系统中的具体产品类不断增多时候,可能会出现要求工厂类根据不同条件创建不同实例的需求.这种对条件的判断和对具体产品类型的判断交错在一起,很难避免模块功能的蔓延,对系统的维护和扩展非常不利;
    这些缺点在工厂方法模式中得到了一定的克服。
     
    代码实现
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace DesignPattern
    {
        class Program
        {
            static void Main(string[] args)
            {
                IAutoCarMake parents = new Factory().CreateAutoCar("red");
    
               // IAutoCarMake parents = new Factory().CreateAutoCar("blue");
    
                parents.CreateAutoCar();
    
                Console.ReadKey();
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace DesignPattern
    {
       public class Factory
        {
            public IAutoCarMake CreateAutoCar(string flag)
            {
                switch (flag)
                {
                    case "red":
                        return new _RedCar();
                    case "blue":
                        return new _BlueCar();
                }
                return null;
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace DesignPattern
    {
        /// <summary>
        /// 简单工厂模式
        /// </summary>
        public interface IAutoCarMake
        {
            /// <summary>
            /// 创建汽车
            /// </summary>
            void CreateAutoCar();
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace DesignPattern
    {
    
        /// <summary>
        /// 红色小轿车
        /// </summary>
        public class _RedCar : IAutoCarMake
        {
            public void CreateAutoCar()
            {
                Console.WriteLine("生成红色小轿车");
            }
        }
        /// <summary>
        /// 蓝色小轿车
        /// </summary>
        public class _BlueCar : IAutoCarMake
        {
            public void CreateAutoCar()
            {
                Console.WriteLine("生成蓝色小轿车");
            }
        }
    }
  • 相关阅读:
    sp2010 升级sp2013 用户无法打开网站
    powerviot install in sharepoint 2013
    can not connect cube in performancce dashboard
    westrac server security configure user info
    添加报表服务在多服务器场
    sharepoint 2013 office web app 2013 文档在线浏览 IE11 浏览器不兼容解决方法
    delete job definition
    目前付款申请单内网打开慢的问题
    item style edit in sharepoint 2013
    Could not load file or assembly '$SharePoint.Project.AssemblyFullName$'
  • 原文地址:https://www.cnblogs.com/macT/p/11803615.html
Copyright © 2011-2022 走看看