分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

Definition

Provide an interface for creating families of related or dependent objects without specifying their concrete classes.

Participants

The classes and/or objects participating in this pattern are:

  • AbstractFactory (ContinentFactory)

    • Declares an interface for operations that create abstract products
  • ConcreteFactory (AfricaFactory, AmericaFactory)
    • Implements the operations to create concrete product objects
  • AbstractProduct (Herbivore, Carnivore)
    • Declares an interface for a type of product object
  • Product (Wildebeest, Lion, Bison, Wolf)
    • Defines a product object to be created by the corresponding concrete factory
    • Implements the AbstractProduct interface
  • Client (AnimalWorld)
    • Uses interfaces declared by AbstractFactory and AbstractProduct classes

Sample Code in C#


This structural code demonstrates the Abstract Factory pattern creating parallel hierarchies of objects. Object creation has been abstracted and there is no need for hard-coded class names in the client code.

// --------------------------------------------------------------------------------------------------------------------// <copyright company="Chimomo's Company" file="Program.cs">// Respect the work.// </copyright>// <summary>// Structural Abstract Factory Design Pattern.// </summary>// --------------------------------------------------------------------------------------------------------------------namespace CSharpLearning{    using System;    /// <summary>    /// Startup class for Structural Abstract Factory Design Pattern.    /// </summary>    internal static class MainApp    {        #region Public Methods and Operators        /// <summary>        /// Entry point into console application.        /// </summary>        public static void Main()        {            // Abstract factory #1            AbstractFactory factory1 = new ConcreteFactory1();            var client1 = new Client(factory1);            client1.Run();            // Abstract factory #2            AbstractFactory factory2 = new ConcreteFactory2();            var client2 = new Client(factory2);            client2.Run();        }        #endregion    }    /// <summary>    /// The 'AbstractFactory' abstract class    /// </summary>    internal abstract class AbstractFactory    {        #region Public Methods and Operators        /// <summary>        /// The create product a.        /// </summary>        /// <returns>        /// The <see cref="AbstractProductA"/>.        /// </returns>        public abstract AbstractProductA CreateProductA();        /// <summary>        /// The create product b.        /// </summary>        /// <returns>        /// The <see cref="AbstractProductB"/>.        /// </returns>        public abstract AbstractProductB CreateProductB();        #endregion    }    /// <summary>    /// The 'ConcreteFactory1' class    /// </summary>    internal class ConcreteFactory1 : AbstractFactory    {        #region Public Methods and Operators        /// <summary>        /// The create product a.        /// </summary>        /// <returns>        /// The <see cref="AbstractProductA"/>.        /// </returns>        public override AbstractProductA CreateProductA()        {            return new ProductA1();        }        /// <summary>        /// The create product b.        /// </summary>        /// <returns>        /// The <see cref="AbstractProductB"/>.        /// </returns>        public override AbstractProductB CreateProductB()        {            return new ProductB1();        }        #endregion    }    /// <summary>    /// The 'ConcreteFactory2' class    /// </summary>    internal class ConcreteFactory2 : AbstractFactory    {        #region Public Methods and Operators        /// <summary>        /// The create product a.        /// </summary>        /// <returns>        /// The <see cref="AbstractProductA"/>.        /// </returns>        public override AbstractProductA CreateProductA()        {            return new ProductA2();        }        /// <summary>        /// The create product b.        /// </summary>        /// <returns>        /// The <see cref="AbstractProductB"/>.        /// </returns>        public override AbstractProductB CreateProductB()        {            return new ProductB2();        }        #endregion    }    /// <summary>    /// The 'AbstractProductA' abstract class    /// </summary>    internal abstract class AbstractProductA    {    }    /// <summary>    /// The 'AbstractProductB' abstract class    /// </summary>    internal abstract class AbstractProductB    {        #region Public Methods and Operators        /// <summary>        /// The interact.        /// </summary>        /// <param name="a">        /// The a.        /// </param>        public abstract void Interact(AbstractProductA a);        #endregion    }    /// <summary>    /// The 'ProductA1' class    /// </summary>    internal class ProductA1 : AbstractProductA    {    }    /// <summary>    /// The 'ProductB1' class    /// </summary>    internal class ProductB1 : AbstractProductB    {        #region Public Methods and Operators        /// <summary>        /// The interact.        /// </summary>        /// <param name="a">        /// The a.        /// </param>        public override void Interact(AbstractProductA a)        {            Console.WriteLine(this.GetType().Name + " interacts with " + a.GetType().Name);        }        #endregion    }    /// <summary>    /// The 'ProductA2' class    /// </summary>    internal class ProductA2 : AbstractProductA    {    }    /// <summary>    /// The 'ProductB2' class    /// </summary>    internal class ProductB2 : AbstractProductB    {        #region Public Methods and Operators        /// <summary>        /// The interact.        /// </summary>        /// <param name="a">        /// The a.        /// </param>        public override void Interact(AbstractProductA a)        {            Console.WriteLine(this.GetType().Name + " interacts with " + a.GetType().Name);        }        #endregion    }    /// <summary>    /// The 'Client' class. Interaction environment for the products.    /// </summary>    internal class Client    {        #region Fields        /// <summary>        /// The abstract product a.        /// </summary>        private AbstractProductA abstractProductA;        /// <summary>        /// The abstract product b.        /// </summary>        private AbstractProductB abstractProductB;        #endregion        // Constructor        #region Constructors and Destructors        /// <summary>        /// Initializes a new instance of the <see cref="Client"/> class.        /// </summary>        /// <param name="factory">        /// The factory.        /// </param>        public Client(AbstractFactory factory)        {            this.abstractProductA = factory.CreateProductA();            this.abstractProductB = factory.CreateProductB();        }        #endregion        #region Public Methods and Operators        /// <summary>        /// The run.        /// </summary>        public void Run()        {            this.abstractProductB.Interact(this.abstractProductA);        }        #endregion    }}// Output:/*ProductB1 interacts with ProductA1ProductB2 interacts with ProductA2*/

