2. Simple Factory Mode
See an example of ordering bread that requires the program to be scalable
1) There are many kinds of bread. (ButterBread, Toast...)
2) Preparation process (prepare, bake, pack)
3) Complete the ordering function of bread
2.1 Traditional methods
Ideas:
Needed classes, Bread Abstract classes, ButterBread,Toast specific implementation classes, OrderBread bread ordering classes
Specifically, order different types of bread in OrderBread
UML Class Diagram
Code
//Bread.java public abstract class Bread { protected String type; //Value used to set type public void setType(String type) { this.type = type; } //Preparing bread materials public void prepare() { System.out.println("Preparing" + type + "Bread materials"); } //Baking bread public void bake() { System.out.println(type + "Baking in progress"); } //Pack bread public void pack() { System.out.println(type + "Packing in progress"); } } class Toast extends Bread { } class ButterBread extends Bread { } //OrderBread.java public class OrderBread { public void orderBread() { while(true) { String type = getType(); Bread bread; if ("Toast".equalsIgnoreCase(type)) { bread = new ButterBread(); bread.setType("Toast"); } else if ("ButterBread".equalsIgnoreCase(type)) { bread = new Toast(); bread.setType("ButterBread"); } else { break; } //Output bread ordering process if (bread != null) { bread.prepare(); bread.bake(); bread.pack(); System.out.println("----------------------------"); } } } //You can get the type of bread the customer wants to order by getting input data from the console private String getType() { try { BufferedReader strin = new BufferedReader(new InputStreamReader(System.in)); System.out.println("input Bread type:"); String str = strin.readLine(); return str; } catch (IOException e) { e.printStackTrace(); return ""; } } } //main test code public static void main(String[] args) { OrderBread orderBread = new OrderBread(); orderBread.orderBread(); }
Test results
input pizza types:
toast
Preparing materials for Toast bread
Toast is baking
Toast is being packaged
----------------------------
input pizza types:
butterbread
Preparing materials for ButterBread bread
ButterBread is baking
ButterBread is being packaged
----------------------------
input pizza types:
Problem
1. Let's see what happens if you add a type of bread (WholeWheatBread)
You can see the two red lines in the diagram. The main change is to add a class and modify the if branch code in OrderBread
2. The previous situation seems not to be very problematic, referring to the modification of two parts, so if our store expands and we want to add a new ordering class? It will become the following situation
And if we were adding a bread and we needed to change both OrderBread1 and OrderBread2, we would double that directly.
As you can see, this approach is very detrimental to expansion and maintenance as more classes are added. The more classes there are, the more changes needed to increase expansion will be made, and slightly inadvertent omissions may occur and errors will occur.
2.2 Simple Factory Mode
Ideas:
A new SimpleFactory class is added, which transfers the ordering process of bread to the inside of the class, so that other Order classes depend on SimpleFactory classes, making all kinds of Order classes independent of bread. Other Order classes depend on SimpleFactory classes instead.
UML class diagram
Code (only classes modified or added)
//OrderBread.java public class OrderBread { private SimpleFactory simpleFactory = new SimpleFactory(); public void orderBread() { while (true) { Bread bread = simpleFactory.createBread(getType()); //Output bread ordering process if (bread != null) { bread.prepare(); bread.bake(); bread.pack(); System.out.println("----------------------------"); } else { System.out.println("Failed to order"); break; } } } //You can get the type of bread the customer wants to order by getting input data from the console private String getType() { try { BufferedReader strin = new BufferedReader(new InputStreamReader(System.in)); System.out.println("input Bread type:"); String str = strin.readLine(); return str; } catch (IOException e) { e.printStackTrace(); return ""; } } } //New SimpleFactory Class public class SimpleFactory { //Move the bread creation process from the Order class here public Bread createBread(String type) { Bread bread = null; if ("Toast".equalsIgnoreCase(type)) { bread = new ButterBread(); bread.setType("Toast"); } else if ("ButterBread".equalsIgnoreCase(type)) { bread = new Toast(); bread.setType("ButterBread"); } return bread; } }
Expand Requirements
When we try to add a new Order and bread category, we just need to add a new bread category in SimpleFactory instead of modifying each Order class entirely.
2.3 Comparison of the two methods
Traditional methods: easy to understand, but not conducive to expansion and maintenance.
Simple Factory Mode: Delegate the process of creating classes to the factory class to complete, allowing for easy expansion of new functionality.