zoukankan      html  css  js  c++  java
  • 设计模式系列之-工厂方法

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    using DPC.Domain;
    namespace DPC.CreateModel.FactoryMethod
    {
        class Program
        {
            static void Main(string[] args)
            {
                //创建生产小米手机的工厂
                IMobilePhoneFactory xiaomiFactory = new XiaoMiFactory();
                MobilePhone xiaomi = xiaomiFactory.CreateMobilePhone(); //调用工厂对象生产手机的方法
                xiaomi.Show();
                Console.WriteLine();
    
    
                //创建生产苹果手机的工厂
                IMobilePhoneFactory iphoneFactory = new IPhoneFactory();
                MobilePhone iphone = iphoneFactory.CreateMobilePhone(); //调用工厂对象生产手机的方法
                iphone.Show();
    
    
                //Console.WriteLine();
                //工厂方法与简单工厂结合使用
                //MobliePhoneFactoryHelper.GetPhone("苹果").Show();
                //MobliePhoneFactoryHelper.GetPhone("小米").Show();
                Console.ReadKey();
            }
        }
    
        #region 工厂方法
        //工厂方法模式是对简单工厂模式的改造和升级,在工厂模式的基础上对简单工厂做了更高层次的抽象,抽象出工厂接口。
        //工厂方法模式符合对修改关闭、对扩展开放的原则,具有更好的扩展性。
        //但工厂方法模式引入了更高的代码复杂度,学习和调用成本更高,且创建每个类的实例都需要相关的工厂类配合,所以某些情况下需要增加很多相关类。
        //同时简单工厂和工厂方法结合可降低调用方的复杂度。
    
    
        /// <summary>
        /// 手机工厂接口(只要实现了该接口的类就是具备生产某种手机能力的工厂,这里有可能是苹果手机或者小米手机工厂)
        /// </summary>
        public interface IMobilePhoneFactory
        {
            MobilePhone CreateMobilePhone();
        }
    
        public class IPhoneFactory : IMobilePhoneFactory
        {
            public MobilePhone CreateMobilePhone()
            {
                return new IPhone("iphone6 plus", 6500);
            }
        }
    
        public class XiaoMiFactory : IMobilePhoneFactory
        {
            public MobilePhone CreateMobilePhone()
            {
                return new XiaoMi("小米2代", 2500);
            }
        }
        #endregion
    
        #region 工厂方法与简单工厂结合
        public class MobliePhoneFactoryHelper
        {
            public static MobilePhone GetPhone(string name)
            {
                IMobilePhoneFactory factory = null;
                switch (name)
                {
                    case "苹果":
                        factory = new IPhoneFactory();
                        return factory.CreateMobilePhone();
                    case "小米":
                        factory = new XiaoMiFactory();
                        return factory.CreateMobilePhone();
                    default:
                        throw new NotImplementedException("不存在的品牌");
                }
            }
        }
        #endregion
    
    }
  • 相关阅读:
    Proteus仿真与实际的差别
    用 Proteus学习51单片机之1602液晶
    用Proteus学习51单片机之中断
    用Proteus学习51单片机之键盘
    用Proteus学习51单片机之数码管
    PHPnow 安装服务 [ Apache_pn ] 失败的解决方法 for[windows7/vista]
    chrome 不能打印背景图的解决方法
    JS 无块级作用域
    IE6下zindex犯癫不起作用bug的初步研究
    ie6下的js调试工具companion.js
  • 原文地址:https://www.cnblogs.com/huangzelin/p/4253220.html
Copyright © 2011-2022 走看看