• 设计模式(3)工厂方法模式


    模式介绍

    Factory Method模式是一个Creation模式,它定义了用于创建对象的接口,但是没有指定该接口的各个实现将实例化什么对象。

    这意味着,当使用这个模式时,您可以定义对象的某些方法和属性,这些方法和属性对于使用Factory方法创建的所有对象都是通用的,但是让各个Factory方法定义它们将实例化的特定对象。

    示例

    我们还是使用三明治举例子

    三明治组成抽象类

    /// <summary>
    /// Product
    /// </summary>
    abstract class Ingredient { }
    

    三明治组成具体类

    /// <summary>
    /// Concrete Product
    /// </summary>
    class Bread : Ingredient { }
    
    /// <summary>
    /// Concrete Product
    /// </summary>
    class Turkey : Ingredient { }
    
    /// <summary>
    /// Concrete Product
    /// </summary>
    class Lettuce : Ingredient { }
    
    /// <summary>
    /// Concrete Product
    /// </summary>
    class Mayonnaise : Ingredient { }
    

    创建三明治的抽象类

    /// <summary>
    /// Creator
    /// </summary>
    abstract class Sandwich
    {
        private List<Ingredient> _ingredients = new List<Ingredient>();
    
        public Sandwich()
        {
            CreateIngredients();
        }
    
        //Factory method
        public abstract void CreateIngredients();
    
        public List<Ingredient> Ingredients
        {
            get { return _ingredients; }
        }
    }
    

    创建三明治的具体类
    (下面那个是个超级三明治...)

    /// <summary>
    /// Concrete Creator
    /// </summary>
    class TurkeySandwich : Sandwich
    {
        public override void CreateIngredients()
        {
            Ingredients.Add(new Bread());
            Ingredients.Add(new Mayonnaise());
            Ingredients.Add(new Lettuce());
            Ingredients.Add(new Turkey());
            Ingredients.Add(new Turkey());
            Ingredients.Add(new Bread());
        }
    }
    
    /// <summary>
    /// Concrete Creator
    /// </summary>
    class Dagwood : Sandwich
    {
        public override void CreateIngredients()
        {
            Ingredients.Add(new Bread());
            Ingredients.Add(new Turkey());
            Ingredients.Add(new Turkey());
            Ingredients.Add(new Lettuce());
            Ingredients.Add(new Lettuce());
            Ingredients.Add(new Mayonnaise());
            Ingredients.Add(new Bread());
            Ingredients.Add(new Turkey());
            Ingredients.Add(new Turkey());
            Ingredients.Add(new Lettuce());
            Ingredients.Add(new Lettuce());
            Ingredients.Add(new Mayonnaise());
            Ingredients.Add(new Bread());
            Ingredients.Add(new Turkey());
            Ingredients.Add(new Turkey());
            Ingredients.Add(new Lettuce());
            Ingredients.Add(new Lettuce());
            Ingredients.Add(new Mayonnaise());
            Ingredients.Add(new Bread());
            Ingredients.Add(new Turkey());
            Ingredients.Add(new Turkey());
            Ingredients.Add(new Lettuce());
            Ingredients.Add(new Lettuce());
            Ingredients.Add(new Mayonnaise());
            Ingredients.Add(new Bread());
            Ingredients.Add(new Turkey());
            Ingredients.Add(new Turkey());
            Ingredients.Add(new Lettuce());
            Ingredients.Add(new Lettuce());
            Ingredients.Add(new Mayonnaise());
            Ingredients.Add(new Bread());
            Ingredients.Add(new Turkey());
            Ingredients.Add(new Turkey());
            Ingredients.Add(new Lettuce());
            Ingredients.Add(new Lettuce());
            Ingredients.Add(new Mayonnaise());
            Ingredients.Add(new Bread());
        }
    }
    

    客户端调用

    class Program
    {
        static void Main(string[] args)
        {
            var turkeySandwich = new TurkeySandwich();
            var dagwood = new Dagwood();
            //Do something with these sandwiches (like, say, eat them).
            ...
        }
    }
    

    总结

    Factory Method模式提供了一种可以实例化对象的方式,但是创建这些实例的细节由实例类自己定义。

    源代码

    https://github.com/exceptionnotfound/DesignPatterns/tree/master/FactoryMethodPattern

    原文

    https://www.exceptionnotfound.net/the-daily-design-pattern-factory-method/

  • 相关阅读:
    用C#制作PDF文件全攻略
    侦测软件鸟哥linux学习笔记之源代码安装侦测软件
    类模式Java设计模式之十五(桥接模式)类模式
    安装配置Maven入门什么是maven和maven的安装和配置安装配置
    查看表空间oracle rman catalog目录数据库创建过程查看表空间
    产品群体互联网产品设计规划产品群体
    问题修改highcharts 导出图片 .net c#(二)问题修改
    音频播放android4.2音频管理技术音频播放
    重启启动eclipse 中启动Tomcat超时了错误如下:重启启动
    关系建立对于内向、不善于社交的人来说,如何建立人脉?关系建立
  • 原文地址:https://www.cnblogs.com/talentzemin/p/9810257.html
Copyright © 2020-2023  润新知