《HeadFirst设计模式(第二版)》第四章代码——工厂模式

代码文件目录结构:

《HeadFirst设计模式(第二版)》第四章代码——工厂模式_第1张图片

Cheese:

原料ingredient类中只以Cheese为例,不重复展示:

package Chapter4_FactoryPattern.abstractFactoryPattern.Ingredient;

/**
 * @Author 竹心
 * @Date 2023/8/4
 **/

public abstract class Cheese {
    String name;

    String getName(){
        return name;
    }
}
package Chapter4_FactoryPattern.abstractFactoryPattern.Ingredient;

/**
 * @Author 竹心
 * @Date 2023/8/4
 **/

public class NYCheese extends Cheese{
    public NYCheese() {
        name = "NYCheese";
    }
}
package Chapter4_FactoryPattern.abstractFactoryPattern.Ingredient;

/**
 * @Author 竹心
 * @Date 2023/8/4
 **/

public class ChicagoCheese extends Cheese{
    public ChicagoCheese(){
        name = "ChicagoCheese";
    }
}
PizzaIngredientFactory
package Chapter4_FactoryPattern.abstractFactoryPattern.Ingredient;

/**
 * @Author 竹心
 * @Date 2023/8/4
 **/

public interface PizzaIngredientFactory {
    public Dough createDough();
    public Sauce createSauce();
    public Cheese createCheese();
    public Clams createClams();
}
NYPizzaIngredientFactory 
package Chapter4_FactoryPattern.abstractFactoryPattern.Ingredient;

/**
 * @Author 竹心
 * @Date 2023/8/4
 **/

public class NYPizzaIngredientFactory implements PizzaIngredientFactory{
    @Override
    public Dough createDough() {
        return new NYDough();
    }

    public Sauce createSauce(){
        return new NYSauce();
    }

    @Override
    public Cheese createCheese() {
        return new NYCheese();
    }

    @Override
    public Clams createClams() {
        return new NYClams();
    }
}
ChicagoPizzaIngredientFactory
package Chapter4_FactoryPattern.abstractFactoryPattern.Ingredient;

/**
 * @Author 竹心
 * @Date 2023/8/4
 **/

public class ChicagoPizzaIngredientFactory implements PizzaIngredientFactory{
    @Override
    public Dough createDough() {
        return new ChicagoDough();
    }

    public Sauce createSauce(){
        return new ChicagoSauce();
    }

    @Override
    public Cheese createCheese() {
        return new ChicagoCheese();
    }

    @Override
    public Clams createClams() {
        return new ChicagoClams();
    }
}
Pizza
package Chapter4_FactoryPattern.abstractFactoryPattern.Products;

import Chapter4_FactoryPattern.abstractFactoryPattern.Ingredient.*;

/**
 * @Author 竹心
 * @Date 2023/8/4
 **/

public abstract class Pizza {
    String name;

    Dough dough;
    Sauce sauce;
    Cheese cheese;
    Clams clams;

    public abstract void prepare();
    public void bake(){
        System.out.println("Bake for 25 minutes at 350");
    }

    public void cut(){
        System.out.println("Cutting the pizza into diagonal slices");
    }

    public void box(){
        System.out.println("place pizza in official PizzaStore box");
    }

    public String getName(){
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Pizza{" +
                "name='" + name + '\'' +
                ", dough=" + dough +
                ", sauce=" + sauce +
                ", cheese=" + cheese +
                ", clams=" + clams +
                '}';
    }
}
CheesePizza
package Chapter4_FactoryPattern.abstractFactoryPattern.Products;

import Chapter4_FactoryPattern.abstractFactoryPattern.Ingredient.PizzaIngredientFactory;

/**
 * @Author 竹心
 * @Date 2023/8/4
 **/

public class CheesePizza extends Pizza{
    PizzaIngredientFactory ingredientFactory;

    public CheesePizza(PizzaIngredientFactory ingredientFactory){
        //在初始化的时候确定是哪一种品味(通过原料工厂)
        this.ingredientFactory = ingredientFactory;
    }

    @Override
    public void prepare() {
        System.out.println("preparing "+name);
        dough = ingredientFactory.createDough();
        sauce = ingredientFactory.createSauce();
        cheese = ingredientFactory.createCheese();
    }
}
ClamPizza
package Chapter4_FactoryPattern.abstractFactoryPattern.Products;

import Chapter4_FactoryPattern.abstractFactoryPattern.Ingredient.PizzaIngredientFactory;

/**
 * @Author 竹心
 * @Date 2023/8/4
 **/

public class ClamPizza extends Pizza{
    PizzaIngredientFactory ingredientFactory;

