23种设计模式之--抽象工厂模式
女娲的失误:
工厂方法模式中讲了女娲造人的故事。人是造出来了,世界也热闹了,可以低头一看,都是清一色的类型,
缺少关爱、仇恨、喜怒哀乐等情绪,人类的生命太平淡了,女娲一想,猛然一拍脑袋,忘记给人类定义性别了,那怎么办呢?
八卦炉分为两个,一个用于造女人,一个用于造男人,有了性别,就有了爱情,也就有了丰富的人生。
public interface Human { void getColor(); void talk(); void getSex(); } public abstract class AbstractBlackHuman implements Human { public void getColor() { System.out.println( "black" ); } public void talk() { System.out.println( "black human talking." ); } } public abstract class AbstractWhiteHuman implements Human { public void getColor() { System.out.println( "white" ); } public void talk() { System.out.println( "white human talking." ); } } public abstract class AbstractYellowHuman implements Human { public void getColor() { System.out.println( "yellow" ); } public void talk() { System.out.println( "yellow human talking." ); } } public class MaleBlackHuman extends AbstractBlackHuman { public void getSex() { System.out.println( "male" ); } } public class MaleWhiteHuman extends AbstractWhiteHuman { public void getSex() { System.out.println( "male" ); } } public class MaleYellowHuman extends AbstractYellowHuman { public void getSex() { System.out.println( "male" ); } } public class FemaleBlackHuman extends AbstractBlackHuman { public void getSex() { System.out.println( "female" ); } } public class FemaleWhiteHuman extends AbstractWhiteHuman { public void getSex() { System.out.println( "female" ); } } public class FemaleYellowHuman extends AbstractYellowHuman { public void getSex() { System.out.println( "female" ); } }
public interface HumanFactory { Human createBlackHuman(); Human createWhiteHuman(); Human createYellowHuman(); } 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 FemaleFactory implements HumanFactory { public Human createBlackHuman() { return new FemaleBlackHuman(); } public Human createWhiteHuman() { return new FemaleWhiteHuman(); } public Human createYellowHuman() { return new FemaleYellowHuman(); } }
public class NvWa { public static void main( String[] args ) { HumanFactory maleFactory = new MaleFactory(); HumanFactory femaleFactory = new FemaleFactory(); Human maleYellowHuman = maleFactory.createYellowHuman(); Human femaleYellowHuman = femaleFactory.createYellowHuman(); maleYellowHuman.getColor(); maleYellowHuman.talk(); maleYellowHuman.getSex(); femaleYellowHuman.getColor(); femaleYellowHuman.talk(); femaleYellowHuman.getSex(); } }
定义:
Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
为创建一组相关或相互依赖的对象提供一个接口。
注意:有M个产品等级就应该有M个实现工厂类,在每个实现工厂中,实现不同产品族的生产任务。
优点:
1. 封装性:每个产品的实现类不需要关心高层模块,只要知道工厂类是谁,就可以创建需要的产品。
2. 产品族内的约束为非公开状态。
缺点:
1.产品族扩展非常困难。如果要增加一个产品c,抽象类AbstractCreator要增加一个方法createProductC(),然后两个实现类都要修改,这就违反了开闭原则。
使用场景:
一个对象族(或是没有任何关系的对象)都有相同的约束,则可以使用抽象工厂模式。例如,一个文本编辑器在window下和在linux下功能相同,但是实现不同。也就是他们都有共同的约束条件:操作系统类型。