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.SimpleFactory
    {
        class Program
        {
            static void Main(string[] args)
            {
                MobilePhone phone = MobilePhoneFactory.GetMobilePhone("苹果");
                phone.Show();
                Console.WriteLine();
    
                MobilePhone xiaomo = MobilePhoneFactory.GetMobilePhone("小米");
                xiaomo.Show();
                Console.ReadKey();
            }
        }
    
    
        //简单工厂(又名静态工厂)
        //优点:该模式代码简单易于理解,学习成本较小。
        //缺点:业务变动时需要修改工厂中的已有代码,违背了对修改关闭、对扩展开放的设计原则,不利于程序的扩展和维护。
        public class MobilePhoneFactory
        {
            public static MobilePhone GetMobilePhone(string name)
            { 
                switch(name)
                {
                    case "苹果":
                        return new IPhone("iphone6", 5000);
                    case "小米":
                        return new XiaoMi("小米青春版", 2000);
                    default:
                        throw new NotImplementedException("不存在的品牌");
                }
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace DPC.Domain
    {
        public abstract class MobilePhone
        {
            public MobilePhone(string model,decimal price)
            {
                this.Model = model;
                this.Price = price;
            }
    
            /// <summary>
            /// 名字
            /// </summary>
            public abstract string Name { get; }
    
            /// <summary>
            /// 型号
            /// </summary>
            public string Model { get; private set; }
    
            /// <summary>
            /// 价格
            /// </summary>
            public decimal Price { get; private set; }
    
            /// <summary>
            /// 综合情况展示
            /// </summary>
            public virtual void Show()
            {
                Console.WriteLine("品牌:{0},型号:{1},价格:{2}", this.Name, this.Model, this.Price);
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace DPC.Domain
    {
        public class IPhone : MobilePhone
        {
            private static readonly string name = "苹果";
    
            public IPhone(string model, decimal price)
                : base(model, price) 
            { }
    
            public override string Name
            {
                get { return name; }
            }
    
            public override void Show()
            {
                Console.WriteLine("土豪手机闪亮登场......");
                Console.WriteLine("品牌:{0},型号:{1},价格:{2}",this.Name,this.Model,this.Price);
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace DPC.Domain
    {
        public class XiaoMi : MobilePhone
        {
            public XiaoMi(string model, decimal price)
                : base(model, price)
            { }
    
            public override string Name
            {
                get { return "小米"; }
            }
    
            public override void Show()
            {
                Console.WriteLine("made in china手机闪亮登场......");
                Console.WriteLine("品牌:{0},型号:{1},价格:{2}", this.Name, this.Model, this.Price);
            }
        }
    }
  • 相关阅读:
    NSInvocation的基本使用
    OC和JS代码的互调
    HTTPS的基本使用
    数据安全
    AFN框架
    部分文件的MIMEType
    linux中文件颜色,蓝色,白色等各自代表的含义
    CentOS下mysql常用命令
    mysql 完整备份和恢复
    mysqldump 导出提示Couldn't execute SELECT COLUMN_NAME...
  • 原文地址:https://www.cnblogs.com/huangzelin/p/4253213.html
Copyright © 2011-2022 走看看