标签:c style class blog code java
抽象工厂模式:提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们的具体类。
简单描述:有多种抽象产品,并且每种抽象产品都有多个具体产品。一个抽象工厂,提供多个具体工厂,每个工厂则提供不同种类的具体产品。
大话设计模式中的截图:
例子代码:
AbstractProductA类:
1 package com.longsheng.abstractfactory; 2 3 public abstract class AbstractProductA { 4 5 public abstract void methodA(); 6 7 public abstract void methodB(); 8 9 }
具体产品ProductA1类:
1 package com.longsheng.abstractfactory; 2 3 public class ProductA1 extends AbstractProductA { 4 5 @Override 6 public void methodA() { 7 System.out.println("ProductA1--methodA"); 8 } 9 10 @Override 11 public void methodB() { 12 System.out.println("ProductA1--methodB"); 13 } 14 15 }
具体产品ProductA2类:
1 package com.longsheng.abstractfactory; 2 3 public class ProductA2 extends AbstractProductA { 4 5 @Override 6 public void methodA() { 7 System.out.println("ProductA2--methodA"); 8 } 9 10 @Override 11 public void methodB() { 12 System.out.println("ProductA2--methodB"); 13 } 14 15 }
AbstractProductB类:
1 package com.longsheng.abstractfactory; 2 3 public abstract class AbstractProductB { 4 5 public abstract void methodX(); 6 7 public abstract void methodY(); 8 }
具体产品ProductB1类:
1 package com.longsheng.abstractfactory; 2 3 public class ProductB1 extends AbstractProductB { 4 5 @Override 6 public void methodX() { 7 System.out.println("ProductB1--methodX"); 8 } 9 10 @Override 11 public void methodY() { 12 System.out.println("ProductB1--methodY"); 13 } 14 15 }
具体产品ProductB2类:
1 package com.longsheng.abstractfactory; 2 3 public class ProductB2 extends AbstractProductB { 4 5 @Override 6 public void methodX() { 7 System.out.println("ProductB2--methodX"); 8 } 9 10 @Override 11 public void methodY() { 12 System.out.println("ProductB2--methodY"); 13 } 14 15 }
抽象工厂AbstractFactory类:
1 package com.longsheng.abstractfactory; 2 3 public abstract class AbstractFactory { 4 5 public abstract AbstractProductA createProductA(); 6 7 public abstract AbstractProductB createProductB(); 8 9 }
具体工厂ConcreteFactory1类:
1 package com.longsheng.abstractfactory; 2 3 public class ConcreteFactory1 extends AbstractFactory { 4 5 @Override 6 public AbstractProductA createProductA() { 7 return new ProductA1(); 8 } 9 10 @Override 11 public AbstractProductB createProductB() { 12 return new ProductB1(); 13 } 14 15 }
具体工厂ConcreteFactory2类:
1 package com.longsheng.abstractfactory; 2 3 public class ConcreteFactory2 extends AbstractFactory { 4 5 @Override 6 public AbstractProductA createProductA() { 7 return new ProductA2(); 8 } 9 10 @Override 11 public AbstractProductB createProductB() { 12 return new ProductB2(); 13 } 14 15 }
客户端类:
1 package com.longsheng.abstractfactory; 2 3 public class Client { 4 5 public static void main(String[] args) { 6 //AbstractFactory mAbstractFactory = new ConcreteFactory1(); 7 AbstractFactory mAbstractFactory = new ConcreteFactory2(); 8 AbstractProductA productA = mAbstractFactory.createProductA(); 9 AbstractProductB productB = mAbstractFactory.createProductB(); 10 11 productA.methodA(); 12 productA.methodB(); 13 14 productB.methodX(); 15 productB.methodY(); 16 } 17 18 }
运行结果:
1 ProductA2--methodA 2 ProductA2--methodB 3 ProductB2--methodX 4 ProductB2--methodY
抽象工厂的好处:
(1)便于交换产品系列,如更改产品成ProductA1/ProductB1,只需要更改AbstractFactory mAbstractFactory = new ConcreteFactory1();改变这条创建具体工厂的语句即可
(2)创建实例的过程与客户端分离,客户端通过抽象接口操纵实例,产品的具体类名也被具体工厂的实现分离,不会出现在客户代码中
大话设计模式_抽象工厂模式(Java代码),布布扣,bubuko.com
标签:c style class blog code java
原文地址:http://www.cnblogs.com/creasylai19/p/3775625.html