Design Pattern ——Factory Method&Abstract Factory

今天开始复习设计模式。设计模式相关的资料有很多,概念性的东西就画个图就可以了。把关注点放在例子上,设计模式还是要使用中才有感受。

从Factory Method&Abstract Factory开始。

 一、Factory Method

工厂方法模式的用意是定义一个创建产品对象的工厂接口,将实际创建工作推迟到子类中。

我们的例子如下:

我们项目中有一个代码提交记录releasenote,我们需要将这个releasenote分成csv/xls/xml格式分别导出

1 public interface ReleasenoteFactory {
2 
3     public Releasenote exportReleasenote();
4     
5 }
1 public interface Releasenote {
2 
3     public void exportFile();
4 
5 }
1 public class CSVReleasenoteFactory implements ReleasenoteFactory {
2 
3     @Override
4     public Releasenote exportReleasenote() {
5         // TODO Auto-generated method stub
6         return new CSVReleasenote();
7     }
8 
9 }
 1 public class CSVReleasenote implements Releasenote {
 2     
 3     @Override
 4     public void exportFile() {
 5         csvOperation();
 6     }
 7 
 8     public void csvOperation(){
 9         System.out.println("CSV");
10     }
11     
12 }
1 public class XlsReleasenoteFactory implements ReleasenoteFactory {
2 
3     @Override
4     public Releasenote exportReleasenote() {
5         // TODO Auto-generated method stub
6         return new XlsReleasenote();
7     }
8 
9 }
 1 public class XlsReleasenote implements Releasenote {
 2 
 3     @Override
 4     public void exportFile() {
 5         xlsOperation();
 6     }
 7 
 8     public void xlsOperation(){
 9         System.out.println("xls");
10     }
11 }
1 public class XmlReleasenoteFactory implements ReleasenoteFactory {
2 
3     @Override
4     public Releasenote exportReleasenote() {
5         // TODO Auto-generated method stub
6         return new XmlReleasenote();
7     }
8 
9 }
 1 public class XmlReleasenote implements Releasenote {
 2 
 3     @Override
 4     public void exportFile() {
 5         xmlOperation();
 6     }
 7 
 8     public void xmlOperation(){
 9         System.out.println("xml");
10     }
11 }

客户端的调用如下:

1         ReleasenoteFactory F1 = new CSVReleasenoteFactory();
2         ReleasenoteFactory F2 = new XlsReleasenoteFactory();    
3         ReleasenoteFactory F3 = new XmlReleasenoteFactory();
4         
5         F1.exportReleasenote().exportFile();
6         F2.exportReleasenote().exportFile();
7         F3.exportReleasenote().exportFile();

二、Abstract Factory

      抽象工厂模式是工厂方法模式的升级版本,他用来创建一组相关或者相互依赖的对象。他与工厂方法模式的区别就在于,工厂方法模式针对的是一个产品等级结构;而抽象工厂模式则是针对的多个产品等级结构。在编程中,通常一个产品结构,表现为一个接口或者抽象类,也就是说,工厂方法模式提供的所有产品都是衍生自同一个接口或抽象类,而抽象工厂模式所提供的产品则是衍生自不同的接口或抽象类。

      在抽象工厂模式中,有一个产品族的概念:所谓的产品族,是指位于不同产品等级结构中功能相关联的产品组成的家族。抽象工厂模式所提供的一系列产品就组成一个产品族;而工厂方法提供的一系列产品称为一个等级结构。我们依然拿生产汽车的例子来说明他们之间的区别。

 

抽象工厂模式,就以《java与模式》中的电脑的例子来学习:

我们知道计算机配件的生产是要配套进行的,PC和MAC是完全不同的两条生产过程,CPU RAM也是完全不同

这个例子中产品族有两个,一个是PC,一个是MAC。产品等级结构有两个,一个是RAM,一个是CPU

1 public interface ComputerProducer {
2 
3     Cpu createCpu();
4     Ram createRam();
5 
6 }
 1 public class MacProducer implements ComputerProducer {
 2 
 3     @Override
 4     public Cpu createCpu() {
 5         // TODO Auto-generated method stub
 6         return new MacCpu();
 7     }
 8 
 9     @Override
10     public Ram createRam() {
11         // TODO Auto-generated method stub
12         return new MacRam();
13     }
14 
15 }
 1 public class PcProducer implements ComputerProducer {
 2 
 3     @Override
 4     public Cpu createCpu() {
 5         // TODO Auto-generated method stub
 6         return new PcCpu();
 7     }
 8 
 9     @Override
10     public Ram createRam() {
11         // TODO Auto-generated method stub
12         return new PcRam();
13     }
14 
15 }
1 public interface Cpu {
2 
3     public void cpuOperation();
4     
5 }
1 public class PcCpu implements Cpu {
2 
3     @Override
4     public void cpuOperation() {
5         System.out.println("Pc Cpu processing!");
6         
7     }
8 
9 }
1 public class MacCpu implements Cpu {
2 
3     @Override
4     public void cpuOperation() {
5         System.out.println("Mac cpu processing!");
6     }
7 
8     
9 }
1 public interface Ram {
2 
3     public void ramOperation();
4     
5 }
1 public class PcRam implements Ram {
2 
3     @Override
4     public void ramOperation() {
5         System.out.println("Pc Ram processing!");
6         
7     }
8 
9 }
1 public class MacRam implements Ram {
2 
3     @Override
4     public void ramOperation() {
5         System.out.println("Mac Ram processing!");
6         
7     }
8 
9 }

最后在客户端调用

 1 public class Client {
 2 
 3     public static void main(String[] args){
 4         ComputerProducer pcFactory = new PcProducer();
 5         ComputerProducer macFactory = new MacProducer();
 6         Cpu pcCpu = pcFactory.createCpu();
 7         Ram pcRam = pcFactory.createRam();
 8         Cpu macCpu = macFactory.createCpu();
 9         Ram macRam = macFactory.createRam();
10         pcCpu.cpuOperation();
11         pcRam.ramOperation();
12         macCpu.cpuOperation();
13         macRam.ramOperation();    
14     }
15     
16 }

参考资料:

《Java与模式》

原文地址:https://www.cnblogs.com/xerrard/p/5019700.html