一、概述
1-抽象工厂
提供一个接口,用于创建相关或者依赖对象的家族,而不需要指定具体的实现类
2-设计原则
(1)开闭(2)多用组合,少继承
抽象工厂和工厂方法的区别
(1)工厂通过继承解耦;抽象工厂通过组合解耦
(2)工厂用于创建一个抽象产品,具体的工厂用于创建具体的产品;抽象工厂用于创建一个产品家族的抽象类型
3-使用场合
(1)创建产品家族(2)提供一个产品库,并只想显示器接口而非实现
========抽象食物接口
package SheJiMoShiNaDianShi.AbstractFactory.model;
/**
* Created by hjw on 16/8/28.
* 食物辅助接口,用于实现共性,如打印信息
*/
public interface IFood {
public void printMessage();
}
========抽象食物
package SheJiMoShiNaDianShi.AbstractFactory.model;
/**
* Created by hjw on 16/8/28.
* 抽象的食物对象
*/
public abstract class AbstractBaseFood {
//类别
protected String kind;
//数量
protected int num;
//单价
protected float price;
//总价
public float totalPrice() {
return this.num * this.price;
}
}
========抽象食物鸡翅
package SheJiMoShiNaDianShi.AbstractFactory.model;
/**
* Created by hjw on 16/8/28.
*/
public abstract class ChickenWings extends AbstractBaseFood implements IFood{
@Override
public void printMessage() {
System.out.println("----"+this.kind+"风味鸡翅,\t 单价:"+this.price + ", \t数量:"+this.num+
", \t总计:"+this.totalPrice());
}
}
========抽象食物汉堡
package SheJiMoShiNaDianShi.AbstractFactory.model;
/**
* Created by hjw on 16/8/28.
* 创建不同食物的抽象类
*/
public abstract class Hamburg extends AbstractBaseFood implements IFood{
@Override
public void printMessage() {
System.out.println("----"+this.kind+"风味汉堡,\t 单价:"+this.price + ", \t数量:"+this.num+
", \t总计:"+this.totalPrice());
}
}
========具体食物鸡翅
package SheJiMoShiNaDianShi.AbstractFactory.model.kfc;
import SheJiMoShiNaDianShi.AbstractFactory.model.ChickenWings;
/**
* Created by hjw on 16/8/28.
* 鸡翅实现类
*/
public class ChinaChickenWings extends ChickenWings {
/**
* 构造函数
* @param kind
* @param price
* @param num
*/
public ChinaChickenWings(String kind,float price,int num) {
this.kind = kind;
this.price = price;
this.num = num;
}
}
========具体食物汉堡
package SheJiMoShiNaDianShi.AbstractFactory.model.kfc;
import SheJiMoShiNaDianShi.AbstractFactory.model.Hamburg;
/**
* Created by hjw on 16/8/28.
* 创建具体的食物
*/
public class ChinaHamburg extends Hamburg {
/**
* 构造函数
* @param kind
* @param price
* @param num
*/
public ChinaHamburg(String kind,float price,int num) {
this.kind = kind;
this.price = price;
this.num = num;
}
}
========抽象工厂接口
package SheJiMoShiNaDianShi.AbstractFactory.itf;
import SheJiMoShiNaDianShi.AbstractFactory.model.ChickenWings;
import SheJiMoShiNaDianShi.AbstractFactory.model.Hamburg;
/**
* Created by hjw on 16/8/28.
* 抽象工厂接口
*/
public interface IKfcFactory {
public Hamburg creatHamburg(String kind,float price,int num);
public ChickenWings creatChichenWings(String kind,float price,int num);
}
========具体工厂
package SheJiMoShiNaDianShi.AbstractFactory.itf;
import SheJiMoShiNaDianShi.AbstractFactory.model.ChickenWings;
import SheJiMoShiNaDianShi.AbstractFactory.model.Hamburg;
import SheJiMoShiNaDianShi.AbstractFactory.model.kfc.ChinaChickenWings;
import SheJiMoShiNaDianShi.AbstractFactory.model.kfc.ChinaHamburg;
/**
* Created by hjw on 16/8/28.
* 具体的工厂
*/
public class ChinaKfcFactory implements IKfcFactory {
@Override
public Hamburg creatHamburg(String kind, float price, int num) {
return new ChinaHamburg(kind, price, num);
}
@Override
public ChickenWings creatChichenWings(String kind, float price, int num) {
return new ChinaChickenWings(kind, price, num);
}
}
=====客户类-通过工厂暴露的方法实现具体对象的创建
package SheJiMoShiNaDianShi.AbstractFactory.custom;
import SheJiMoShiNaDianShi.AbstractFactory.itf.IKfcFactory;
import SheJiMoShiNaDianShi.AbstractFactory.model.ChickenWings;
import SheJiMoShiNaDianShi.AbstractFactory.model.Hamburg;
/**
* Created by hjw on 16/8/28.
* 客户类-通过工厂暴露的方法实现具体对象的创建
*/
public class Custom {
IKfcFactory iKfcFactory;
public Custom(IKfcFactory iKfcFactory) {
this.iKfcFactory = iKfcFactory;
}
public float orderHamburg(String kind, float price, int num){
Hamburg hamburg = iKfcFactory.creatHamburg(kind, price, num);
hamburg.printMessage();
return hamburg.totalPrice();
}
public float orderChickenWings(String kind, float price, int num){
ChickenWings chickenWings = iKfcFactory.creatChichenWings(kind, price, num);
chickenWings.printMessage();
return chickenWings.totalPrice();
}
}
=====app
package SheJiMoShiNaDianShi.AbstractFactory;
import SheJiMoShiNaDianShi.AbstractFactory.custom.Custom;
import SheJiMoShiNaDianShi.AbstractFactory.itf.ChinaKfcFactory;
import SheJiMoShiNaDianShi.AbstractFactory.itf.IKfcFactory;
/**
* Created by hjw on 16/8/28.
*/
public class MainApp {
public static void main(String[] args) {
IKfcFactory iKfcFactory = new ChinaKfcFactory();
Custom custom = new Custom(iKfcFactory);
float hamburgMoney = custom.orderHamburg("麻辣", 1.2f, 2);
float chichenWingsMoney = custom.orderChickenWings("奥尔良", 2, 4);
System.out.println("总计: " + (hamburgMoney + chichenWingsMoney));
}
}