适用于产品组装。工厂方法是生产不同牌子的某一单一产品如cpu。抽象工厂是生产有多种配件,且需要自由组装的产品如电脑。
思路:
要生产的产品是一个多组件的产品,每一个组件有一个接口,其实现类分别生产不同牌子的组件产品。
组装产品的工作交给工厂。因为可以组装多套产品,所以有一个抽象工厂,每个实现为一个组装方案。
客户端需要有各种子组件的抽象类引用,和抽象工厂类的引用。
抽象产品类 及其实现
public interface IMother {
public void printName();
}
public interface IFather {
public void printName();
}
//实现
//中国妈妈
public class ChineseMother implements IMother{
private String name;
public ChineseMother(String name) {
this.name = name;
System.out.println("create a cn mother.");
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
@Override
public void printName() {
System.out.println(this.getClass().getName() + ":" + this.name);
}
}
//美国妈妈
public class AmericanMother implements IMother{
private String name;
public AmericanMother(String name) {
this.name = name;
System.out.println("create a us mother.");
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
@Override
public void printName() {
System.out.println(this.getClass().getName() + ":" + this.name);
}
}
//中国爸爸
public class ChineseFather implements IFather{
private String name;
public ChineseFather(String name) {
this.name = name;
System.out.println("create a cn father.");
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
@Override
public void printName() {
System.out.println(this.getClass().getName() + ":" + this.name);
}
}
//美国爸爸
public class AmericanFather implements IFather{
private String name;
public AmericanFather(String name) {
this.name = name;
System.out.println("create a us father.");
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
@Override
public void printName() {
System.out.println(this.getClass().getName() + ":" + this.name);
}
}
家庭工厂(组装工厂)
抽象工厂类 定义组装方式
public interface IFamilyFactory {
public IFather createFather(String name);
public IMother createMother(String name);
}
实现类 选择具体子组件
public class ChineseFamilyFactory implements IFamilyFactory{
@Override
public IFather createFather(String name) {
return new ChineseFather(name);
}
@Override
public IMother createMother(String name) {
return new ChineseMother(name);
}
}
public class AmericanFamilyFactory implements IFamilyFactory{
@Override
public IFather createFather(String name) {
return new AmericanFather(name);
}
@Override
public IMother createMother(String name) {
return new AmericanMother(name);
}
}
客户端
public class Main {
public static void main(String[] args) {
IFamilyFactory cnFamilyFactory = new ChineseFamilyFactory();
IFamilyFactory usFamilyFactory = new AmericanFamilyFactory();
IFather cnFather = cnFamilyFactory.createFather("cn father-test");
IMother cnMother = cnFamilyFactory.createMother("cn mother-test");
IFather usFather = usFamilyFactory.createFather("us father-test");
IMother usMother = usFamilyFactory.createMother("us mother-test");
cnFather.printName();
cnMother.printName();
usFather.printName();
usMother.printName();
}
}
- 优点:
- 当一个产品族中的多个对象被设计成一起工作时,它能保证客户端始终只使用同一个产品族中的对象。
- 增加新的具体工厂和产品族很方便,无须修改已有系统,符合“开闭原则”