This real-world code demonstrates the creation of different animal worlds for a computer game using different factories. Although the animals created by the Continent factories are different, the interactions among the animals remain the same.

// --------------------------------------------------------------------------------------------------------------------// <copyright company="Chimomo's Company" file="Program.cs">// Respect the work.// </copyright>// <summary>// Real-World Abstract Factory Design Pattern.// </summary>// --------------------------------------------------------------------------------------------------------------------namespace CSharpLearning{    using System;    /// <summary>    /// Startup class for Real-World Abstract Factory Design Pattern.    /// </summary>    internal static class MainApp    {        #region Public Methods and Operators        /// <summary>        /// Entry point into console application.        /// </summary>        public static void Main()        {            // Create and run the African animal world            ContinentFactory africa = new AfricaFactory();            var world = new AnimalWorld(africa);            world.RunFoodChain();            // Create and run the American animal world            ContinentFactory america = new AmericaFactory();            world = new AnimalWorld(america);            world.RunFoodChain();        }        #endregion    }    /// <summary>    /// The 'AbstractFactory' abstract class    /// </summary>    internal abstract class ContinentFactory    {        #region Public Methods and Operators        /// <summary>        /// The create carnivore.        /// </summary>        /// <returns>        /// The <see cref="Carnivore"/>.        /// </returns>        public abstract Carnivore CreateCarnivore();        /// <summary>        /// The create herbivore.        /// </summary>        /// <returns>        /// The <see cref="Herbivore"/>.        /// </returns>        public abstract Herbivore CreateHerbivore();        #endregion    }    /// <summary>    /// The 'ConcreteFactory1' class    /// </summary>    internal class AfricaFactory : ContinentFactory    {        #region Public Methods and Operators        /// <summary>        /// The create carnivore.        /// </summary>        /// <returns>        /// The <see cref="Carnivore"/>.        /// </returns>        public override Carnivore CreateCarnivore()        {            return new Lion();        }        /// <summary>        /// The create herbivore.        /// </summary>        /// <returns>        /// The <see cref="Herbivore"/>.        /// </returns>        public override Herbivore CreateHerbivore()        {            return new Wildebeest();        }        #endregion    }    /// <summary>    /// The 'ConcreteFactory2' class    /// </summary>    internal class AmericaFactory : ContinentFactory    {        #region Public Methods and Operators        /// <summary>        /// The create carnivore.        /// </summary>        /// <returns>        /// The <see cref="Carnivore"/>.        /// </returns>        public override Carnivore CreateCarnivore()        {            return new Wolf();        }        /// <summary>        /// The create herbivore.        /// </summary>        /// <returns>        /// The <see cref="Herbivore"/>.        /// </returns>        public override Herbivore CreateHerbivore()        {            return new Bison();        }        #endregion    }    /// <summary>    /// The 'AbstractProductA' abstract class    /// </summary>    internal abstract class Herbivore    {    }    /// <summary>    /// The 'AbstractProductB' abstract class    /// </summary>    internal abstract class Carnivore    {        #region Public Methods and Operators        /// <summary>        /// The eat.        /// </summary>        /// <param name="h">        /// The h.        /// </param>        public abstract void Eat(Herbivore h);        #endregion    }    /// <summary>    /// The 'ProductA1' class    /// </summary>    internal class Wildebeest : Herbivore    {    }    /// <summary>    /// The 'ProductB1' class    /// </summary>    internal class Lion : Carnivore    {        #region Public Methods and Operators        /// <summary>        /// The eat.        /// </summary>        /// <param name="h">        /// The h.        /// </param>        public override void Eat(Herbivore h)        {            // Eat Wildebeest            Console.WriteLine(this.GetType().Name + " eats " + h.GetType().Name);        }        #endregion    }    /// <summary>    /// The 'ProductA2' class    /// </summary>    internal class Bison : Herbivore    {    }    /// <summary>    /// The 'ProductB2' class    /// </summary>    internal class Wolf : Carnivore    {        #region Public Methods and Operators        /// <summary>        /// The eat.        /// </summary>        /// <param name="h">        /// The h.        /// </param>        public override void Eat(Herbivore h)        {            // Eat Bison            Console.WriteLine(this.GetType().Name + " eats " + h.GetType().Name);        }        #endregion    }    /// <summary>    /// The 'Client' class    /// </summary>    internal class AnimalWorld    {        #region Fields        /// <summary>        /// The carnivore.        /// </summary>        private Carnivore carnivore;        /// <summary>        /// The herbivore.        /// </summary>        private Herbivore herbivore;        #endregion        // Constructor        #region Constructors and Destructors        /// <summary>        /// Initializes a new instance of the <see cref="AnimalWorld"/> class.        /// </summary>        /// <param name="factory">        /// The factory.        /// </param>        public AnimalWorld(ContinentFactory factory)        {            this.carnivore = factory.CreateCarnivore();            this.herbivore = factory.CreateHerbivore();        }        #endregion        #region Public Methods and Operators        /// <summary>        /// The run food chain.        /// </summary>        public void RunFoodChain()        {            this.carnivore.Eat(this.herbivore);        }        #endregion    }}// Output:/*Lion eats WildebeestWolf eats Bison*/

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

