23 design modes - Simple factory mode tells you how to buy milk tea

Software Design Pattern, also known as design pattern, is a set of experience summary of repeated use, most people know, categorized and coded design.It describes some recurring problems in the software design process and solutions to them.That is to say, it is a series of routines to solve specific problems, is a summary of the code design experience of predecessors, has a certain universality, and can be used repeatedly.The goal is to improve code reusability, readability, and reliability.

Simple Factory Mode - Basic Definitions:

Simple factory mode, also known as static factory method, is a creation mode.In simple factory mode, instances of different classes can be returned depending on the parameters passed.The Simple Factory pattern defines a class specifically for creating instances of other classes that share a common parent.

Specifically, the simple factory mode is not one of the 23 design modes, but it is the prototype of the latter factory mode and the abstract factory mode, so here is his introduction first.Understanding the simple factory model first will help you learn later.


Pattern structure:


Simple Factory Mode UML Structure Diagram

Pattern Analysis:

Factory: Factory role.A factory dedicated to creating instance classes provides a method for returning specific instances of different classes depending on the parameters passed.

Product: Abstract product role.Is the parent of all products.

ConcreteProduct: Specific product role.

Simple factory mode separates object creation from the object's own business processing, reduces system coupling and makes both relatively easy to modify.When you make changes later, you only need to modify the factory class.

Examples of patterns:

There is a customer who wants to buy milk tea. He goes to the salesperson to buy a cup of milk tea. The salesperson should go to the caterer to order according to different customers'tastes, such as stockings milk tea, mandarin duck milk tea, etc. The salesperson does not care about how the milk tea is made. The milk tea is obtained according to the contents of the customer's purchase order and delivered to the customer.

Guests buy milk tea

Mode Summary:

Advantage

1. Simple factory mode achieves the division of responsibilities and provides specialized factory classes for creating objects.

2. Clients do not need to know the class name of the specific product class they are creating. They only need to know the parameters corresponding to the specific product class. For some complex class names, simple factory mode can reduce the user's memory.

3. By introducing configuration files, new specific product classes can be replaced and added without modifying any client code, which improves the flexibility of the system to some extent.

shortcoming

1. Because the factory class centralizes all the product creation logic, once it does not work properly, the whole system will be affected.

2. Using the simple factory mode will increase the number of classes in the system, and increase the complexity and difficulty of understanding the system in a certain program.

3. System expansion is difficult. Once new products are added, factory logic has to be modified. When there are many types of products, factory logic may be too complex to facilitate system expansion and maintenance.

4. Simple Factory Mode uses a static factory method, which prevents factory roles from forming an inheritance-based hierarchy.

Application Scenarios

1. The factory class is responsible for creating fewer objects.

2. The client only knows the parameters passed into the factory class and does not care how to create the object

code implementation

Step 1: Define the products we sell, milk tea.Any milk tea needs to be prepared, prepared, and completed in three steps.

PS: Create milk tea interface

package com.leeborn.design.simpleFactory;
/**
 * The Milk Category is responsible for describing that I need milk tea.
 * @author leeborn
 */
public abstract class TeaMilk {
	public abstract void prepare();
	public abstract void make();
	public abstract void complete();
}

Step 2: Select our chefs to make all kinds of milk tea

PS: Specific milk tea production implementation class

Create two classes: TeaMilkStocking, TeaMilkDuck

package com.leeborn.design.simpleFactory;
//Implement 1 Chef for Silk Socks Milk Tea
public class TeaMilkStocking extends TeaMilk{
	@Override
	public void prepare() {
		System.out.println("Prepare stockings! O(∩_∩)O");	
	}
	@Override
	public void make() {
		System.out.println("Making silk stockings milk tea!");		
	}
	@Override
	public void complete() {
		System.out.println("Complete Silk Stockings Milk Tea");
	}
}

package com.leeborn.design.simpleFactory;
//The chef who makes 2 Duck Milk Tea
public class TeaMilkDuck extends TeaMilk{
	@Override
	public void prepare() {
		System.out.println("Prepare the ducks! O(∩_∩)O");
	}
	@Override
	public void make() {
		System.out.println("Making duck milk tea!");

	}
	@Override
	public void complete() {
		System.out.println("Complete the Duck Milk Tea");
	}
}

Step 3: Select the Tea Tea Tea Tea Tea Tea Tea Tea Tea Leader to tell the specific chef who is working

package com.leeborn.design.simpleFactory;
import java.time.LocalDateTime;
//Head of Tea Chef Group
public class TeaMilkStore {
	private SimpleTeaMilkFactory simpleTeaMilkFactory;
	public TeaMilkStore(SimpleTeaMilkFactory simpleTeaMilkFactory){
		this.simpleTeaMilkFactory = simpleTeaMilkFactory;
	}
	//Make specific milk tea
	public void orderTeaMilk(String typePizza){
		System.out.println("Start making types of milk tea:"+ typePizza);
		TeaMilk teaMilk = simpleTeaMilkFactory.createTeaMilk(typePizza);
		teaMilk.prepare();
		teaMilk.make();
		teaMilk.complete();
		System.out.println("Making milk tea:" + LocalDateTime.now());
	}
}

Step 4: Make a cash register available to salespeople

The salesperson only needs to choose: Silk stockings milk tea or Mandarin duck milk tea

package com.leeborn.design.simpleFactory;

//The Milk Tea Trainer requests and obtains the specific milk tea through him
public class SimpleTeaMilkFactory {
	public TeaMilk createTeaMilk(String typePizza){
		if (typePizza.equals("DUCK")) {
			return new TeaMilkDuck();
		}
		if (typePizza.equals("STOCKING")) {
			return new TeaMilkStocking();
		}
		else {
			return null;
		}
	}
}

Step 5: Customer Buying Milk Tea PS: Code Test

package com.leeborn.design.simpleFactory;
public class test {
	public static void main(String[] args) {
		SimpleTeaMilkFactory simpleTeaMilkFactory = new SimpleTeaMilkFactory();
		TeaMilkStore teaMilkStore = new TeaMilkStore(simpleTeaMilkFactory);

		//Customer orders Mandarin duck milk tea
		teaMilkStore.orderTeaMilk("DUCK");
		//Customer's milk tea with single stockings
		teaMilkStore.orderTeaMilk("STOCKING");
	}
}


Tags: Java

Posted on Mon, 23 Mar 2020 13:55:59 -0400 by petromanian