场景
当今每个人都有追求高品质生活的能力或需求。相信你也不例外,现在你在陪男朋友(或女朋友)在咖啡厅里点咖啡。(咖啡厅里只有一个店员)你告诉店员说:我要一杯拿铁;此时你男朋友(或女朋友)说:我要一杯卡布基诺。你喝到拿铁并赞叹:“我觉得口味偏奶气”,你男朋友喝到卡布基诺并赞叹:“我觉得口味糖分比较多”
代码
public class Drink {
private String name;
private String taste;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTaste() {
return taste;
}
public void setTaste(String taste) {
this.taste = taste;
}
}
public class Mocha extends Drink {
public Mocha(){
super.setName("mocha");
super.setTaste("hot chocolate");
}
}
public class Cappuccino extends Drink {
public Cappuccino(){
super.setName("cappuccino");
super.setTaste("rich milk coffee");
}
}
public class Customer {
public void drinking(String drinkName) {
Drink drink = Waiter.makeDrink(drinkName);
System.out.println("我觉得口味"+drink.getTaste());
}
}
public class Waiter {
public static Drink makeDrink(String drinkName) {
Drink drink = null;
if(StringUtils.equals(drinkName, "latte")){
drink = new Latte();
} else if(StringUtils.equals(drinkName, "cappuccino")) {
drink = new Cappuccino();
} else if(StringUtils.equals(drinkName, "mocha")) {
drink = new Mocha();
}
return drink;
}
}
public class Test {
public static void main(String[] args) {
Customer me = new Customer();
me.drinking("latte");
Customer he = new Customer();
he.drinking("cappuccino");
}
}
简单工厂
概念
静态工厂方法模式(Static FactoryMethod Pattern),是通过专门定义一个类来负责创建其他类的实例,被创建的实例通常都具有共同的父类。
注意:一般简单工厂要么是静态方法提供,要么是以单例存在。
类图
场景
大型的酒店后厨里,都有很多类型的厨师。现在你来酒店点单,你点了如下单品:日式拉面、牛排再来个松子桂鱼。订单到厨师长手里,后厨利有:
- 日式厨师,擅长:生鱼片、日式拉面;
- 欧式厨师,擅长:牛排、意大利面;
- 中式厨师,擅长:东北乱炖、松子桂鱼;
那对于厨师长来说怎么安排工作呢?使用代码实现厨师长工作安排。
代码
public class JapaneseNoodles extends Food {
public JapaneseNoodles(){
setName("Japanese_noodles");
setTaste("好吃还有豚骨口味");
}
}
public interface IChef {
public Food makeFood(String foodName);
public List<String> supportFoodName();
}
public class ChineseChef implements IChef {
public List<String> supportFoodName() {
return new ArrayList<String>(){
{
add("东北乱炖");
add("松子桂鱼");
}
};
}
public Food makeFood(String foodName) {
if(StringUtils.equals(foodName, "东北乱炖")) {
return new NortheastChaosStew();
}
if(StringUtils.equals(foodName, "松子桂鱼")){
return new PineNutfish();
}
throw new RuntimeException("我不擅长你点的菜品:"+ foodName);
}
}
public class EuropeanChef implements IChef{
public List<String> supportFoodName() {
return new ArrayList<String>(){
{
add("Steak");
add("spaghetti");
}
};
}
public Food makeFood(String foodName) {
if(StringUtils.equals(foodName, "Steak")) {
return new Steak();
} else if(StringUtils.equals(foodName, "spaghetti")) {
return new Spaghetti();
}
throw new RuntimeException("我不擅长你点的菜品:"+ foodName);
}
}
public class Test {
public static void main(String[] args) {
IChef chineseChef = new ChineseChef();
Food chinaFood = chineseChef.makeFood("Japanese_noodles");
System.out.println(chinaFood.getTaste());
IChef japanChef = new JapaneseChef();
Food japanFood = japanChef.makeFood("Japanese_noodles");
System.out.println(japanFood.getTaste());
}
}
工厂方法
概念
定义一个用于创建对象的接口,让子类决定实例化哪一个类,工厂方法使一个类的实例化延迟到其子类。
类图
场景:
现在车轮厂、车架厂和车座厂商给做车的厂商提供基础材料,车场出很品牌很多类型的车,请实现此功能
代码
public class Wheel {
private String size;
private String model;
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
}
public class MiqlWheel extends Wheel {
public void MiqlWheel() {
setModel("miqilin");
setSize("large");
}
}
public class WheelFactory {
public Wheel getWheel(String wheelModel){
if(StringUtils.equals(wheelModel, "miql")){
return new MiqlWheel();
}
return null;
}
}
public class Frame {
private String size;
private String color;
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
public class TaiFrame extends Frame {
public TaiFrame(){
setColor("yellow");
setSize("large");
}
}
public class FrameFactory {
public Frame makeFrame(String frameName) {
if(StringUtils.equals("taiFrame", frameName)) {
return new TaiFrame();
}
return null;
}
}
public interface ICarFactory {
public Wheel getWheel(String wheelName);
public Frame getFrame(String frameName);
}
public class AutoCarFactory implements ICarFactory {
private FrameFactory frameFactory = new FrameFactory();
private WheelFactory wheelFactory = new WheelFactory();
public Wheel getWheel(String wheelName) {
return wheelFactory.getWheel(wheelName);
}
public Frame getFrame(String frameName) {
return frameFactory.makeFrame(frameName);
}
}
如果需要增加材料厂,只需要增加材料选择工厂,提供方法,对原有的接口提供无变化。
抽象工厂
概念
抽象工厂模式围绕一个超级工厂,创造其他工厂。这个工厂也被称为工厂的工厂。提供一个创建一系列相关或相互依赖对象的接口,而无需指定他们具体的类