1 抽象工厂(Abstract Factory)模式
1.类图
?
2.类
?
?? Creator
package com.javapatterns.abstractfactory;public interface Creator {public ProductA factoryA();public ProductB factoryB();}
?? ProductA
package com.javapatterns.abstractfactory;public interface ProductA {}
??? ProductB
package com.javapatterns.abstractfactory;public interface ProductB {}?
package com.javapatterns.abstractfactory;public class ConcreteCreatorA implements Creator {public ProductA factoryA() {// TODO Auto-generated method stubreturn new ConcreteProductA1();}public ProductB factoryB() {// TODO Auto-generated method stubreturn new ConcreteProductB1();}}?
package com.javapatterns.abstractfactory;public class ConcreteCreatorB implements Creator {public ProductA factoryA() {// TODO Auto-generated method stubreturn new ConcreteProductA2();}public ProductB factoryB() {// TODO Auto-generated method stubreturn new ConcreteProductB2();}}?
package com.javapatterns.abstractfactory;public class ConcreteProductA1 implements ProductA{}?
package com.javapatterns.abstractfactory;public class ConcreteProductA2 implements ProductA{}
?
package com.javapatterns.abstractfactory;public class ConcreteProductB1 implements ProductB{}
?
?
package com.javapatterns.abstractfactory;public class ConcreteProductB2 implements ProductB{}
测试代码:
package com.javapatterns.abstractfactory;import util.S;public class test {/** * @param args */public static void main(String[] args) {ConcreteCreatorA ConcreteCreatorA = new ConcreteCreatorA();ProductA productA1 = ConcreteCreatorA.factoryA();ProductB productB1 = ConcreteCreatorA.factoryB();ConcreteCreatorB ConcreteCreatorB = new ConcreteCreatorB();ProductA productA2 = ConcreteCreatorB.factoryA();ProductB productB2 = ConcreteCreatorB.factoryB();S.s(productA1);S.s(productA2);S.s(productB1);S.s(productB2);}}
?z