Design Model Factory
Abstract: Model Factory
Definition: to provide Create Object interface.
Why?
We factory model is the most commonly used model, the famous Jive Forum, the large-scale use of the factory model.
Why do I say that the factory model is the most commonly used, because the factory to create a model equivalent to the new object. Factory model is used to create the object.
For example, we have a category we want to create Sample Sample targets:
Sample sample = new Sample ();
If we want to do something before the sample in the creation of conditions, for example, assignment, etc., you can use the constructor function Sample:
Sample sample = new Sample (parameters);
If created when the sample is not such as to do such a simple thing assignment may be for a long period of the code, if the function is written into the structure, which obviously runs counter to the principles of object-oriented. Package (Encapsulation) and assigned (Delegation);
We need to create an example of responsibility and the responsibility of examples of the use of separate, making statements
Sample sample = new Sample (parameters);
Responsibility is simple: the use of this example Sample As for the task of creating Sample Factory to the factory model.
Also, if there is a succession Sample as MySample, according oriented programming interface, we need to abstract into a Sample interface.
Sample now is the interface, there are two sub-categories MySample and HisSample we need to instantiate them, as follows:
Sample mysample = new MySample ();
Sample hissample = new HisSample ();
Along with the deepening of the project, Sample may be "many sons by birth," we should be on the son of one of these examples, even worse, may need to revise the code before: Add son was unexpected examples. This is the traditional process can not be avoided.
But if you have a sense of beginning use of the factory model, there will be no trouble.
You will establish a specialized factory production Sample examples:
(Public class Factory
Public static Sample creator () (
….
If (which == 1)
Return new MySample ();
Else if (which == 2)
Return new HisSample ();
)
)
Well, your procedures, if MySample examples of when. Use
Sample sample = Factory.creator ();
This, in the whole Sample is not related to the specific sub-category to package effects, it changes to reduce errors, this principle can be very popular to describe it: that the more concrete things done, the easier it is to Fan wrong. Each of these a specific work done minds of the people, on the contrary, the higher the official has done, the more abstract state of the general, the less possibility of error Fan. for procedures like we can realize the truth of life? Ha ha.
Well, Back, as the inevitable use of factory, then we understand what the factory model.
How to use it?
Factory model: simple Factory method (Factory Method) abstract factory (Abstract Factory).
On the cases, we use a simple plant. These models there is no obvious difference, in my concept, simple plant should be only one factory method, if we object creation methods becoming more complicated, and we may Factory to the cases into abstract categories, will be packaged in the common part of the abstract category, the use of different parts of sub-categories to achieve:
(Public abstract class Factory
Public abstract Sample creator ();
Public abstract Sample2 creator ();
)
Public class SimpleFactory extends Factory (
Public Sample creator () (
……
)
Public Sample2 creator () (
……
)
)
Public class BombFactory extends Factory (
Public Sample creator () (
……
)
Public Sample2 creator () (
……
)
)
Only in the case of an interface Sample products, factory method and abstract factories can create multiple examples of interface products, such as Sample2 Sample3
FactoryMethod Often it is just a creation of a single example. Abstract Factory Group to create a series of examples, these examples are related to each other.
For example 1
The chart is abstract factory plans:
In this map, there are two types of interface products and interface CPU interface RAM; at the same time there are two ways to create: MacProducer and PCProducer, these two methods have created createCPU () and createRAM (), to return to the example of target group is the CPU and RAM, which is different from the two categories of products interface, the surface is related to each other. Therefore it is abstract factory.
For example 2
We Jive the ForumFactory example:
(Public abstract class ForumFactory
Private static Object initLock = new Object ();
Private static String className = "com.jivesoftware.forum.database.DbForumFactory";
Private static ForumFactory factory = null;
Public static ForumFactory getInstance (Authorization authorization) (
/ / If no valid authorization passed in, return null.
If (authorization == null) (
Return null;
)
/ / Use the following single-mode if Singleton (factory == null) (
Synchronized (initLock) (
If (factory == null) (
……
Try (
/ / Dynamic reprint of Class c = Class.forName (className);
Factory = (ForumFactory) c.newInstance ();
)
Catch (Exception e) (
Return null;
)
)
)
)
/ / Now, returning to the proxy. Used to limit the authority of the forum visit return new ForumFactoryProxy (authorization, factory,
Factory.getPermissions (authorization));
)
/ / Create genuine forum by way of inheritance forumfactory subclass to complete.
Public abstract Forum createForum (String name, String description)
Throws UnauthorizedException, ForumAlreadyExistsException;
….
)
It is through the Jive Forum Posts database system, such as content stored data, if someone simply for the need to expand the file system storage forum posts, the factory method ForumFactory provides a dynamic interface:
Private static String className = "com.jivesoftware.forum.database.DbForumFactory";
You can use to create their own development of the alternatives com.jivesoftware.forum.database.DbForumFactory forum can be.
In the above section of code sharing in a three models, in addition to the factory model, there are single-mode Singleton, as well as proxy mode, proxy model is largely used to authorized users on the forum's visit because there are two kinds of people visit forum: A is a registered user is a tourist guest, then it is not appropriate authority, and this authority is throughout the whole system, so the establishment of a proxy, similar to the concept of gateways, this can be achieved very good results.
Well. Simple description of the above, you should have the factory pattern impression of a simple, if you want to delve into the factory model, there are a lot of network information in English, but I also think that there is no need to study too, is the use of practice, the actual use of Many factory model variants, as long as you know that this probably thinking, I believe that in practice you will be creating the design of the factory model master!
↑ Back
Tags: the design pattern






