一、是什么
1. 定义
工厂方法模式: 由子类决定要创建的具体类是哪一个
抽象工厂: 允许客户创建对象的家族,而无需指定他们的具体类
2. 理解
从定义中可以看出, 抽象工厂包含了工厂方法,区别是抽象工厂创建的是家族
3. 好处:
封装具体对象的创建,促进松耦合
4. 设计原则
依赖倒置原则--依赖抽象,不依赖具体类
二、示例
工厂方法
1. 工厂相关类
/** * 工厂 */ public abstract class Factory { public abstract Milk makeMilk(); } /** * A 工厂 */ public class FactoryA extends Factory { @Override public Milk makeMilk() { return new MilkA(); } } /** * B 工厂 */ public class FactoryB extends Factory { @Override public Milk makeMilk() { return new MilkB(); } }
2. 牛奶类
/** * 产品抽象类 */ public abstract class Milk { public abstract void print(); } /** * 产品A */ public class MilkA extends Milk { @Override public void print() { System.out.println("我是牛奶A"); } } /** * 产品B */ public class MilkB extends Milk { @Override public void print() { System.out.println("我是牛奶B"); } }
3. 测试
/** * 测试类 */ public class Main { public static void main(String[] args) { Factory factoryA = new FactoryA(); Factory factoryB = new FactoryB(); Milk milkA = factoryA.makeMilk(); Milk milkB = factoryB.makeMilk(); milkA.print(); milkB.print(); } }
抽象工厂
1. 代码背景,分为南京和杭州两家工厂,他们各自生产自己的产品
2. 芝士
/** * 芝士 */ public abstract class Cheese { public String address = ""; public Cheese(String address) { this.address = address; } public abstract void print(); } /** * 南京生产的芝士 */ public class NanJingCheese extends Cheese { public NanJingCheese(String address) { super(address); } @Override public void print() { System.out.println(address + "的芝士"); } } /** * 杭州芝士 */ public class HangZhouCheese extends Cheese { public HangZhouCheese(String address) { super(address); } @Override public void print() { System.out.println(address + "的芝士"); } }
3. 牛奶
/** * 产品抽象类 */ public abstract class Milk { public String address = ""; public Milk(String address) { this.address = address; } public abstract void print(); } /** * 南京的牛奶 */ public class NanJingMilk extends Milk { public NanJingMilk(String address) { super(address); } @Override public void print() { System.out.println(address + "的牛奶"); } } /** * 杭州牛奶 */ public class HangZhouMilk extends Milk { public HangZhouMilk(String address) { super(address); } @Override public void print() { System.out.println(this.address + "的牛奶"); } }
3. 工厂: 生产一组产品
/** * 抽象工厂类 */ public abstract class Factory { public abstract Milk makeMilk(); public abstract Cheese makeCheese(); } /** * 南京工厂 */ public class NanJingFactory extends Factory { @Override public Milk makeMilk() { return new NanJingMilk("南京江宁"); } @Override public Cheese makeCheese() { return new NanJingCheese("南京江宁"); } } /** * 杭州工厂 */ public class HangZhouFactory extends Factory { @Override public Milk makeMilk() { return new HangZhouMilk("杭州萧山"); } @Override public Cheese makeCheese() { return new HangZhouCheese("杭州萧山"); } }
4. 测试
/** * 测试类 */ public class Main { public static void main(String[] args) { Factory hangZhouFactory = new HangZhouFactory(); Cheese hangZhouCheese = hangZhouFactory.makeCheese(); Milk hangZhouMilk = hangZhouFactory.makeMilk(); hangZhouCheese.print(); hangZhouMilk.print(); Factory nanJingFactory = new NanJingFactory(); Cheese nanJingCheese = nanJingFactory.makeCheese(); Milk nanJingMilk = nanJingFactory.makeMilk(); nanJingCheese.print(); nanJingMilk.print(); } }
三、总结
1. 所有工厂的目的都是封装对象的创建
2.工厂方法使用的是继承,把对象的创建委托给子类,子类来实现工厂方法来创建对象
3. 使用工厂模式的好处: 减少应用程序和具体类之间的依赖促进松耦合