Java抽象工厂模式
Java抽象工厂模式
- 基本定义
抽象工厂模式是所有形态的工厂模式中最为抽
象和最其一般性的。抽象工厂模式可以向客户端
提供一个接口,使得客户端在不必指定产品的具
体类型的情况下,能够创建多个产品族的产品对
象。 - 编写具体代码以及测试类
package abstractFactory; public abstract class Apple implements Fruit { public abstract void get(); } package abstractFactory; public abstract class Banana implements Fruit { public abstract void get(); } package abstractFactory; public interface Fruit { public void get(); } package abstractFactory; public interface FruitFactory { //实例化Apple public Fruit getApple(); //实例化Banana public Fruit getBanana(); } package abstractFactory; public class InnerApple extends Apple { @Override public void get() { System.out.println("长在室内的苹果"); } } package abstractFactory; public class InnerBanana extends Banana { @Override public void get() { System.out.println("长在室内的香蕉"); } } package abstractFactory; public class InnerFruitFactory implements FruitFactory { @Override public Fruit getApple() { return new InnerApple(); } @Override public Fruit getBanana() { return new InnerBanana(); } } package abstractFactory; public class NorthApple extends Apple { @Override public void get() { System.out.println("长在北方的苹果"); } } package abstractFactory; public class NorthBanana extends Banana { @Override public void get() { System.out.println("长在北方的香蕉"); } } package abstractFactory; public class NorthFruitFactory implements FruitFactory { @Override public Fruit getApple() { return new NorthApple(); } @Override public Fruit getBanana() { return new NorthBanana(); } } package abstractFactory; public class SouthApple extends Apple { @Override public void get() { System.out.println("长在南方的苹果"); } } package abstractFactory; public class SouthBanana extends Banana { @Override public void get() { System.out.println("长在南方的香蕉"); } } package abstractFactory; public class SouthFruitFactory implements FruitFactory { @Override public Fruit getApple() { return new SouthApple(); } @Override public Fruit getBanana() { return new SouthBanana(); } } //测试类 package abstractFactory; public class MainClass { public static void main(String[] args) { FruitFactory ff = new NorthFruitFactory(); Fruit apple = ff.getApple(); apple.get(); Fruit banana = ff.getBanana(); banana.get(); System.out.println("~~~~~~~~~~~~~~~~~~~~"); FruitFactory bb = new SouthFruitFactory(); Fruit apple2 = bb.getApple(); apple2.get(); Fruit banana2 = bb.getBanana(); banana2.get(); System.out.println("~~~~~~~~~~~~~~~~~~~~"); //比如要增加室内innerApple,InnerBanana FruitFactory cc = new InnerFruitFactory(); Fruit apple3 = cc.getApple(); apple3.get(); Fruit banana3 = cc.getBanana(); banana3.get(); } }
-
模式中包含的角色及其职责
1.抽象工厂(Creator)角色
抽象工厂模式的核心,包含对多个产品结构的声明,任何工厂类都必须实现这个接口。(FruitFactory)2.具体工厂( Concrete Creator)角色
具体工厂类是抽象工厂的一个实现,负责实例化某个产品族中的产品对象。(InnerFruitFactory,NorthFruitFactory,SouthFruitFactory)3.抽象(Product)角色
抽象模式所创建的所有对象的父类,它负责描述所有实例所共有的公共接口。(Fruit)4.具体产品(Concrete Product)角色
抽象模式所创建的具体实例对象(NorthApple,NorthBanana,SouthApple,SouthBanana,InnerBanana,InnerFruitFactory)
总结:抽象工厂中方法对应产品结构,具体工厂对应产品族。 - 具体和前面两节模式相比较图形,我整理出来如下图所示: