zoukankan      html  css  js  c++  java
  • C#设计模式:工厂模式

    一,工厂模式

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace _2._1工厂模式
    {
        class Program
        {
            static void Main(string[] args)
            {
                IPeopleFactory a = new ChinePeople();
                Console.Write("中国人人穿了" + a.ShowShoes("红色") + "," + a.ShowClothes("红色"));
                IPeopleFactory b = new USAPeople();
                Console.Write("美国人穿了" + b.ShowShoes("白色") + "," + b.ShowClothes("白色"));
            }
        }
        public interface IPeopleFactory
        {
            string ShowShoes(string type);
            string ShowClothes(string type);
        }
        public class ChinePeople : IPeopleFactory
        {
            public string ShowShoes(string type)
            {
                return type + "上衣";
            }
            public string ShowClothes(string type)
            {
                return type + "裤子";
            }
        }
        public class USAPeople : IPeopleFactory
        {
            public string ShowShoes(string type)
            {
                return type + "上衣";
            }
            public string ShowClothes(string type)
            {
                return type + "裤子";
            }
        }
    }

     二,主要用于隔离类对象的使用者和具体类型之间的耦合关系和实现多态等

    与抽象工厂模式的区别在抽象工厂模式中详解

    三,有时候我们有种疑惑,为什么我们不使用工厂模式,而使用现在的IOC呢?

    其实本质上还是因为IOC是通过反射机制来实现的。当我们的需求出现变动时,工厂模式会需要进行相应的变化。但是IOC的反射机制允许我们不重新编译代码,因为它的对象都是动态生成的。

  • 相关阅读:
    深入解析MySQL分区(Partition)功能
    使用mysqlreplicate命令快速搭建 Mysql 主从复制
    MySQL优化/面试,看这一篇就够了
    Nginx+Keepalived高可用集群
    高并发核心技术
    Redis详解
    memcached 常用命令及使用说明
    MemCached详解
    音频和视频
    frameset 和iframe框架标签
  • 原文地址:https://www.cnblogs.com/May-day/p/8418814.html
Copyright © 2011-2022 走看看