• c# 设计模式(一) 工厂模式


    源代码在github上面,需要的自己进行下载:https://github.com/yuzhoukamen/UnikmDesignPattern.git

    工厂模式(Factory Pattern)是最常用的设计模式之一。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。

    在工厂模式中,我们在创建对象时不会对客户端暴露创建逻辑,并且是通过使用一个共同的接口来指向新创建的对象。

    介绍

    意图:定义一个创建对象的接口,让其子类自己决定实例化哪一个工厂类,工厂模式使其创建过程延迟到子类进行。

    主要解决:主要解决接口选择的问题。

    何时使用:我们明确地计划不同条件下创建不同实例时。

    如何解决:让其子类实现工厂接口,返回的也是一个抽象的产品。

    关键代码:创建过程在其子类执行。

    应用实例: 1、您需要一辆汽车,可以直接从工厂里面提货,而不用去管这辆汽车是怎么做出来的,以及这个汽车里面的具体实现。 2、Hibernate 换数据库只需换方言和驱动就可以。

    优点: 1、一个调用者想创建一个对象,只要知道其名称就可以了。 2、扩展性高,如果想增加一个产品,只要扩展一个工厂类就可以。 3、屏蔽产品的具体实现,调用者只关心产品的接口。

    缺点:每次增加一个产品时,都需要增加一个具体类和对象实现工厂,使得系统中类的个数成倍增加,在一定程度上增加了系统的复杂度,同时也增加了系统具体类的依赖。这并不是什么好事。

    使用场景: 1、日志记录器:记录可能记录到本地硬盘、系统事件、远程服务器等,用户可以选择记录日志到什么地方。 2、数据库访问,当用户不知道最后系统采用哪一类数据库,以及数据库可能有变化时。 3、设计一个连接服务器的框架,需要三个协议,"POP3"、"IMAP"、"HTTP",可以把这三个作为产品类,共同实现一个接口。

    注意事项:作为一种创建类模式,在任何需要生成复杂对象的地方,都可以使用工厂方法模式。有一点需要注意的地方就是复杂对象适合使用工厂模式,而简单对象,特别是只需要通过 new 就可以完成创建的对象,无需使用工厂模式。如果使用工厂模式,就需要引入一个工厂类,会增加系统的复杂度。

    实现

    我们将创建一个 IShape 接口和实现 IShape 接口的实体类。下一步是定义工厂类 ShapeFactory

    FactoryPatternDemo,我们的演示类使用 ShapeFactory 来获取 Shape 对象。它将向 ShapeFactory 传递信息(CIRCLE / RECTANGLE / SQUARE),以便获取它所需对象的类型。

    工厂模式的 UML 图

    1、创建接口

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Unikm.DP.FactoryPattern
    {
        /// <summary>
        /// 创建一个接口
        /// </summary>
        public interface IShape
        {
            /// <summary>
            /// 
            /// </summary>
            void Draw();
        }
    }

     2、创建类

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Unikm.DP.FactoryPattern
    {
        /// <summary>
        /// 
        /// </summary>
        public class Circle : IShape
        {
            /// <summary>
            /// 
            /// </summary>
            public void Draw()
            {
                Console.WriteLine("Inside Circle::draw() method.");
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Unikm.DP.FactoryPattern
    {
        /// <summary>
        /// 
        /// </summary>
        public class Rectangle : IShape
        {
            /// <summary>
            /// 
            /// </summary>
            public void Draw()
            {
                Console.WriteLine("Inside Rectangle::draw() method.");
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Unikm.DP.FactoryPattern
    {
        /// <summary>
        /// 
        /// </summary>
        public class Square : IShape
        {
            /// <summary>
            /// 
            /// </summary>
            public void Draw()
            {
                Console.WriteLine("Inside Square::draw() method.");
            }
        }
    }

    3、创建工厂

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Unikm.DP.FactoryPattern
    {
        /// <summary>
        /// 创建一个工厂,生成基于给定信息的实体类的对象。
        /// </summary>
        public class ShapeFactory
        {
            /// <summary>
            /// 使用 getShape 方法获取形状类型的对象
            /// </summary>
            /// <param name="shapeType"></param>
            /// <returns></returns>
            public IShape GetShape(string shapeType)
            {
                if (shapeType == null)
                {
                    return null;
                }
    
                if (shapeType.ToUpper().Equals("CIRCLE"))
                {
                    return new Circle();
                }
                else if (shapeType.ToUpper().Equals("RECTANGLE"))
                {
                    return new Rectangle();
                }
                else if (shapeType.ToUpper().Equals("SQUARE"))
                {
                    return new Square();
                }
    
                return null;
            }
        }
    }

    4、调用demo

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Unikm.DP.FactoryPattern
    {
        /// <summary>
        /// 
        /// </summary>
        public class FactoryPatternDemo
        {
            /// <summary>
            /// 
            /// </summary>
            /// <param name="args"></param>
            public static void Main(String[] args)
            {
                ShapeFactory shapeFactory = new ShapeFactory();//使用该工厂,通过传递类型信息来获取实体类的对象。
    
                IShape shape1 = shapeFactory.GetShape("CIRCLE");//获取 Circle 的对象,并调用它的 draw 方法
    
                shape1.Draw();//调用 Circle 的 draw 方法
                
                IShape shape2 = shapeFactory.GetShape("RECTANGLE");//获取 Rectangle 的对象,并调用它的 draw 方法
                
                shape2.Draw();//调用 Rectangle 的 draw 方法
                
                IShape shape3 = shapeFactory.GetShape("SQUARE");//获取 Square 的对象,并调用它的 draw 方法
    
                shape3.Draw();//调用 Square 的 draw 方法
    
                Console.ReadLine();
            }
        }
    }
    人生,总是有一些空城旧事,年华未央;总是有些季节,一季花凉,满地忧伤。许多事,看开了,便会峰回路转;许多梦,看淡了,便会云开日出。学会思索,学会珍藏,微笑领悟,默默坚强。
  • 相关阅读:
    Docker+geoserver发布shp地图服务
    Docker中运行命令时提示:Cannot connect to the Docker daemony...以及设置docker开机启动
    Docker在服务器之间怎样导入导出镜像(服务器之间容器复制)
    Docker+Tomcat+geoserver+shp发布地图服务
    Windows中将文件压缩成linux支持的tar.gz格式的压缩包
    Docker怎样提交镜像(把容器打成镜像)
    Docker中宿主机与容器之间互传文件(docker cp的方式)
    移动零
    旋转数组
    有序数组的平方
  • 原文地址:https://www.cnblogs.com/yuzhou133/p/10432601.html
Copyright © 2020-2023  润新知