工厂模式-D- 抽象工厂模式

package D_Factory.d;

public class CheesePizza extends Pizza {
PizzaIngredientFactory ingredientFactory;

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

@Override
public void prepare() {
    dough = ingredientFactory.createDough();
    sauce = ingredientFactory.createSauce();
    cheese = ingredientFactory.createCheese();
}

}
package D_Factory.d;

public class CheesePizza extends Pizza {
PizzaIngredientFactory ingredientFactory;

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

@Override
public void prepare() {
    dough = ingredientFactory.createDough();
    sauce = ingredientFactory.createSauce();
    cheese = ingredientFactory.createCheese();
}

}
package D_Factory.d;

public class ClamPizza extends Pizza {
PizzaIngredientFactory ingredientFactory;

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

@Override
public void prepare() {
    //需要的材料跟工厂要,不同风味的披萨可以使用原料工厂处理地区差异,而不需要设计不同的类进行处理
    dough = ingredientFactory.createDough();
    sauce = ingredientFactory.createSauce();
    cheese = ingredientFactory.createCheese();
    cheese = ingredientFactory.createClam();
}

}
package D_Factory.d;

public interface Dough {

}
package D_Factory.d;

public class NYPizzaIngredientFactory implements PizzaIngredientFactory {

// 可以创建具体的dough材料
public Dough createDough() {
    return new ThickCrustDough();
}

public String createSauce() {
    return null;
}

public String createCheese() {
    return null;
}

public String createClam() {
    return null;
}

}

class ThickCrustDough implements Dough {

}
package D_Factory.d;

/*
* 需要的材料跟工厂要,不同风味的披萨可以使用原料工厂处理地区差异,而不需要设计不同的类进行处理
*/
public class NYPizzaStore extends PizzaStore {

@Override
protected Pizza createPizza(String type) {
    PizzaIngredientFactory pizzaIngredientFactory = new NYPizzaIngredientFactory();
    Pizza pizza = null;
    if (type.equals("cheese")) {
        pizza = new CheesePizza(pizzaIngredientFactory);
        pizza.setName("cheese");
    } else if (type.equals("clam")) {
        pizza = new ClamPizza(pizzaIngredientFactory);
        pizza.setName("clam");
    }
    return pizza;
}

}
package D_Factory.d;

import D_Factory.d.Dough;

public abstract class Pizza {
public String name;
public Dough dough;
public String sauce;
public String cheese;
public String clam;

public void prepare() {
    System.out.println("prepare");
}

public void bake() {
    System.out.println("bake");
}

public void cut() {
    System.out.println("cut");
}

public void box() {
    System.out.println("box");
}

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

public String getName() {
    return name;
}

}
package D_Factory.d;

public interface PizzaIngredientFactory {

public Dough createDough();

public String createSauce();

public String createCheese();

public String createClam();

}
package D_Factory.d;

/**
* 由于做披萨做法有差异,使用其他厂商的盒子等等
* 解决让披萨制作局限于pizzaStore类,同时又能让加盟店依然可以自由制作该地区风味的披萨
*/
public abstract class PizzaStore {

public final 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);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
工厂模式是一种创建型设计模式,它提供了一种创建对象的最佳方式,而无需暴露创建逻辑。以下是 C 语言实现工厂模式的示例代码: ``` #include <stdio.h> #include <stdlib.h> // 定义抽象产品类 typedef struct Product { int id; char* name; } Product; // 定义具体产品类 typedef struct ConcreteProductA { Product product; // 继承自抽象产品类 int price; } ConcreteProductA; typedef struct ConcreteProductB { Product product; // 继承自抽象产品类 char* color; } ConcreteProductB; // 定义抽象工厂类 typedef struct Factory { Product* (*createProduct)(int id, char* name); // 创建产品的方法 } Factory; // 定义具体工厂类 typedef struct ConcreteFactoryA { Factory factory; // 继承自抽象工厂类 int price; } ConcreteFactoryA; typedef struct ConcreteFactoryB { Factory factory; // 继承自抽象工厂类 char* color; } ConcreteFactoryB; // 实现抽象工厂类的方法 Product* createProduct(int id, char* name) { Product* product = (Product*)malloc(sizeof(Product)); product->id = id; product->name = name; return product; } // 实现具体工厂类的方法 Product* createProductA(int id, char* name) { ConcreteProductA* product = (ConcreteProductA*)malloc(sizeof(ConcreteProductA)); product->product = *createProduct(id, name); product->price = 100; return (Product*)product; } Product* createProductB(int id, char* name) { ConcreteProductB* product = (ConcreteProductB*)malloc(sizeof(ConcreteProductB)); product->product = *createProduct(id, name); product->color = "red"; return (Product*)product; } // 主函数 int main() { ConcreteFactoryA factoryA = { { createProductA }, 200 }; ConcreteFactoryB factoryB = { { createProductB }, "blue" }; Product* productA = factoryA.factory.createProduct(1, "productA"); printf("productA: id=%d name=%s price=%d\n", productA->id, productA->name, ((ConcreteProductA*)productA)->price); Product* productB = factoryB.factory.createProduct(2, "productB"); printf("productB: id=%d name=%s color=%s\n", productB->id, productB->name, ((ConcreteProductB*)productB)->color); free(productA); free(productB); return 0; } ``` 在上面的代码中,我们首先定义了抽象产品类 `Product` 和具体产品类 `ConcreteProductA` 和 `ConcreteProductB`。然后定义了抽象工厂类 `Factory` 和具体工厂类 `ConcreteFactoryA` 和 `ConcreteFactoryB`,并实现了抽象工厂类的方法 `createProduct`,以及具体工厂类的方法 `createProductA` 和 `createProductB`。最后在主函数中使用具体工厂类创建产品,并输出产品信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值