    public ClamPizza(PizzaIngredientFactory ingredientFactory){
        this.ingredientFactory = ingredientFactory;
    }

    @Override
    public void prepare() {
        System.out.println("preparing "+name);
        dough = ingredientFactory.createDough();
        sauce = ingredientFactory.createSauce();
        cheese = ingredientFactory.createCheese();
        clams = ingredientFactory.createClams();
    }
}
PizzaStore
package Chapter4_FactoryPattern.abstractFactoryPattern;

import Chapter4_FactoryPattern.abstractFactoryPattern.Products.Pizza;

/**
 * @Author 竹心
 * @Date 2023/8/4
 **/

public abstract class PizzaStore {

    public Pizza orderPizza(String type){
        Pizza pizza;
        pizza = createPizza(type);

        pizza.prepare();
        pizza.bake();
        pizza.cut();
        pizza.box();
        return pizza;

    }

    protected abstract Pizza createPizza(String type);

}
NYPizzaStore
package Chapter4_FactoryPattern.abstractFactoryPattern;

import Chapter4_FactoryPattern.abstractFactoryPattern.Ingredient.NYPizzaIngredientFactory;
import Chapter4_FactoryPattern.abstractFactoryPattern.Ingredient.PizzaIngredientFactory;
import Chapter4_FactoryPattern.abstractFactoryPattern.Products.CheesePizza;
import Chapter4_FactoryPattern.abstractFactoryPattern.Products.ClamPizza;
import Chapter4_FactoryPattern.abstractFactoryPattern.Products.Pizza;

/**
 * @Author 竹心
 * @Date 2023/8/4
 **/

public class NYPizzaStore extends PizzaStore {
    protected Pizza createPizza(String item){
        Pizza pizza = null;
        PizzaIngredientFactory ingredientFactory =
                new NYPizzaIngredientFactory();

        if(item == "cheese"){
            pizza = new CheesePizza(ingredientFactory);
            pizza.setName("New York Style Cheese Pizza");
        }else if(item =="clam"){
            pizza = new ClamPizza(ingredientFactory);
            pizza.setName("New York Style Clam Pizza");
        }
        return pizza;
    }
}
ChicagoPizzaStore
package Chapter4_FactoryPattern.abstractFactoryPattern;

import Chapter4_FactoryPattern.abstractFactoryPattern.Ingredient.ChicagoPizzaIngredientFactory;
import Chapter4_FactoryPattern.abstractFactoryPattern.Ingredient.PizzaIngredientFactory;
import Chapter4_FactoryPattern.abstractFactoryPattern.Products.CheesePizza;
import Chapter4_FactoryPattern.abstractFactoryPattern.Products.ClamPizza;
import Chapter4_FactoryPattern.abstractFactoryPattern.Products.Pizza;

/**
 * @Author 竹心
 * @Date 2023/8/4
 **/

public class ChicagoPizzaStore extends PizzaStore {
    @Override
    protected Pizza createPizza(String item) {
        Pizza pizza = null;
        PizzaIngredientFactory ingredientFactory =
                new ChicagoPizzaIngredientFactory();

        if(item == "cheese"){
            pizza = new CheesePizza(ingredientFactory);
            pizza.setName("Chicago Style Cheese Pizza");
        }else if(item =="clam"){
            pizza = new ClamPizza(ingredientFactory);
            pizza.setName("Chicago Style Clam Pizza");
        }
        return pizza;
    }
}
PizzaTestDrive
package Chapter4_FactoryPattern.abstractFactoryPattern;

import Chapter4_FactoryPattern.abstractFactoryPattern.Products.Pizza;

/**
 * @Author 竹心
 * @Date 2023/8/4
 **/

public class PizzaTestDrive {
    public static void main(String[] args) {
        PizzaStore nyStore = new NYPizzaStore();
        PizzaStore chicagoStore = new ChicagoPizzaStore();

        Pizza pizza = nyStore.orderPizza("cheese");
        System.out.println("Ethan ordered a "+pizza.getName()+'\n');

        pizza = chicagoStore.orderPizza("cheese");
        System.out.println("Joel ordered a "+pizza.getName()+'\n');
    }
}
notes:
简单工厂模式:
    就是将平常的new过程封装成一个类来实现

工厂方法模式:
    定义一个创建对象的接口,但是由它的子类决定要实例化哪个类。工厂方法让类把实例化推迟到子类
    这里的“决定”其实是表示用哪个接口子类来创建产品。

抽象工厂模式:
    提供一个接口,用于创建相关或者依赖对象的家族,而不必指定他们的具体类
运行结果:

《HeadFirst设计模式(第二版)》第四章代码——工厂模式_第2张图片

 

你可能感兴趣的:(设计模式,java)