Head First Design Patterns-- simple factory pattern (static factory)

In this article, a basic knowledge of UML class diagrams, if there are not clear can refer to:

UML correct way to eat ---- positive solution! !

When we instantiate an object, the first thought is that new objects, no house, a new, no girlfriend, a new and even more good. The object is established between the new class and the coupling relationship, if there are a lot of objects, between classes and objects to form a strong coupling, flexibility deteriorates, so, in general we are facing in the development process "Interface" programming through polymorphism, this interface may be implemented with any new class. However, the same code is too big, you get it working! So in addition to new you will have no other way to do it? This involves the design of design patterns:

Users specify the type to be created in the test class:

We look at the class diagram:
Here Insert Picture Description

Description: Pizza class is an abstract class, then inherit Pizza CheesePizza and ClamPizza class and override the prepare method, then, to create the specified depending on the type of Pizza in SimpleFactory simple plant and then combined in PizzStore in SimpleFactory, what the user wants Pizza, in PizzaStore tells, then let SimpleFactory make a specified type, which itself is not to be concerned with what type of Pizza. Whatever the final Pizza should bake, cut, box operation, after SimpleFactory finished creating Pizza, PizzaStore implementation bake, cut, box. Of course, there will be students ask, if we add a kind of Pizza it, then we can add SimpleFactory to get in, do the "open for extension, but closed for modification" principle ocp a mouthful! Let's look at specific implementation process

  • Pizza
public abstract class Pizza {

    private String name;
    public abstract void prepare();

    public void bake(){
        System.out.println(name+ "  "+ "backing");
    }
    public void cut(){
        System.out.println(name+ "  "+ "cutting");
    }
    public void box(){
        System.out.println(name+ "  "+ "boxing");
    }

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

}
  • ClamPizza
public class ClamPizza extends Pizza {
    @Override
    public void prepare() {
        System.out.println("加入ClamPizza调料");
    }
}
  • CheesePizza
public class CheesePizza extends Pizza {
    @Override
    public void prepare() {
        System.out.println("加入CheesePizza调料");
    }
}

  • SimpleFactory
public  class SimpleFactory {

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

        if (type.equals("Clam")){
            pizza = new ClamPizza();
            pizza.setName(type);
        }else if (type.equals("Cheese")){
            pizza = new CheesePizza();
            pizza.setName(type);
        }
        return pizza;
    }
}
  • PizzaStore
public class PizzaStore {

    SimpleFactory simpleFactory;

    public void setSimpleFactory(SimpleFactory simpleFactory) {
        this.simpleFactory = simpleFactory;
    }

    public Pizza orderPizza(String type, SimpleFactory simpleFactory){
        Pizza pizza;

        pizza = simpleFactory.createPizza(type);

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

        return pizza;

    }
}
  • TestPizza
public class TestPizza {
    public static void main(String[] args) {

        //新建一个PizzaStore用来卖Pizza
        PizzaStore pizzaStore = new PizzaStore();

        //传入要买的类型,并且要有一个工厂
        pizzaStore.orderPizza("Clam",new SimpleFactory());
    }
}
  • Test Results
    Here Insert Picture Description

The type of console users to create dynamic input:

1) There are two types of pizza: Pizza, Greece (greek), cheese pizza (Cheese)
2) pizza production there BAK, Cut, Box;
3) Given a type of pizza creation process

  • Pizza
//定义一个抽象的披萨类
public abstract  class Pizza {
	
	//抽象方法
    public abstract void prepare();

	//制做的工艺
    public void bake() {
        System.out.println("baking;");
    }

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

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

}
  • CheesePizza2
public class CheesePizza2 extends Pizza {
    @Override
    public void prepare() {
        System.out.println("给奶酪披萨准备原材料");
    }
}

  • GreekPizza2
public class GreekPizza2 extends Pizza{
    @Override
    public void prepare() {
        System.out.println("给希腊披萨准备原材料");
    }
}

  • orderPizza2
public class orderPizza2 {

    Pizza pizza = null;

    public Pizza orderPizza(String orderType){

        if (orderType.equals("greek")){
            pizza = new GreekPizza2();
        }else if (orderType.equals("cheese")){
            pizza = new CheesePizza2();
        }

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

        return pizza;

    }
}

  • PizzaStore
public class PizzaStore {
    public static void main(String[] args) {

        orderPizza2 orderPizza2 = new orderPizza2();
        orderPizza2.orderPizza("greek");
        orderPizza2.orderPizza("cheese");

    }
}
  • test

Here Insert Picture Description

  • There are class diagrams
    Here Insert Picture Description

While we realize the function, but when we add a kind of pizza, which is necessary in orderPizza2 add, if we can orderPizza2 inside pulled out according to the method name to create a kind of pizza, a new method, designed to create an object, and then polymerized inside orderPizza2 this new way of doing this, it will be open for extension, but closed for modification to meet the principles ocp, went ahead!

  • Pizza
public abstract  class Pizza {

    public abstract void prepare();

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

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

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

}
  • CheesePizza2
public class CheesePizza2 extends Pizza {
    @Override
    public void prepare() {
        System.out.println("经制做奶酪披萨准备原材料");
    }
}
  • GreekPizza2
public class GreekPizza2 extends Pizza{
    @Override
    public void prepare() {
        System.out.println("给希腊披萨准备原材料");
    }
}

  • SimpleFactory2
public class SimpleFactory2 {

    public Pizza createPizza(String orderType){

        Pizza pizza = null;

        if (orderType.equals("greek")){
            pizza = new GreekPizza2();
        }else if (orderType.equals("cheese")){
            pizza = new CheesePizza2();
        }

        return pizza;
    }

}
  • OrderPizza2
//定义一个生成厂商,对传入的某种披萨进行prepare,bake,cut,box操作
public class orderPizza2 {

    SimpleFactory2 simpleFactory2;

    Pizza pizza = null;

    public orderPizza2(SimpleFactory2 simpleFactory2) {
        setSimpleFactory2(simpleFactory2);
    }

    public void  setSimpleFactory2(SimpleFactory2 simpleFactory2) {

        this.simpleFactory2 = simpleFactory2;

        //获取披萨的类型是什么
        String orderType = " ";
        orderType = getype();

        pizza = simpleFactory2.createPizza(orderType);

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

    }

    //写一个方法,可以获取客户端希望订购的披萨种类
    private String getype(){
        try {
            BufferedReader strin = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("input Pizza type:");
            String str = strin.readLine();
            return str;
        }catch (IOException e){
            e.printStackTrace();
            return "";
        }
    }
}
  • PizzaStore
public class PizzaStore {

    public static void main(String[] args) {

        new orderPizza2(new SimpleFactory2());
    }
}
  • result

Here Insert Picture Description

  • FIG class represents
    Here Insert Picture Description

In the instance of pizza, we use a factory to create pizza Depending on the type, come to realize prepare, bake, cut, box operation OrderPizza2 inside, if you want to add a new kind of pizza, just in the factory class join method, other codes do not change, thus achieving the purpose of simplifying the code!

In this article, a basic knowledge of UML class diagrams, if there are not clear can refer to:

UML correct way to eat ---- positive solution! !

If you think there is a problem you can be written in the comments area!

Published 47 original articles · won praise 34 · views 8862

Guess you like

Origin blog.csdn.net/weixin_42893085/article/details/105309872