当前位置:网站首页>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 :
Suppose we design a pizza ordering system now , requirement Specific needs Yes :
- 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 .
- Every pizza has a specific Preparing for production 、 Bake 、 For cutting 、 Perform packaging and other operations .
- Finish the pizza shop Ordering system .
According to the principle of design pattern , We can design the following structure
PizzaFactory The pizza factory , Produce different pizzas according to different information .
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 )
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
List of articles
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 ~~~~
边栏推荐
- MySQL故障案例 | ERROR 1071 (42000): Specified key was too long
- 一个采用直接映射方式的32KB缓存......存储器课后习题
- Zone d'entrée du formulaire ionic5 et boutons radio
- Flink error --caused by: org apache. calcite. sql. parser. SqlParseException: Encountered “time“
- 玩转NanoPi 2 裸机教程编程-01点亮User LED难点解析
- 使用base64,展示图片
- One of the 12 balls is different from the others. Provide a balance and find it three times
- 类型从属名称的使用必须以“typename”为前缀
- Redis学习笔记—持久化机制之AOF
- Aiming at the overseas pet market, "grasshand" has developed an intelligent tracking product independent of mobile phones | early project
猜你喜欢
随机推荐
12个球,有一个与其他不一样,提供一个天平,三次找出来
Redis learning notes - redis and Lua
Leetcode topic analysis 3sum closest
Community article | mosn building subset optimization ideas sharing
36氪首发|云原生数据库公司「拓数派」完成新一轮战略融资,估值已达准独角兽级别
The sliding window of the force button "step by step" (209. sub array with the smallest length, 904. fruit basket)
ucosii(学习笔记)
JSP入门总结
"Coach, I want to play basketball" -- AI Learning Series booklet for students who are making systems
Longest substring without repeated characters (C language)
学习SCI论文绘制技巧(E)
Redis学习笔记—数据类型:字符串(string)
4、 Database design
C#之Lambda不得不说的用法
js 用**遮罩身份证以及手机号的重要数据
Utilisation du cookie du module de demande de noeud
What exactly is RT?
Quartz Crystal Drive Level Calculation
ionic5表单输入框和单选按钮
Combination sum II of leetcode topic analysis






