4. creator Mode
1. Motorcycle Assembly System
These examples are suitable for beginners and easy to understand,
After reading it, I got an impression on what it looks like.
Motorcycle = frame + wheel + tire + engine.
First, design motorcycles and various parts:
Motorcycle: public interface motorcycle {void build ();} public class motorcycle1 implements motorcycle {void build () {sysout. out. println ("start assembling a motorcycle... ") ;}} motorcycle parts: Motorcycle Frame: public interface carcase {void build ();} public class carcase1 implements carcase {void build () {sysout. out. println ("starting from assembling a motorcycle frame... ") ;}} wheel class: public interface wheel {void build ();} public class wheel1 implements wheel {void build () {sysout. out. println ("Assemble the motorcycle wheel to start... ") ;}} tire type: public interface tyre {void build ();} public class tyre1 implements tyre {void build () {sysout. out. println ("starting to assemble a motorcycle tire... ") ;}} engine class: Public Interface Engine {void build ();} public class engine1 implements engine {void build () {sysout. out. println ("starting to assemble the engine... ");}}
Then design an abstract factory class that defines the creation method only:
Public interface factory {carcase createcarcase (); // create the frame object wheel createwheel (); // create the wheel object tyre createtyre (); // create the tire object engine createengine (); // create an engine object} public class motorcyclefactory implements factory {publci carcase createcarcase () {return New carcase1 ();} wheel createwheel () {return New wheel1 ();}; tyre createtyre () {return New tyre1 () ;}; engine createengine () {return New engine1 ();};}
The Code is as follows:
Public class client {public static void main (string [] ARGs) {factory = new motorcyclefactory (); carcase = factory. createcarcase (); carcase. build (); wheel = factory. createwhee (); wheel. build (); tyre = factory. createtyre (); tyre. build (); Engine engine = factory. createengine (); Engine. build (); // assemble the motorcycle parts. Return new motorcycle1 ();}}
This feature is implemented, but it seems strange and has some problems:
There are many motorcycle parts, so it is troublesome to assemble the client.
In the factory, the factory class is also very troublesome.
Solution:
Continue to divide responsibilities and add a separate class to be responsible for motorcycle assembly.
Add an assembly class to be responsible for product assembly. The factory class is only responsible for product creation,
The client directly calls the Assembly class.
Added Assembly class:
Public class director {private factory; Public void Director (factory) {This. factory = factory;} public motorcycle assemblemotorcycle () {carcase = factory. createcarcase (); carcase. build (); wheel = factory. createwhee (); wheel. build (); tyre = factory. createtyre (); tyre. build (); Engine engine = factory. createengine (); Engine. build (); // assemble the motorcycle parts. Return new motorcycle1 ();}}
In this way, the client call is simple:
Public class client
{
Public static void main (string [] ARGs ){
Directorr Director = new director (New motorcyclefactory ());
Motorcycle motorcycle = Director. assemblemotorcycle ();
}
}
2. Introduction to the Creator Mode
[Definition ]:
The Creator mode separates the creation of a complex object from its representation,
Yes. Different representations can be created for the same build process, which is not required by the client.
Understand the details of object creation.
Principle]
The Creator mode consists of five parts:
Assembly class, abstract creator class, specific creator class, abstract product class, and specific product class.
[Time to use]
When the system needs to create a complex object and the complex object is difficult to assemble,
You can use the Creator mode.
3. Advantages and Disadvantages
[Advantages]
In the Creator class, the client is no longer responsible for object creation and assembly, but is responsible for the Creation.
The specific creator class is assigned to the specific Assembly class. The client is only responsible for object calls,
Clarify the responsibilities of each category.
[Disadvantages]
Although different types of products can be created using the Creator mode, if the differences between products are too large
You need to write multiple creator classes for implementation, which is better if combined with the factory mode.
Easy to learn design mode Reading Notes (6)-creator Mode