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
    
    }
  • 相关阅读:
    nginx学习(十):nginx搭建2台tomcat集群
    IOT设备SmartConfig实现
    AIDL原理分析
    MySQL升级-CentOS6.8
    CentOS更新yum源
    .net core微服务通信——gRPC(下)
    .net core微服务通信——gRPC(上)
    实时web应用方案——SignalR(.net core)
    redis常见Bug及雪崩、穿透、击穿解析
    asp.net core托管到windows服务
  • 原文地址:https://www.cnblogs.com/huangzelin/p/4253220.html
Copyright © 2011-2022 走看看