设计模式之抽象工厂模式
抽象工厂模式定义:Provide an interface for creating families of related or dependent objects without specifying their concrete classes.(为创建一组相关或相互依赖的对象提供一个接口,而且无需指定它们的具体类型)
抽象工厂模式是工厂方法模式的升级版本,在有多个业务品种、业务分类时,通过抽象工厂模式产生需要的对象是一种非常好的解决方式。
通用类图
通用源码类图
抽象产品类A
public abstract class AbstractProductA {
//共有方法
public void shareMethod(){
}
//相同方法不同实现
public abstract void doSomething();
}
具体产品类A1
public class ProductA1 extends AbstractProductA{
public void doSomething(){
System.out.println("产品A1");
}
}
具体产品类A2
public class ProductA2 extends AbstractProductA{
public void doSomething(){
System.out.println("产品A2");
}
}
产品类B同A
抽象工厂类
public abstract class AbstractCreator {
public abstract AbstractProductA createProductA();
public abstract AbstractProductB createProductB();
}
具体工厂类1
public class Creator1 extends AbstractCreator{
public AbstractProductA createProductA(){
return new ProductA1();
}
public AbstractProductB createProductB(){
return new ProductB1();
}
}
具体工厂类2
public class Creator2 extends AbstractCreator{
public AbstractProductA createProductA()
{
return new ProductA2();
}
public AbstractProductB createProductB()
{
return new ProductB2();
}
}
场景类
public class Client {
public static void main(String[] args) {
// TODO Auto-generated method stub
AbstractCreator creator1=new Creator1();
AbstractCreator creator2=new Creator2();
AbstractProductA a1=creator1.createProductA();
AbstractProductA a2=creator2.createProductA();
AbstractProductB b1=creator1.createProductB();
AbstractProductB b2=creator2.createProductB();
}
}
抽象工厂模式优点
1.封装性,每个产品类的实现不是高层模块需要关心的,高层模块只需要关心接口、抽象,工厂类则关心对象是如何创建出来的,只要知道工厂类是谁,就能创造一个需要的对象,省时省力;
2.产品族内的约束为非公开状态。
抽象工厂模式缺点
抽象工厂模式最大的缺点就是产品族的扩展非常困难,以通用代码为例,如要增加产品C,抽象类AbstractCreator需要增加一个方法createProductC,然后两个类的实现需要改变,这就违反了开闭原则。
抽象工厂模式使用场景
一个对象族(或者是一组没有任何关系的对象)都有相同的约束,则可以使用抽象工厂模式。
注意事项
抽象工厂模式的产品族扩展比较困难,但是一定要清楚,是产品族扩展困难,而不是产品等级。在该模式下产品等级是非常容易扩展的,增加一个产品等级,只要增加一个工厂类负责增加出来的产品生产任务即可。也就是说横向扩展容易,纵向扩展难。
例子:女娲造人
人类接口
public interface Human {
public void getColor();
public void talk();
public void getSex();
}
白色人抽象类
public abstract class AbstractWhiteHuman implements Human{
public void getColor(){
System.out.println("白人");
}
public void talk(){
System.out.println("白人说英语");
}
}
黄色人抽象类,黑色人抽象类,同上
白色男人类
public class MaleWhiteHuman extends AbstractWhiteHuman{
public void getSex(){
System.out.println("白人汉子");
}
}
白色女人类
public class FemalWhiteHuman extends AbstractWhiteHuman{
public void getSex(){
System.out.println("白人妹子");
}
}
黑色男人类,黑色女人类,黄色男人类,黄色女人类,同上
工厂类接口
public interface HumanFactory {
public Human createYellowHuman();
public Human createWhiteHuman();
public Human createBlackHuman();
}
女性工厂类
public class FemalFactory implements HumanFactory{
public Human createBlackHuman(){
return new FemalBlackHuman();
}
public Human createWhiteHuman(){
return new FemalWhiteHuman();
}
public Human createYellowHuman(){
return new FemaleYellowHuman();
}
}
男性工厂类
public class MaleFactory implements HumanFactory{
public Human createBlackHuman(){
return new MaleBlackHuman();
}
public Human createWhiteHuman(){
return new MaleWhiteHuman();
}
public Human createYellowHuman(){
return new MaleYellowHuman();
}
}
女娲类
public class NvWa {
public static void main(String[] args) {
// TODO Auto-generated method stub
HumanFactory maleHumanFactory=new MaleFactory();
HumanFactory femaleHumanFactory=new FemalFactory();
Human maleYellowMan=maleHumanFactory.createYellowHuman();
Human femaleYellowMan=femaleHumanFactory.createYellowHuman();
femaleYellowMan.getColor();
femaleYellowMan.talk();
femaleYellowMan.getSex();
maleYellowMan.getColor();
maleYellowMan.talk();
maleYellowMan.getSex();
}
}