标签:ima turn span 设计模式 inter 创建 ack desc tabs
1.抽象工厂(AbstractFactory)模式的定义
是一种为访问类提供一个创建一组相关或相互依赖对象的接口,且访问类无须指定所要产品的具体类就能得到同族的不同等级的产品的模式结构。
2.使用抽象工厂模式一般要满足以下条件
3.抽象工厂模式除了具有工厂方法模式的优点外,其他主要优点如下
其缺点是:当产品族中需要增加一个新的产品时,所有的工厂类都需要进行修改。
4.工厂方法模式结构图
5.抽象工厂模式的实现,以生产电器,不同产品和不同产商为例
package com.lw.designpattern.abstractfactory; /** * @Classname AirConditionerFactory * @Description 空调工厂 * @Author lw * @Date 2019-12-24 10:58 */ public interface AirConditionerFactory { public void airConditionerInfo(); }
package com.lw.designpattern.abstractfactory; /** * @Classname FridgeFactory * @Description 冰箱工厂 * @Author lw * @Date 2019-12-24 10:59 */ public interface FridgeFactory { public void fridgeInfo(); }
package com.lw.designpattern.abstractfactory; /** * @Classname ElectricalAbstractFactory * @Description 电器抽象工厂 * @Author lw * @Date 2019-12-24 11:05 */ public interface ElectricalAbstractFactory { public FridgeFactory createFridge(); public AirConditionerFactory createAirConditioner(); }
package com.lw.designpattern.abstractfactory.media; import com.lw.designpattern.abstractfactory.AirConditionerFactory; import com.lw.designpattern.abstractfactory.ElectricalAbstractFactory; import com.lw.designpattern.abstractfactory.FridgeFactory; /** * @Classname MediaFactory * @Description 美的工厂 * @Author lw * @Date 2019-12-24 11:06 */ public class MediaFactory implements ElectricalAbstractFactory { @Override public FridgeFactory createFridge() { System.out.println("生产美的冰箱。。。。。。"); return new MediaFridge(); } @Override public AirConditionerFactory createAirConditioner() { System.out.println("生产美的空调。。。。。。"); return new MediaAirConditioner(); } }
package com.lw.designpattern.abstractfactory.media; import com.lw.designpattern.abstractfactory.AirConditionerFactory; /** * @Classname MediaAirConditioner * @Description 美的空调信息 * @Author lw * @Date 2019-12-24 11:02 */ public class MediaAirConditioner implements AirConditionerFactory { @Override public void airConditionerInfo() { System.out.println("美的空调牛逼。。。。。。"); } }
package com.lw.designpattern.abstractfactory.media; import com.lw.designpattern.abstractfactory.FridgeFactory; /** * @Classname MediaFridge * @Description 美的冰箱信息 * @Author lw * @Date 2019-12-24 11:03 */ public class MediaFridge implements FridgeFactory { @Override public void fridgeInfo() { System.out.println("美的冰箱牛逼。。。。。。"); } }
package com.lw.designpattern.abstractfactory.haier; import com.lw.designpattern.abstractfactory.AirConditionerFactory; import com.lw.designpattern.abstractfactory.ElectricalAbstractFactory; import com.lw.designpattern.abstractfactory.FridgeFactory; /** * @Classname HaierFactory * @Description 海尔工厂 * @Author lw * @Date 2019-12-24 11:07 */ public class HaierFactory implements ElectricalAbstractFactory { @Override public FridgeFactory createFridge() { System.out.println("生产海尔冰箱。。。。。。"); return new HaierFridge(); } @Override public AirConditionerFactory createAirConditioner() { System.out.println("生产海尔空调。。。。。。"); return new HaierAirConditioner(); } }
package com.lw.designpattern.abstractfactory.haier; import com.lw.designpattern.abstractfactory.FridgeFactory; /** * @Classname MediaFridge * @Description 海尔冰箱信息 * @Author lw * @Date 2019-12-24 11:03 */ public class HaierFridge implements FridgeFactory { @Override public void fridgeInfo() { System.out.println("海尔冰箱牛逼。。。。。。"); } }
package com.lw.designpattern.abstractfactory.haier; import com.lw.designpattern.abstractfactory.AirConditionerFactory; /** * @Classname MediaAirConditioner * @Description 海尔空调信息 * @Author lw * @Date 2019-12-24 11:02 */ public class HaierAirConditioner implements AirConditionerFactory { @Override public void airConditionerInfo() { System.out.println("海尔空调牛逼。。。。。。"); } }
/** * 工厂设计模式-抽象工厂模式 */ @Test public void testAbstractFactory(){ // 海尔 ElectricalAbstractFactory haierFactory = new HaierFactory(); FridgeFactory haierFridge = haierFactory.createFridge(); haierFridge.fridgeInfo(); AirConditionerFactory haierAirConditioner = haierFactory.createAirConditioner(); haierAirConditioner.airConditionerInfo(); System.out.println("------------------------"); // 美的 ElectricalAbstractFactory mediaFactory = new MediaFactory(); FridgeFactory mediaFridge = mediaFactory.createFridge(); mediaFridge.fridgeInfo(); AirConditionerFactory mediaAirConditioner = mediaFactory.createAirConditioner(); mediaAirConditioner.airConditionerInfo(); }
打印结果
6.抽象工厂模式通常适用于以下场景
标签:ima turn span 设计模式 inter 创建 ack desc tabs
原文地址:https://www.cnblogs.com/lwcode6/p/12090907.html