首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

一 抽象工厂(Abstract Factory)模式

2012-11-03 
1 抽象工厂(Abstract Factory)模式1.类图?2.类??? Creatorpackage com.javapatterns.abstractfactorypubl

1 抽象工厂(Abstract Factory)模式

1.类图


一 抽象工厂(Abstract Factory)模式

?

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

热点排行