当前位置:网站首页>Pizza ordering design - simple factory model

Pizza ordering design - simple factory model

2022-06-23 09:16:00 Gaolw1102

Pizza ordering system ---- Simple factory mode implementation

Basic introduction of simple factory model :
 Insert picture description here

Suppose we design a pizza ordering system now , requirement Specific needs Yes :

  1. Now the pizza There are many kinds , Such as GreekPizza、CheesePizza、Pepper Wait for pizza type , Ask the user to enter Pizza name , Get the pizza .
  2. Every pizza has a specific Preparing for production 、 Bake 、 For cutting 、 Perform packaging and other operations .
  3. Finish the pizza shop Ordering system .

According to the principle of design pattern , We can design the following structure

  1. PizzaFactory The pizza factory , Produce different pizzas according to different information .

  2. OrderPizza、PizzaStore Pizza orders ( Responsible for receiving pizza information input by users 、 Obtain factory production objects 、 Output pizza information ) and Pizza shop ( The client side , Program initial point )

  3. Pizza、GreekPizza、CheesePizza、PepperPizza An abstract pizza class 、 Greek pizza 、 Cheese pizza 、 Pepper pizza .

      Because there are many parts involved , Just list the implementation steps , Hey .
    

    If the little friends are interested in it , Can watch --> Mr. Han Shunping's java Design patterns

Code demonstration

Pizza shops and pizza orders

Pizza shop

package com.design_patterns.factory.simplefactory.order;

/** *  Pizza shop , Pizza can be produced and obtained through pizza shops  */
public class PizzaStore {
    
    public static void main(String[] args) {
    
        new OrderPizza(new PizzaFactory());         // Instantiate the pizza order class , Transfer pizza factory objects in for production 
        System.out.println("~~~~ Exit procedure ~~~~");     // Output program exit information 
    }

}

Pizza ordering system code

package com.design_patterns.factory.simplefactory.order;

import com.design_patterns.factory.simplefactory.pizza.CheesePizza;
import com.design_patterns.factory.simplefactory.pizza.GreekPizza;
import com.design_patterns.factory.simplefactory.pizza.Pizza;

import java.util.Scanner;

/** *  Define the pizza ordering class , This class allows you to order pizza  */
public class OrderPizza {
    

    Pizza pizza = null;                     // Declare pizza class objects 
    String orderType = null;                // Declare the subscription information entered by the user 
    Scanner sc = new Scanner(System.in);            // Define standard input stream objects and instantiate 

    // The factory object that produces pizza is passed in through the construction method 
    public OrderPizza(PizzaFactory pizzaFactory) {
    
        getPizza(pizzaFactory);         // call getPizza Method 

    }

    /** *  This method enters the ordered for the user pizza type , Factory production returns , How to output pizza information  * @param pizzaFactory */
    public void getPizza(PizzaFactory pizzaFactory){
    
        do{
    
            System.out.println(" Please enter the... You need to order Pizza type :");
            orderType = sc.nextLine();                      // User input order pizza Information about 
            pizza = pizzaFactory.createPizza(orderType);            // The corresponding... Is produced through the factory object pizza
            if(pizza != null){
    
                // Output Pizza The production process of 
                pizza.prepared();
                pizza.bake();
                pizza.cut();
                pizza.box();
            }else {
    
                System.out.println(" No such pizza type , Order failed !");
                break;
            }
        }while (true);
    }

}

Pizza factory

package com.design_patterns.factory.simplefactory.order;

import com.design_patterns.factory.simplefactory.pizza.CheesePizza;
import com.design_patterns.factory.simplefactory.pizza.GreekPizza;
import com.design_patterns.factory.simplefactory.pizza.PepperPizza;
import com.design_patterns.factory.simplefactory.pizza.Pizza;

/** *  Define the pizza factory class , All types of pizza can be produced by this method  */
public class PizzaFactory {
    

    private Pizza pizza = null;

    /** *  Definition Pizza The factory creates various Pizza Methods , Produce all kinds of pizza through order information  *  Adopted java Characteristics of polymorphism , all Pizza All subclasses of can be instantiated  * @param orderType * @return */
    public Pizza createPizza(String orderType){
    
        // Use switch sentence , According to the different needs of users, different kinds of pizza
        switch (orderType){
    
            case "greek":
                pizza = new GreekPizza();           // Instantiate pizza 
                pizza.setName(" Greek pizza ---->");     // Set the name 
                break;
            case "cheese":
                pizza = new CheesePizza();
                pizza.setName(" Cheese pizza ---->");
                break;
            case "pepper":
                pizza = new PepperPizza();
                pizza.setName(" Pepper pizza ---->");
                break;
            default:
                pizza = null;
                break;
        }
        return pizza;               // Return to the pizza produced 
    }
}

Pizza abstract class and its subclasses

abstract class

package com.design_patterns.factory.simplefactory.pizza;

// Define the abstract parent class of all pizzas 
public abstract class Pizza {
    
    protected String name;                  // Define the pizza name 

    // Prepare raw materials , Different pizzas are different , therefore , To make this method an abstract method 
    public abstract void prepared();

    // Baking method 
    public void bake(){
    
        System.out.println(name + " baking; ");
    }

    // Cutting method 
    public void cut(){
    
        System.out.println(name + " cutting; ");
    }

    // Packaging method 
    public void box(){
    
        System.out.println(name + " boxing; ");
    }

    // by Pizza Set the name 
    public void setName(String name){
    
        this.name = name;
    }
}

Greek pizza

package com.design_patterns.factory.simplefactory.pizza;

// Define the Greek pizza class 
public class GreekPizza extends Pizza {
    

    @Override
    public void prepared() {
    
        System.out.println(" Making Greek pizza ----> Prepare raw materials !");
    }
}

Cheese pizza

package com.design_patterns.factory.simplefactory.pizza;

// Define cheese pizza classes 
public class CheesePizza extends Pizza {
    

    @Override       // Override the abstract method of the parent class 
    public void prepared() {
    
        System.out.println(" Make cheese pizza ----> Prepare raw materials !");
    }


}

Pepper pizza

package com.design_patterns.factory.simplefactory.pizza;

// Define the pepper pizza class 
public class PepperPizza extends Pizza {
    

    @Override
    public void prepared() {
    
        System.out.println(" Make pepper pizza ----> Prepare raw materials !");
    }
}

Running results (PizzaStore For program entry )

 Please enter the... You need to order Pizza type :
greek
 Making Greek pizza ----> Prepare raw materials !
 Greek pizza ----> baking; 
 Greek pizza ----> cutting; 
 Greek pizza ----> boxing; 
 Please enter the... You need to order Pizza type :
cheese
 Make cheese pizza ----> Prepare raw materials !
 Cheese pizza ----> baking; 
 Cheese pizza ----> cutting; 
 Cheese pizza ----> boxing; 
 Please enter the... You need to order Pizza type :
pepper
 Make pepper pizza ----> Prepare raw materials !
 Pepper pizza ----> baking; 
 Pepper pizza ----> cutting; 
 Pepper pizza ----> boxing; 
 Please enter the... You need to order Pizza type :
chian
 No such pizza type , Order failed !
~~~~ Exit procedure ~~~~

原网站

版权声明
本文为[Gaolw1102]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230858151638.html