Principles of design patterns:Programming with excuses
The role of the factory model:
A. In application design, object creation is concentrated in one place or managed by a certain class (spring)
B. You can directly add objects without modifying the application, which is also conducive to object maintenance.
Types of factory models:
A. Simple Factory
B. Factory method
C. Abstract Factory
Wedge:
In other words, a boss in Northeast China has three cars in his house: Mercedes-Benz, BMW, and Lamborghini. He has hired an experienced driver. on different occasions, he will instruct the driver to drive different cars for entertainment.
Simple Factory:
Abstruact:
Package com. Product. Abstruct;
Public interface car {
Public void drive (); // The car is used for driving, so there must be an engine
}
Implements:
Package com. Product. implement;
Import com. Product. Abstruct. CAR;
Public class Audi implements car {
Public void drive () {// BMW, he is also a car, not a horse, but also an engine
// Todo auto-generated method stub
System. Out. println ("Yes, today wo are driving Audi .... ");
}
}
Package com. Product. implement;
Import com. Product. Abstruct. CAR;
Public class Benz implements car {
Public void drive () {// Audi, he is also a car, not Dior, but also an engine.
// Todo auto-generated method stub
System. Out. println ("Yes, today wo are driving Benz .... ");
System. Out. println ("boss, follow your instructions ....");
}
}
===== Lamborghini ====
Factory class:
The factory class is a management class and the direct owner of the automobile. This person is the driver of the boss;
Package com. Product. factory;
Import com. Product. Abstruct. CAR;
Import com. Product. Implement. Audi;
Import com. Product. Implement. Benz;
Public class driver {
Public static car drivecar (string carname) throws exception {
If (carname. inclusignorecase ("Benz ")){
Return New Benz ();
}
If (carname. inclusignorecase ("BMW ")){
Return new BWM ();
}
Return New Audi ();
}
}
One day, the boss was about to go to England to watch Liverpool's home game against Vera. He needed to drive BMW to the airport, so he called the driver and asked him to start BMW at two o'clock P.M. to pick him up...
Package com. Product. test;
Import com. Product. Abstruct. CAR;
Import com. Product. Factory. driver;
Public class test {
Public static void main (string [] ARGs) throws exception {
// Tell the driver which car wo drive todaty Wo will to Amsterdam
Car car = driver. drivecar ("Benz"); // let the driver pick up the car
// Mrs Wang go
Car. Drive (); // one-click STARTUP
}
}
Factory method:
As the boss's business grows, he gets richer and richer, so he has bought several sports cars-Ferrari, Porsche, and Jaguar. As the number of cars increases, all cars must be managed by an old driver. Annual Inspection, insurance, tickets, and car washing are difficult for old drivers, in addition, the boss will instruct the old driver to drive a car of different styles from Monday to Saturday. At this time, the old driver will have to remember which car the boss needs to drive out, so he proposed to the boss: hire a driver for each car, and each car has a dedicated person in charge. When I need to say hello, I will send corresponding personnel to pick up the boss, the boss did not hesitate to say: that's the case .......
Abstract automobile:
Package com. Product. Car. Abstruct;
Public interface car {
Public void drive ();
}
Audi car:
Package com. Product. Car. implement;
Import com. Product. Car. Abstruct. CAR;
Public class audidriver implements car {
Public void drive (){
System. Out. println ("four circles to be an official ");
}
}
Mercedes-Benz:
Package com. Product. Car. implement;
Import com. Product. Car. Abstruct. CAR;
Public class audidriver implements car {
Public void drive (){
System. Out. println ("four circles to be an official ");
}
}
====== Omit other cars ======
Car Manager (old driver)
Package com. Product. Car. Manager;
Import com. Product. Car. Abstruct. CAR;
Public interface carmanager {
Public Car drivecar ();
}
New Audi drivers:
Package com. Product. Car. managerimpl;
Import com. Product. Car. Abstruct. CAR;
Import com. Product. Car. Implement. audidriver;
Import com. Product. Car. Manager. carmanager;
Public class audimanager implements carmanager {
Public Car drivecar (){
Return new audidriver ();
}
}
Newly recruited Mercedes-Benz drivers:
Package com. Product. Car. managerimpl;
Import com. Product. Car. Abstruct. CAR;
Import com. Product. Car. Implement. benzdriver;
Import com. Product. Car. Manager. carmanager;
Public class benzmanager implements carmanager {
Public Car drivecar (){
Return new benzdriver ();
}
}
Boss wants to go to Liverpool one day:
Package com. Product. test;
Import com. Product. Car. Abstruct. CAR;
Import com. Product. Car. Implement. benzdriver;
Import com. Product. Car. Manager. carmanager;
Public class test {
Public static void main (string [] ARGs) throws exception {
// Find the car supervisor and tell him that I want to open a BMW to watch the Liverpool match against Vera today
Carmanager = (carmanager) New benzdriver ();
// The driver in charge of BMW receives a call from the butler to fill the BMW with oil.
Car drivecar = carmanager. drivecar ();
// Fly to Liverpool
Drivecar. Drive ();
}
}
The above is the simple factory and factory method in the factory model. First, we will compare the differences between the two models and their respective advantages,
Programming:
A. Simple factory has no abstract class and only one factory class. The parameters of the object to be created are passed and created by the factory class in a unified manner.
B. Define an interface for creating a product object. This interface plays this core role. It only needs to define the methods required by the subclass and hand over the object to create it.
Advantages and disadvantages:
A. relatively simple factory methods should be more abstract. The advantage of doing so is not to change the role of the factory (not to dismiss the old driver or assign him a shift) you can easily add new members (recruit more drivers );
B. the advantage of a simple factory is that it is simple. You only need to create the corresponding object based on parameters, but the disadvantage is also quite obvious. All objects are created in this class, the created object can only be known in advance (suddenly one day the boss bought a manual transmission Jetta, and the boss must notify the old driver that I bought a Jetta and put it in X ); adding a member object will result in adding methods to the core class, which violates high cohesion. When the number of product classes in the system increases, the factory class may be required to create different instances according to different conditions. this kind of judgment on the condition is intertwined with the judgment on the specific product type, and it is difficult to avoid the spread of module functions, which is very unfavorable for system maintenance and expansion;
Next, how can we reasonably apply the factory design model to the application of the factory model in program design?
A. for a product, the caller knows exactly which factory service should be used, instantiate the specific factory, and produce specific products. The iterator () method in the Java Collection belongs to this situation.
B. only a product is required, and you do not need to know which factory is for production, that is, the decision on which specific factory is used is decided by the producer, they instantiate a specific factory and return it to the user based on the current system, and the decision-making process is transparent to the user..