【C#设计模式-抽象工厂模式】

一.抽象工厂模式的定义:

为创建一组相关或相互依赖的对象提供一个接口,而且无需指定他们的具体类。

二.抽象工厂模式的结构:

抽象工厂模式是所有形态的工厂模式中最为抽象和最具一般性的一种形态。抽象工厂模式是指当有多个抽象角色时,使用的一种工厂模式。

抽象工厂模式可以向客户端提供一个接口,使客户端在不必指定产品的具体的情况下,创建多个产品族中的产品对象。

应用场景:一个生产集团,在北京的工厂需要生产A类汽车,A类电视;在上海的工厂需要生产B类汽车,A类电视。而在广州的工厂需要生产C类汽车,C类电视。

我们可以使用抽象工厂,抽象出工厂类,和产品类,然后继承工厂类,生产所需要的具体产品类型,产品继承抽象出来的产品,实现里面的行为方法。

1.我们抽象出Car产品和TV产品:

[csharp] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace ProductEntity  
  7. {  
  8.     /// <summary>  
  9.     /// 抽象汽车产品  
  10.     /// </summary>  
  11.     public abstract class Car  
  12.     {  
  13.         public abstract void Build();  
  14.     }  
  15. }  
[csharp] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace ProductEntity  
  7. {  
  8.     /// <summary>  
  9.     /// 抽象TV产品  
  10.     /// </summary>  
  11.     public abstract class TV  
  12.     {  
  13.         public abstract void work();  
  14.     }  
  15. }   

2.继承自抽象出来的产品类,实现里面的方法,成为具体产品:这里只举例Acar和TVA的,后面的Bcar,Ccar,TVB,TVC,类似。

[csharp] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace ProductEntity  
  7. {  
  8.     public class CarA:Car      
  9.     {  
  10.         public override void Build()  
  11.         {  
  12.             Console.WriteLine("CarA");  
  13.         }  
  14.     }  
  15. }  
[csharp] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace ProductEntity  
  7. {  
  8.     public class TVA : TV  
  9.     {  
  10.         public override void work()  
  11.         {  
  12.             Console.WriteLine("TVA");  
  13.         }  
  14.     }  
  15. }   

3.抽象出工厂类,里面有待实现创建产品的方法:

[csharp] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using ProductEntity;  
  6.   
  7. namespace FactoryMeth  
  8. {  
  9.     /// <summary>  
  10.     /// 抽象生产工厂  
  11.     /// </summary>  
  12.     public abstract class AbstractFactory  
  13.     {  
  14.   
  15.         /// <summary>  
  16.         /// 生产TV产品  
  17.         /// </summary>  
  18.         /// <returns></returns>  
  19.         public abstract TV newTV();  
  20.   
  21.         /// <summary>  
  22.         /// 生产Car类产品  
  23.         /// </summary>  
  24.         /// <returns></returns>  
  25.         public abstract Car newCar();  
  26.     }  
  27. }  

4.创建具体的工厂类,继承自抽象出来的工厂,实现里面创建具体产品的方法。例:后面的B工厂,和C工厂类似实现。

[csharp] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. using ProductEntity;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Text;  
  6.   
  7. namespace FactoryMeth  
  8. {  
  9.     /// <summary>  
  10.     /// A工厂  
  11.     /// </summary>  
  12.     public class AbstractFactoryA:AbstractFactory  
  13.     {  
  14.         /// <summary>  
  15.         /// 生产A品牌电视  
  16.         /// </summary>  
  17.         /// <returns></returns>  
  18.         public override TV newTV()  
  19.         {  
  20.             return new TVA();  
  21.         }  
  22.   
  23.         /// <summary>  
  24.         /// 生产A品牌汽车  
  25.         /// </summary>  
  26.         /// <returns></returns>  
  27.         public override Car newCar()  
  28.         {  
  29.             return new CarA();    
  30.         }   
  31.     }  
  32. }  
[csharp] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. using ProductEntity;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Text;  
  6.   
  7. namespace FactoryMeth  
  8. {  
  9.     /// <summary>  
  10.     /// B工厂生  
  11.     /// </summary>  
  12.     public class AbstractFactoryB:AbstractFactory  
  13.     {  
  14.         /// <summary>  
  15.         /// 生产B品牌电视  
  16.         /// </summary>  
  17.         /// <returns></returns>  
  18.         public override TV newTV()  
  19.         {  
  20.             return new TVB();  
  21.         }  
  22.   
  23.         /// <summary>  
  24.         /// 生产A品牌汽车  
  25.         /// </summary>  
  26.         /// <returns></returns>  
  27.         public override Car newCar()  
  28.         {  
  29.             return new CarA();  
  30.         }  
  31.     }  
  32. }  
[csharp] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using ProductEntity;  
  6. namespace FactoryMeth  
  7. {  
  8.     public class AbstractFactoryC:AbstractFactory  
  9.     {  
  10.         /// <summary>  
  11.         /// 生产C品牌电视  
  12.         /// </summary>  
  13.         /// <returns></returns>  
  14.         public override TV newTV()  
  15.         {  
  16.             return new TVC();  
  17.         }  
  18.   
  19.         /// <summary>  
  20.         /// 生产C品牌汽车  
  21.         /// </summary>  
  22.         /// <returns></returns>  
  23.         public override Car newCar()  
  24.         {  
  25.             return new CarC();  
  26.         }  
  27.     }  
  28. }  


5.调用,根据具体情况进行选择,现在是在哪一个工厂,就创建该工厂:

[csharp] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using FactoryMeth;  
  6. using ProductEntity;  
  7.   
  8. namespace 抽象工厂模式  
  9. {      
  10.     class Program  
  11.     {  
  12.         static void Main(string[] args)  
  13.         {  
  14.             AbstractFactory factorysubA = new AbstractFactoryA();  
  15.             TV t = factorysubA.newTV();  
  16.             Car c = factorysubA.newCar();  
  17.             Console.WriteLine("A工厂生产:");  
  18.             t.work();  
  19.             c.Build();  
  20.   
  21.             AbstractFactory factorysubB = new AbstractFactoryB();  
  22.             t = factorysubB.newTV();  
  23.             c = factorysubB.newCar();  
  24.             Console.WriteLine("B工厂生产:");  
  25.             t.work();  
  26.             c.Build();  
  27.   
  28.             AbstractFactory factorysunC = new AbstractFactoryC();  
  29.             t = factorysunC.newTV();  
  30.             c = factorysunC.newCar();  
  31.             Console.WriteLine("C工厂生产:");  
  32.             t.work();  
  33.             c.Build();  
  34.             Console.Read();  
  35.         }  
  36.     }  
  37. }  


总结:以后如果该集团需要增加新的工厂,制造其他类型的产品,就只需要增加具体工厂类,和产品类,并实现具体产品即可。   

           其实工厂和抽象工厂没有多大区别,只不过是抽象工厂生产的商品是多个而已
           通俗的说,就是抽象工厂模式的具体工厂里面会有多个实例具体对象的方法
           更直观的就是,抽象工厂模式每个工厂一次造多个产品,而工厂模式的每个工厂只造一个产品

原文地址:https://www.cnblogs.com/jjg0519/p/6733076.html