Java design pattern Factory

zhaozj2021-02-16  65

Definition: Provide the interface to create an object. Why use the factory model is our most common mode, the famous JIVE Forum, in a large number of factory models, factory model can be said to be everywhere in the Java program system. Why is the factory model so common? Because the plant mode is equivalent to the new instance object, we often generate instance objects according to class Class, such as A a = new a () factory model is also used to create instance objects, so you will have multiple minds when New. Can I consider practical factory models, although doing this, may do more work, but will give you more scalability and minimal modifications. We use class SAMPLE as an example. If we want to create sample objects: sample sample = new sample (); however, the actual situation is usually, we must do point initialization when you create a Sample instance, such as assignment query database, etc. . First, we think that you can use the Sample constructor, which generates an instance to write: sample sample = new sample (parameter); however, if you create a Sample instance, the initialization work does not like to assign a simple matter, possibly It is a long code. If you also write into the constructor, then your code is ugly (just need REFAACTOR reunion). Why is it difficult to say that beginners may not have this feeling, we analyze the following, if it is a long code, it means that there are many work to do, put a lot of work into a method, equivalent to putting a lot of eggs In a basket, it is very dangerous, which is also a principle of Java-oriented objects. Object-oriented package (encapsulation) and dispatch tell us, try to assign a long code to a "cutting" into each section, will each Segment is then "package" (reducing the coupling between segments and segments), which will disperse the risk, if you need to modify, as long as you change each paragraph, you will not happen. In this example, first, we need to separate the work of the creation instance and the use of the instance, that is, let the large amount of initialization required for the creation instance from separating from the Sample constructor. At this time, we need Factory factory model to generate an object, and you cannot use the simple new sample (parameter) above. Also, if Sample has inherited as mysample, we need to be abstracted as an interface as an interface. Now Sample is interface, there are two subclass mysample and hissample. We have to instantiate them, as follows: Sample MySample = New mysample (); sample hissample = new hissample (); as the project is deeply in-depth, Sample may "give birth to a lot of son", then we have to instantiate these sons, even worse, maybe The previous code is modified: Join the instance of the son later. This is unavoidable in the traditional program. But if you have a conscious use of factory model, these troublesome will create a special production Sample Examples of factories: public class factory {public static Sample Creator (int which) {// getClass generates SAMPLE to be loaded with a dynamic class loading.

IF (Which == 1) Return New Samplea (); Else If (Which == 2) Return New Sampleb ();}} So in your program, if you want to instantiate Sample. Use Sample Samplea = Factory. Creator (1); This, the entire number of packages is not involved in the entire number of packages, and the opportunity to reduce the error modification is reduced. This principle can be used very popular: it is a specific thing, the more Easy to be wrong. This person who has made specific work is deeply appreciated. In contrast, the higher the official, the more abstract, the more abstract, the less like error. It seems to be from the programming. Can you understand the truth of life? Oh. Use the factory method to pay attention to several characters, first you have to define the product interface, such as the SAMPLE above, the product interface is implemented, such as Samplea, followed by a Factory class, with To generate the product Sample, further complicate, it is to expand at the factory class, and the factory class also inherits its realization CONCRETEFACTEFACTefactory. Abstract factory factory models: Factory Method Abstract Factory (these two models are the need to create the complexity of the object. If we create an object's approach, if we create an object Sample, if we have new product interface Sample2. Sample: Sample has two Concrete class Samplea and Samleb, and Sample2 has two Concrete class SAMPLE2A and SAMPLEB2, then we will turn Factory in the above example into an abstract class, in the abstract class, different parts use subclass implementation, the following is to expand Factory in the previous example into an abstract factory: public Abstract class Factory {public abstract Sample creator (); public abstract Sample2 creator (String name);} public class SimpleFactory extends Factory {public Sample creator () {......... return new SampleA} public Sample2 creator (String Name) {......... Return New Sample2a}} PUBLIC CLASS BOMBFACTORY EXTENDS FACTORY {Public Sample Creator () {... Return New Sampleb} PUBLIC SAMPLE2 CREATOR (STRING NAME) {... ... Return New Sample2B}} From above, the two plants have produced a set of Sample and Sample2, maybe you will have questions, why can't I use two factory methods to produce SAMPLE and SAMPLE2? Abstract plants have additional A key point is because there is a certain connection between the SimpleFactory, the production of Sample and the production of SAMPLE2, so it is necessary to bundle these two methods in a class. This factory has its own characteristics, perhaps the manufacturing process is unified. For example, the manufacturing process is relatively simple, so the name is SimpleFactory.

In practical applications, the factory method is used more, and it is a combination of dynamic loaders. For example, we will discuss it in the previous Singleton mode in the previous Singleton mode. Now discuss its factory pattern: 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;} ////////> < Factory == null) {... try {// dynamically reprinted class class c = class.Forname (classname); factory = (forumfactory) c.newinstance ();} catch (exception e) {Return NULL; }}}} // now, return Proxy. To limit the authorization to the Forum access Return New ForumFactoryProxy (Authorization, Factory, Factory.getPermissions (Au thorization));} // method to create a true forum to accomplish by subclasses inherit forumfactory of public abstract Forum createForum (String name, String description) throws UnauthorizedException, ForumAlreadyExistsException;. ....} Because now Jive is through a database system Store content data such as forum posts, if you want to change to implement through file system, this factory method ForumFactory provides a dynamic interface: private static string classname = "com.jivesoftware.forum.database.dbforumfactory";

转载请注明原文地址:https://www.9cbs.com/read-15865.html

New Post(0)