番禺网站设计公司,关键词排名优化如何,设计模式,seo搜索工具栏AbstractFactory抽象工厂模式#xff08;创建型#xff09; 作用#xff1a; 这种模式支持创建不同的对象#xff0c;这些对象功能接近且一般都是在一起创建的。抽象工厂被具体化成不同的工厂来创建类似产品的不同产品。这种模式将类于使用的客户端分离以便通过工厂来创建。…AbstractFactory抽象工厂模式创建型 作用 这种模式支持创建不同的对象这些对象功能接近且一般都是在一起创建的。抽象工厂被具体化成不同的工厂来创建类似产品的不同产品。这种模式将类于使用的客户端分离以便通过工厂来创建。这样各类产品可以方便的变化而不需要改变使用者的结构。 RoleThis pattern supports the creation of products that exist in families and are designed to be produced together. The abstract factory can be refined to concrete factories,each of which can create different products of different types and in different combinations. The pattern isolates the product definitions and their class names from the client so that the only way to get one of them is through a factory. For this reason, product families can easily be interchanged or updated without upsetting the structure of the client. 设计 抽象工厂接口IAbstractFactory含有创建各类抽象产品abstract products的操作 工厂1Factory1, 工厂2Factory2实现了抽象工厂接口IAbstractFactory的创建产品的操作抽象产品接口IProductAIProductB含有某类产品自有的操作具体产品ProductA1, ProductA2, ProductB1, ProductB2实现了抽象产品接口的具体产品类这些具体产品类由相应的工厂生产 举例 AbstractFactory 生产一种钢笔FactoryA 英雄钢笔厂FactoryB 永生钢笔厂AbstractProduct 钢笔产品ProductA1 英雄金笔ProductB1 永生金笔 ProductA2 英雄软笔ProductB2 永生软笔代码 代码 using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace AbstractFactory{ //抽象工厂 abstract class AbstractFactory { abstract public AbstractWheel CreatWheel(); abstract public AbstractOilBox CreatOilBox(); } abstract class AbstractWheel { public AbstractWheel() { //Console.Write(Create a AbstractProduct); } } abstract class AbstractOilBox { public AbstractOilBox() { } } class BMWFactory : AbstractFactory { public override AbstractWheel CreatWheel() { return new BMWWheel(); } public override AbstractOilBox CreatOilBox() { return new BMWOilBox(); } } class BMWWheel : AbstractWheel { public BMWWheel() { Console.WriteLine(Create a BMWwheel); } } class BMWOilBox : AbstractOilBox { public BMWOilBox() { Console.WriteLine(Create a BMWOilBox); } } class BORAWheel:AbstractWheel { public BORAWheel() { Console.Write(Create a BORAWheel); } } class BORAOilBox:AbstractOilBox { public BORAOilBox() { Console.Write(Create a BORAOilBox); } } class BORAFactory:AbstractFactory { public override AbstractWheel CreatWheel() { return new BORAWheel(); } public override AbstractOilBox CreatOilBox() { return new BORAOilBox(); } } class Program { static void Main(string[] args) { AbstractFactory factory null; factory new BORAFactory(); factory.CreatWheel(); factory.CreatOilBox(); Console.ReadLine(); factory new BMWFactory(); factory.CreatWheel(); factory.CreatOilBox(); Console.ReadLine(); } }} 使用场景 1、系统不依赖他要使用对象的创建、组成、表示2、系统需要配置使用多组对象3、创建对象和工厂必须是强制使用的4、重点是要暴露接口而不是实现 Use the Abstract Factory pattern when…• A system should be independent of how its products are created, composed, and represented.• A system can be configured with one of multiple families of products.• The constraint requiring products from the same factory to be used together must be enforced.• The emphasis is on revealing interfaces, not implementations. 总结 抽象工厂模式是一种创建型模式目的是解决实例化对象时所带来的问题。在现实中系统总是需要变化来实例化不同的对象因此封装“创建操作”到抽象工厂由具体的工厂来实例相应的对象。抽象工厂模式面向接口将实例化对象的操作封装起来不同的工厂都实现实例对象的操作。 在系统中经常会遇到“一组相互依赖的对象”的创建工作同时由于需求的变化往往存在更多系列对象的创建工作。面对这种问题避免使用常规的对象创建方法使用“封装实例华对象”来避免系统和这种“多系列具体对象创建工作”的紧耦合。 在需求稳定。系统基本无变化的情况下松耦合和紧耦合是没有区别的但是需求的变化要求在某些方面将系统解耦抽象工厂就是将对象的实例化和系统解耦。 《设计模式》提供一个接口让该接口负责创建一系列“相关或者相互依赖的对象”无需指定他们的具体类。Abstract Factory模式的几个要点 1、如果没有应对“多系列对象构建”的需求变化则没有必要使用Abstract Factory模式。 2、“系列对象”指的是这项对象之间有相互依赖、或作用的关系。 3、Abstract Factory模式主要在于应对“新系列”的需求变动。缺点是难以应对“新对象”的需求变动。这一点应该注意就像前面说的如果我们现在要在加入其他系列的类代码的改动会很大。 4、Abstract Factory模式经常和Factory Method模式共同组合来应对“对象创建”的需求变化。 转载于:https://www.cnblogs.com/utopia/archive/2010/02/28/1675216.html