Design Pattern - Abstract Factory(C#)相关推荐

  1. Head First Design Pattern 读书笔记(4) 工厂模式

    2019独角兽企业重金招聘Python工程师标准>>> Head First Design Pattern 读书笔记(4) Factory Pattern 工厂模式 ##Factor ...

  2. Abstract Factory(抽象工厂)实践

    Abstract Factory(抽象工厂) 抽象工厂,按字面含义来理解,就是一个不存在的工厂,只是抽象出来的一个概念工厂,反应到代码中,可以理解为定义了固定操作接口的一个抽象类,这个类不完成任何事( ...

  3. Abstract Factory(抽象工厂)--对象创建模式

    Abstract Factory (抽象工厂)–对象创建模式 一.意图 提供一个创建一系列相关或者相互依赖的接口,而无需指定它们具体的类. 二.动机 1.在软件系统中,经常面临着"一系列相互 ...

  4. 前端初学者的Ant Design Pro V6总结(上)

    前端初学者的Ant Design Pro V6总结(上) 一.UI组件开发流程 () => {} 通用(异步)函数 useEmotionCss 定义CSS useModel获取全局状态 useC ...

  5. 傻白探索Chiplet,Modular Routing Design for Chiplet-based Systems(十一)

    阅读了Modular Routing Design for Chiplet-based Systems这篇论文,是关于多chiplet通信的,个人感觉核心贡献在于实现了 deadlock-freedo ...

  6. Pattern And Matcher(java)

    Pattern And Matcher(java) 1.什么是Pattern pattern为正则表达式的编译表示形式,指定为字符串的正则表达式必须首先被编译为此类的实例.构造Pattern的方法是私 ...

  7. 设计模式——Abstract Factory(抽象工厂)模式

    抽象的零件:Item类 Item类是Link和Tray类的父类,这样Link和Tray类就具有可替换性了. caption字段表示项目的"标题" makeHTML方法是抽象,需要子 ...

  8. 类似 Observer Pattern 的 NSNotificationCenter (实例)

    NSNotificationCenter 是 Cococa消息中心,统一管理单进程内不同线程的消息通迅,其职责只有两个:  1,提供"观查者们"对感兴趣消息的监听注册 [[NSNo ...

  9. java pattern类使用说明(正则表达式)

    public final class Patternextends Objectimplements Serializable正则表达式的编译表示形式. 指定为字符串的正则表达式必须首先被编译为此类的 ...

最新文章

  1. php饼图只有一个小方块_如何做出PHP数据饼图
  2. Linux搜索查找命令合集
  3. 在pl/sql中使用exp/imp工具实现oracle数据导出/导入
  4. ASP.NET MVC+Spring.net+Nhibernate+EasyUI+Jquery开发案例(1)
  5. 现在是不是很多人都不愿意在银行存钱?
  6. loading gif 透明_搞笑GIF:有这样的女朋友下班哪里都不想去
  7. 3.hello hibernate
  8. Python实现机房管理软件的文件分发功能
  9. 悲苦手机命,“熬”在新零售
  10. C 线程同步的四种方式(Linux)
  11. 关机指令代码_iPhone这些隐藏代码你肯定不知道
  12. 刚刚,2020年中国信息通信技术服务大会盛大召开!
  13. 必修的十堂电影课(男人篇)
  14. 【接口测试实战(三)】接口测试用例的编写
  15. Banner轮播图的基本使用
  16. linux docker安装 制作Elasticsearch容器镜像 并上传docker hub
  17. oracle做分页式报表,报表性能优化方案之单数据集分页SQL实现层式报表
  18. 日渐临近的苹果秋季发布会,iOS 11 GM 固件到底提前泄露了哪些秘密?
  19. racle varchar,date互转,number,varchar互转
  20. 毕业设计 基于单片机的数字出租车计价器

热门文章

  1. Java带有运算符的字符串转换为Long型
  2. Sublime Text插件的离线安装-使用htmlprettify美化您的HTML代码
  3. 关于vue内只要html元素的代码
  4. 动态代理机制详解(JDK 和CGLIB,Javassist,ASM)
  5. 各版本jdk下载地址
  6. 游戏的革命从虚拟现实开始
  7. 【邓侃】哈佛大学机器翻译开源项目 OpenNMT的工作原理
  8. 浅谈Java内存泄漏问题
  9. Fragment 底部菜单栏
  10. 网络主机监控-nagios应用漫谈(三)