JAVA-抽象工厂模式
来源:互联网 发布:大数据变革时代论文 编辑:程序博客网 时间:2024/05/16 17:04
抽象工厂模式是设计模式工厂设计模式较为复杂的一种设计模式,英文概念翻译是:为创建一组相关或相互依赖的对象提供一个借口,无需指定它们的具体类”。抽象工厂模式通常是用于创一族产品,并且这族产品分不同的等级;不同的具体工厂类生产不同等级的一族产品。抽象工厂模式也有四种角色:抽象工厂、具体工厂、抽象产品和具体产品.典型的面向接口/抽象类的编程模式。
抽象工厂模式的类图:
代码示例:
工程示例:
1.创建抽象工厂接口:
package test.edu.absfactory;public interface IAbstractFactory {public IRestaurant restaurantFactory();public ISpecialDishes specialDisheFactory();public IWine wineFactory();}
package test.edu.absfactory;public interface IRestaurant {public void restaurantName();}
package test.edu.absfactory;public interface ISpecialDishes {public void eatSpecialDishes ();}
package test.edu.absfactory;public interface IWine {public void drinkWine ();}3.创建具体产品类:
中国特色餐馆产品族实现类:
package test.edu.impl;import test.edu.absfactory.IRestaurant;public class ChineseRestaurant implements IRestaurant {@Overridepublic void restaurantName() {System.out.println("这是中国餐馆");}}
package test.edu.impl;import test.edu.absfactory.ISpecialDishes;public class ChineseDishes implements ISpecialDishes {@Overridepublic void eatSpecialDishes() {System.out.println("吃特色菜满汉全席");}}
package test.edu.impl;import test.edu.absfactory.IWine;public class ChineseWLY implements IWine {@Overridepublic void drinkWine() {System.out.println("喝五粮液");}}
韩国特色餐馆产品族实现类:
package test.edu.impl;import test.edu.absfactory.IRestaurant;public class KORRestaurant implements IRestaurant {@Overridepublic void restaurantName() {System.out.println("这是韩国菜馆");}}
package test.edu.impl;import test.edu.absfactory.ISpecialDishes;public class KORDishes implements ISpecialDishes {@Overridepublic void eatSpecialDishes() {System.out.println("吃韩国烤肉");}}
package test.edu.impl;import test.edu.absfactory.IWine;public class KORWine implements IWine {@Overridepublic void drinkWine() {System.out.println("喝韩国真露");}}
4,具体的抽象工厂类:实现抽象工厂的同时,各个产品的具体工厂也做具体实现
中国餐馆的工厂类
package test.edu.impl;import test.edu.absfactory.IAbstractFactory;import test.edu.absfactory.IRestaurant;import test.edu.absfactory.ISpecialDishes;import test.edu.absfactory.IWine;public class ChineseDeliciousFoodFactory implements IAbstractFactory {public IRestaurant restaurantFactory(){return new ChineseRestaurant();}public ISpecialDishes specialDisheFactory(){return new ChineseDishes();}public IWine wineFactory(){return new ChineseWLY();}}
韩国餐馆的工厂类
package test.edu.impl;import test.edu.absfactory.IAbstractFactory;import test.edu.absfactory.IRestaurant;import test.edu.absfactory.ISpecialDishes;import test.edu.absfactory.IWine;public class KORDeliciousFoodFactory implements IAbstractFactory {public IRestaurant restaurantFactory(){return new KORRestaurant();}public ISpecialDishes specialDisheFactory(){return new KORDishes();}public IWine wineFactory(){return new KORWine();}}
测试客户端:
去中国菜馆
package test.edu.client;import test.edu.absfactory.IAbstractFactory;import test.edu.impl.ChineseDeliciousFoodFactory;public class AbsFactoryClient {/** * @param args */public static void main(String[] args) {IAbstractFactory dff = new ChineseDeliciousFoodFactory();dff.restaurantFactory().restaurantName();dff.specialDisheFactory().eatSpecialDishes();dff.wineFactory().drinkWine();}}结果:
这是中国餐馆吃特色菜满汉全席喝五粮液
IAbstractFactory dff = new ChineseDeliciousFoodFactory();为:
IAbstractFactory dff = new KORDeliciousFoodFactory();
package test.edu.client;import test.edu.absfactory.IAbstractFactory;import test.edu.impl.ChineseDeliciousFoodFactory;import test.edu.impl.KORDeliciousFoodFactory;public class AbsFactoryClient {/** * @param args */public static void main(String[] args) {IAbstractFactory dff = new KORDeliciousFoodFactory();dff.restaurantFactory().restaurantName();dff.specialDisheFactory().eatSpecialDishes();dff.wineFactory().drinkWine();}}结果:
这是韩国菜馆吃韩国烤肉喝韩国真露
当然在实际应用如何切换不同的工厂类,可以在配置文件配置,也可以利用不同的条件调用不同的抽象工厂实现类。
0 0
- java抽象工厂模式
- java抽象工厂模式
- java 抽象工厂模式
- Java抽象工厂模式
- java 抽象工厂模式
- Java 抽象工厂模式
- java 抽象工厂模式
- Java 抽象工厂模式
- JAVA抽象工厂模式
- java抽象工厂模式
- 【Java】抽象工厂模式
- JAVA-抽象工厂模式
- Java 抽象工厂模式
- java 抽象工厂模式
- JAVA抽象工厂模式
- Java抽象工厂模式
- java 抽象工厂模式
- java 抽象工厂模式
- 晓月_【C#异步服务器】第一天 服务器源代码
- 简单工厂模式
- TRS WCM 几处突破点
- Notification(二)
- Android 里面获取屏幕的高度,宽度
- JAVA-抽象工厂模式
- 课时4第二章:运算符
- Android样式开发——style篇
- OC_消息机制
- oracle聚合函数全记录
- thinkphp 3.2 =》0625-8_分页效果
- 为什么memcpy_s比memcpy安全
- UITableView以及UITableViewCell设置的时候容易出现的错误
- UIViewController