| Back to logs list
47297 2010 年 03 月 27 日 17:56 Reading (loading. ..) Comments (3) Category: Personal Diary
a, primer
saying that ten years ago, an upstart, his family owns three cars - Benz Mercedes-Benz,
tods men shoes, Bmw BMW,
tods online shop, Audi, Audi, also employed the driver for him to drive. However, the upstart is always funny when the car: the Benz cars, said the driver followed by You must say: This man sick! That car is not directly on the line? !
and when the act of the upstart into our programming comes, will find this is a common phenomenon. Fortunately, this sick phenomenon in OO (object oriented) language could be avoided. The following is based on the Java language to introduce the subject of our paper: the factory pattern.
Second, classification
factory pattern is mainly for the creation of the transitional object provides the interface to the specific process will create an object mask in isolation, to achieve the objective of increased flexibility.
factory pattern in
1) simple factory pattern (Simple Factory)
2) Factory Method Pattern (Factory Method)
3) Abstract Factory (Abstract Factory)
gradually from top to bottom the three abstract models and more general.
GOF in the . The simple factory pattern (Simple Factory) for the factory method model to see a special case, both classified as a class.
both can,
tods shoes online, in this paper using the Let's look at how the factory pattern is to
Third, a simple factory pattern
simple factory pattern, also known as a static factory method patterns. Rename this model can be seen on some very simple. The purpose of its existence is very simple: define a interface for creating objects.
first look at its composition:
1) the role of the factory class: This is the core of this model, with some business logic and decision logic. It is often in the java in the implementation by a specific class.
2) the role of the abstract product: it is generally specific products inherit the parent class or implemented interfaces. In the java interface or an abstract by the class.
3) the role of specific products: the factory class object is created an instance of this role. In the java class by a specific implementation.
to use class diagrams to clearly express the relationship between them under the
simple factory pattern
how to use it?
we have a simple factory pattern to transform the way upstart car - now just need to sit in the car on the upstart driver saying:
/ / abstract product role (interface)
public interface Car {
public void drive ();
}
/ / product-specific roles
public class Benz implements Car {
public void drive () {
System.out.println (
}
}
public class Bmw implements Car {
public void drive () {
System.out.println (
}
}
public class Audi implements Car {
public void drive () {
System.out.println (
}
}
/ / factory class Role
public class Driver {
/ / factory method. Note that the return type for the role of the abstract product
public static Car driverCar (String s) throws Exception {
/ / determine the logic, the role of specific products returned to the Client
if (s.equalsIgnoreCase (
return new Benz ();
else if (s.equalsIgnoreCase (
return new Bmw ();
else if (s.equalsIgnoreCase (
return new Audi (); ;
else throw new Exception ();
}
... ...
}
/ / upstarts playing ......( welcome here in simple English Magnate (wealth) that the nouveau riche, nouveau riche are a variety of claims, such as parvenu, and upstart)
public class Magnate {
public static void main (String [] args) {
try {
/ / tell the driver I am today used Mercedes-Benz
; Car car = Driver.driverCar (/ / The following command: drive! !
car.drive ();
}
... ...
}
}
vacancies in this process other information can be filled after the full run. If you put all the classes in a file, please do not forget that only one class is declared as public. The program is run under jdk1.4 through.
the relationship between the program expression of each class are as follows:
This is a simple factory pattern. How, very simple to use, right? It has brought any good?
First, use a simple factory pattern, our program is not households is).
We
following the principle of opening and closing (open for extension; on the modified closed) up analysis in the simple factory pattern. When the upstart increase a car when they meet the abstract product development contracts, as long as notice that the factory class used to be customers. So part of the product, it is in line with the principle of opening and closing; but the factory sector seems to be less than ideal, because each additional car,
tods shoe, have increased in the factory class to determine the appropriate business logic, or logic, which obviously runs counter to open Closed principle. One can imagine the addition of new products, the factory class is very passive. For such a factory class (in our case is the master for the drivers), we call God Almighty class or category.
our example is the simplest situation, and in practice, it is likely product is a multi-level tree structure. As a simple factory pattern, only a factory class to correspond to these products, so that God may take us tired, too tired of us programmers
Factory Method Pattern
then emerged as a savior.
of factory method model
Factory Method Pattern
simple factory pattern removing the static factory method in the property, so that it can be inherited by subclasses. This focus on the simple factory pattern in the pressure on the factory method can be factory method patterns in different sub-classes to share the facility.
you probably guessed the factory method model structure, its composition under the terms:
1) the role of the abstract factory: This is the factory method model is the core, it has nothing to do with the application. Is the specific role of the factory must implement the interface or must extend the parent class. In java in it from the abstract class or interface to implement.
2) the role of specific plant: it contains and the specific business logic-related code. Called by the application specific products to create the corresponding object.
3) the role of the abstract product: it is the parent of inheritance of specific products or implement. In general there is an abstract class in java or interfaces.
4) the role of specific products: the role of the specific factory object is created an instance of this role. In java by the specific class.
use class diagrams to clearly express the relationship between them under:
factory method abstract factory pattern inherited from the role of using multiple sub-class instead of a simple factory pattern in the As mentioned above, this object will share the pressure; and this makes up the structure to be flexible - when there are new products (that is, an upstart car) is generated, as long as the products according to the abstract roles, provides the role of abstract factory contract to build, it can be used by customers without having to modify any existing code. The structure can be seen that the role of the factory is in line with the principles of open and close!
we old rules, a complete example of using the factory pattern to see how the various roles being coordinated. Words upstart business bigger and bigger,
tods shoes sale, their car is also increasing. This can be hard on the driver master, and what car it must be remembered, maintenance, and must go through him to use! So upstart sympathy, he said: see you were with me on so many years later you do not have so much trouble, I'll assign a few extra, you just manage them on the line! Thus, the management of the factory method pattern emerged. Code is as follows:
/ / abstract product role, the role of specific products with a simple factory pattern similar to, but a bit more complicated, here a little.
/ / Abstract Factory role
public interface Driver {
public Car driverCar ();
}
public class BenzDriver implements Driver {
public Car driverCar () {
return new Benz ();
}
}
public class BmwDriver implements Driver {
public Car driverCar () {
return new Bmw ();
}
}
/ / should be and the corresponding relationship between the formation of specific products ...
/ / has requested Mr. upstart
public class Magnate {
public static void main (String [] args) {
try {
Driver driver = new BenzDriver ();
Car car = driver.driverCar ();
car.drive ();
}
... ...
}
factory method can be seen to join, so doubling the number of objects. When the product range is very long, there will be a large number of the corresponding factory object, this is not what we want. Because if you can not avoid this situation, consider using a simple factory pattern and factory method model combination to reduce the factory class: that is similar to the type of the product tree (usually the leaves of the tree each other brother's) use of a simple factory mode to achieve.
V. Summary
factory method seems to have a perfect model of the object created for the packaging, allows customers to program only deals with the role of abstract product interfaces provided. That we really have to code around the factory? Need not be. Perhaps in the following cases you can consider using the Factory Method Pattern:
1) When the client does not need to know to use the object creation process.
2) the existence of objects using the client may change, or do not know which one to use a specific object.
simple factory pattern and factory method patterns actually change the code to avoid it?
not!!
● In the simple factory pattern, the new products added to modify the plant to determine the role of the statement;
● mode in the factory method, or to determine the role of logic in the factory to remain in the abstract or the concrete factory in the client role will be coded (as in the example). Conditions and the product object creation factory will inevitably cause changes in the role of changes.
face of this situation, Java's reflection mechanism and the configuration file to limit the ingenious combination of a breakthrough - the perfect embodiment in the Spring out.
six, abstract factory pattern
first to know what is under the product family: in the hierarchical structure of different products, the function of the composition of the associated product family. Let us use an example to explain it vividly.
figure BmwCar and BenzCar tree is two products (product-level structure); and as shown BenzSportsCar and BmwSportsCar is a product family. They can put the family car, so to be associated features. Similarly BmwBussinessCar and BenzSportsCar is a product family.
back to the topic of the abstract factory pattern.
can be said that the abstract factory pattern and factory method patterns of difference is the need to create an object on the complexity. And three inside the abstract factory pattern is the most abstract and most general.
factory pattern is intended to: provide an interface to the client, you can create more than one product family in the product object
the abstract factory and what conditions must satisfy:
1) the system has more than one product family,
tods womens shoes, and the system in which a consumer can only be a family of products.
2) belong to the same family of products with a product to use.
abstract factory pattern to see all the roles (and the same factory method):
1) the role of the abstract factory: This is the factory method model is the core, it has nothing to do with the application. Is the specific role of the factory must implement the interface or must extend the parent class. In java in it from the abstract class or interface to implement.
2) the role of specific plant: it contains and the specific business logic-related code. Called by the application specific products to create the corresponding object. It consists in java in a specific class.
3) the role of the abstract product: it is the parent of inheritance of specific products or implement. In general there is an abstract class in java or interfaces.
4) the role of specific products: the role of the specific factory object is created an instance of this role. In java by the specific class.
class diagram as follows:
seen the first two patterns, this pattern of coordination between the various actors should have in mind the number of cases, and I do not give a concrete example. Just be sure to pay attention to meet the conditions for using the abstract factory pattern, oh.
article is taken from CSDN