23 design patterns - simple factory pattern tells you how to buy a tea

Software design mode (Software Design Pattern), also known as design patterns, is a set of repeated use, known to most people, after cataloging, code design experience summary. It describes some of the problems in the software design process repeated occurrence, as well as solutions to the problem. In other words, it is a series of routines to solve a specific problem, is a summary of the code design experience of predecessors, has a certain universality, can be used repeatedly. Its purpose is to enhance code reusability reliability, readability and code.

Simple factory pattern - basic definitions:

Simple factory pattern is also known as a static factory method, is to create a schema. In a simple model the factory, according to the different parameters passed, returns an instance of a different class. Simple factory pattern defines a class that is specialized for creating instances of other classes, these classes are created have a common parent class.

Special instructions simple factory pattern design pattern is not one kind of 23 kinds of them, but he was behind the factory model and prototype the abstract factory pattern, so here to tell us about him as a primer. First understand the simple factory pattern help behind the study.


Mode structure:


23 design patterns - simple factory pattern tells you how to buy a tea

Simple factory pattern configuration diagram UML

Pattern Analysis:

Factory: factories role. Specifically used to create instances of the class factory to provide a method that returns Specific examples of the different classes according to different parameters passed.

Product: Abstract product role. For all products of the parent class.

ConcreteProduct: product specific role.

Simple factory mode creates the object and the object itself separated business process, the system can reduce the coupling, so that both of them are relatively easy to modify. When implementing change in the future, only you need to modify the factory class can be.

Mode Example:

There is a guest wanted to buy tea, he went to find the salesman to buy a cup of tea, salesman according to the different tastes of customers, catering staff to find the next one, such as stockings tea, mandarin tea, etc., the salesman does not care about how to do specific tea , according to the contents of the client purchase orders to get tea, and handed customers.

23 design patterns - simple factory pattern tells you how to buy a tea

Guests buy tea

Mode Summary:

advantage

1, a simple factory pattern to achieve the division of responsibility, to provide a special factory class for creating objects.

2, clients do not need to know the class name specific product category created only need to know the specific product class corresponding parameters can be, for some complex class name, a simple factory pattern can reduce the amount of user memory.

3, through the introduction of profiles, you can not modify the replacement and addition of new concrete product class in any case the client code and improve the flexibility of the system to some extent.

Shortcoming

1, because the factory class centralizes all product creation logic, once does not work, the entire system must be affected.

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

3, system expansion difficult, once the new product will have to add logic to modify the plant, when there are many types of products, may cause the plant logic is too complicated, is not conducive to the expansion and maintenance of the system.

4, a simple factory pattern due to the use of static factory method, resulting in the role of the factory can not form a hierarchy based on inheritance .

Scenarios

1, the object factory class is responsible for creating relatively small.

2, the client only knows the parameters passed factory class, how to create objects do not care

Code

The first step: the definition of the product we sell tea. Any need to prepare tea, production, and complete three steps.

PS: Creating tea Interface

com.leeborn.design.simpleFactory Package Penalty for; 
/ ** 
 * milk meal classified responsible for describing I need milk  
 * @author leeborn 
 * / 
public abstract class TeaMilk { 
	public abstract void PREPARE (); 
	public abstract void the make (); 
	public abstract void Complete (); 
}

Step Two: Pick We produce all kinds of tea cook

PS: tea making specific implementation class

Create two classes: TeaMilkStocking, TeaMilkDuck

com.leeborn.design.simpleFactory Package Penalty for; 
// achieve stockings tea cook 1 
public class TeaMilkStocking the extends TeaMilk { 
	@Override 
	public void PREPARE () { 
		System.out.println ( "ready stockings O (∩_∩) O!") ;	 
	} 
	@Override 
	public void the make () { 
		System.out.println ( "stockings made tea!");		 
	} 
	@Override 
	public void complete () { 
		System.out.println ( "complete pantyhose tea"); 
	} 
}

com.leeborn.design.simpleFactory Package Penalty for; 
// achieve 2 Yuanyang tea chefs 
public class TeaMilkDuck the extends TeaMilk { 
	@Override 
	public void PREPARE () { 
		System.out.println ( "ready Yuanyang O (∩_∩) O!") ; 
	} 
	@Override 
	public void the make () { 
		System.out.println ( "duck making tea!"); 

	} 
	@Override 
	public void complete () { 
		System.out.println ( "complete duck tea"); 
	} 
}

The third step: choose tea group foreman, the official told specifically what the chef work

com.leeborn.design.simpleFactory Package; 
Import java.time.LocalDateTime; 
// tea Cook Group Leader 
public class TeaMilkStore { 
	Private SimpleTeaMilkFactory simpleTeaMilkFactory; 
	public TeaMilkStore (SimpleTeaMilkFactory simpleTeaMilkFactory) { 
		this.simpleTeaMilkFactory = simpleTeaMilkFactory; 
	} 
	// create specific tea 
	public void orderTeaMilk (String typePizza) { 
		System.out.println ( "start making the kind of tea:" + typePizza); 
		teaMilk teaMilk = simpleTeaMilkFactory.createTeaMilk (typePizza); 
		teaMilk.prepare (); 
		teaMilk.make (); 
		teaMilk .complete (); 
		System.out.println ( "finished tea:" + LocalDateTime.now ()); 
	} 
}

Step Four: Make Cash registers available to salespeople use

Sales staff only need to select: mandarin duck tea or milk tea stockings

com.leeborn.design.simpleFactory Package Penalty for; 

// tea sales through mass participation ordering members of his application and get specific tea 
public class SimpleTeaMilkFactory { 
	public TeaMilk createTeaMilk (String typePizza) { 
		IF (typePizza.equals ( "DUCK")) { 
			new new TeaMilkDuck return (); 
		} 
		IF (typePizza.equals ( "STOCKING")) { 
			return new new TeaMilkStocking (); 
		} 
		the else { 
			return null; 
		} 
	} 
}

Step five: customers to buy tea PS: code testing

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

		// tea duck under a single client 
		teaMilkStore. orderTeaMilk ( "DUCK"); 
		// single stockings tea at customer 
		teaMilkStore.orderTeaMilk ( "STOCKING"); 
	} 
}


Guess you like

Origin blog.51cto.com/14231461/2481036