设计模式学习总结1 创建型1 Abstract Factory抽象工厂模式

AbstractFactory抽象工厂模式(创建型

作用:

这种模式支持创建不同的对象,这些对象功能接近且一般都是在一起创建的。抽象工厂被具体化成不同的工厂来创建类似产品的不同产品。这种模式将类于使用的客户端分离以便通过工厂来创建。这样,各类产品可以方便的变化而不需要改变使用者的结构。

Role
This 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的创建产品的操作;

抽象产品接口IProductA,IProductB,含有某类产品自有的操作;

具体产品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/p/1675216.html