23:21, I was originally prepared to do other, but where there is time to play.
Continue 鼹 鼹 reading
Factory method mode is a class creation mode, called a Virtual Constructor mode or polymorphic factory mode.
Factory method mode is intended to define a factory interface that creates a product object, delays actual creation work into subclasses. This is just the shortcomings of simple factory models, and solve it in theory.
Note: Factory model has simple factory models, factory method patterns and abstract factory models. For the shortcomings of simple factory model, you can think of why this factory method is modeled.
Parallel level structure, in a system design, often having a product role, then there is a factory role. In the case where the factory method mode can be applied, there will be a grade structure of a product, consisting of one (or even) abstract products and multiple specific products. When the factory method mode is applied to this system, a commonly used practice is to design a factory grade structure of the same structure according to the grade structure of the product. The corresponding product role is then created by the corresponding plant role. The factory method does not limit the level of product grade structure. In the general book, there are two levels as an example, the first layer is an abstract product layer, and the second layer is the specific product layer. But in practical applications, the products often have a more complex level.
Schematic view:
It is not difficult to see the concept of factory method mode by schematic, plus the following code:
public class Client {private static Creator creator1, creator2; private static Product prod1, prod2; public static void main (String args []) {// creator1 dedicated to the production of Product creator1 = new ConcreteCreator1 (); // prod1 belong Which specific product is to see the specific implementation of Creator1, Prod1 = Creator1.Factory (); Creator2 = New Concretecreator2 (); prod2 = creator2.factory ();}}
Ok, we propose and the same problems in front. The farm used the farm is a simple factory model, there is a whole-term gardener role to control all crops, grow and harvest. Now the scale of this farm has become bigger, while the management is more professional. The past all-around characters are not, each crop has special gardens management, forming scale and professional growth.
Then, according to the example map, we can first determine the hierarchy of the plant and then derive the hierarchical structure of the plant according to the hierarchy of the plant. Subclasses in the factory interface are the operation of each of the respective plants.
Implementation of a factory method pattern relies on the polymorphism of factory roles and product roles. In some cases, this mode can be degraded, and its feature is the loss of polymorphism.
Factory methods do not have to put back a new object every time. But the object it returns must be created by himself, not an external object, and then passed to the factory object.
The factory method returns should be an abstract type, not the specific type, only in this way to ensure the polymorphism of the product. In other words, the client to call the factory method can depends on an abstract product type for abstract programming, not a specific product type. In special cases, the factory method only returns a specific product type. At this time, the function of the factory method is degraded, and it is characterized by the loss of the polymorphism of the product role. In other words, the static type of the client from the factory method can know what type of object will be, and violates the factory Method mode is used. Take a look at the following code, think about some questions about factory method model:
Import java.net. *; import java.io. *; public class urlconnectionreader {public static void main (string args []) throws exception {url yahoo = new url ("http://www.yahoo.com"); URLConnection yc = yahoo.openConnection (); BufferedReader in = new BufferedReader (new InputStreamReader (yc.getInputStream ())); String inputLine; while (! (inputLine = in.readLine ()) = null) {System.out.println INPUTLINE);} in.close ();
鼹 鼹 is written in Xi'an
2005-1-17