1. Introduction to life scenes
1. Introducing scenes
A brief description of the ordering process 1) Food Abstract class, specifying the operation of basic properties of food 2) Fish, Chicken Food Expansion 3) Ordering process class, according to the name of the food, processing specified types of food 4) Simulate client reservation
2. Source Code Implementation
1) Relationship Map
2) Code implementation
/** * Simple Factory Mode Introduction Scene */ public class C01_InScene { public static void main(String[] args) { OrderFood1 orderFood = new OrderFood1() ; orderFood.orderFood("fish") ; orderFood.orderFood("chicken") ; } } /** * Ordering process */ class OrderFood1 { public Food1 orderFood (String foodName){ Food1 food1 = null ; if (foodName.equals("fish")){ food1 = new FishFood1() ; food1.setName("Snakehead"); } else if (foodName.equals("chicken")){ food1 = new ChickenFood1() ; food1.setName("Free range chicken"); } if (food1 != null){ food1.foodMaterial(); food1.cookFood(); return food1 ; } else { return null ; } } } /** * Food abstract class */ abstract class Food1 { protected String name ; public abstract void foodMaterial () ; public void cookFood (){ System.out.println("Food cooking:" + name); } public String getName() { return name; } public void setName(String name) { this.name = name; } } /** * Material: One black fish */ class FishFood1 extends Food1 { @Override public void foodMaterial() { System.out.println("Material: One black fish"); } } /** * Material: Turkey No.1 */ class ChickenFood1 extends Food1 { @Override public void foodMaterial() { System.out.println("Material: Turkey No.1"); } }
3. Disadvantage Analysis
1) OCP Principles: Software entities, such as classes, modules, and functions, should be open to extensions but closed to modifications.
2) In violation of OCP principle of design mode, adding new food class is not easy to expand, and the code changes are large.
2. Simple Factory Mode
1. Basic concepts
Simple factory mode is a creation mode, also known as Static Factory Method mode, in which an object instance of which class is created is determined by a factory object.
2. Code logic
1) Relationship Map
2) Code implementation
/** * Simple Factory Mode */ public class C02_SimpleFactory { public static void main(String[] args) { OrderFood2 orderFood2 = new OrderFood2() ; orderFood2.orderFood("chicken"); } } class OrderFood2 { private SimpleFactory simpleFactory = new SimpleFactory() ; public void orderFood (String foodName){ simpleFactory.orderFood(foodName) ; } } /** * Simple Factory Class: Production Process for Encapsulated Food */ class SimpleFactory { public Food2 orderFood (String foodName){ Food2 food2 = null ; if (foodName.equals("fish")){ food2 = new FishFood2() ; food2.setName("Snakehead"); } else if (foodName.equals("chicken")){ food2 = new ChickenFood2() ; food2.setName("Free range chicken"); } if (food2 != null){ food2.foodMaterial(); food2.cookFood(); return food2 ; } else { return null ; } } } abstract class Food2 { protected String name ; public abstract void foodMaterial () ; public void cookFood (){ System.out.println("Food cooking:" + name); } public String getName() { return name; } public void setName(String name) { this.name = name; } } class FishFood2 extends Food2 { @Override public void foodMaterial() { System.out.println("Material: One black fish"); } } class ChickenFood2 extends Food2 { @Override public void foodMaterial() { System.out.println("Material: Turkey No.1"); } }
3. Advantages and disadvantages analysis
1) Summary of advantages
The core of this pattern is the factory class.This class contains the necessary logical judgments.
You can decide when to create an instance of a login validation class.
The caller is exempt from creating the object directly.In this way, the simple factory model divides responsibility.
There is no need to modify the caller when the system introduces a new login method.
2) Summary of shortcomings
This factory class aggregates all the creation logic, and when there is a complex hierarchy of levels, All business logic is implemented in this factory class.When does it stop working? The whole system will be affected.
3. Source code address
GitHub Address: Know a smile https://github.com/cicadasmile/model-arithmetic-parent Code Cloud Address: Know a smile https://gitee.com/cicadasmile/model-arithmetic-parent