Factory mode (Factory) is a more common mode I think is a very easy to understand a model, for a simple example, we often test when we were young, every time the most feared is the parents asked the test paper scores, different scores of our mood is not the same. Then we will write a Java program in Factory mode based on this situation.
public class Factory { public static Student_test creator (String rank) { if (rank.equals ("excellent")) { return new Student_laugh (); } else{ return new Student_cry (); } }
Here we set up a factory class according to the students to get different results, generate the student's different emotions of the reaction class. A lot of people think that I directly based on the value of the new Student () class is not good, yes ah this logic is OK, but if the initialization work done when creating the Student instance is not as simple as assignment, we all know that our hard-pressed scores are played by our teachers, is going through a long process of marking, which may be a long code, and if it is written into a constructor, then our code is very difficult to read (we need to refactor the reorganization).
Why the code is hard to read, most people may not have this feeling at first, I just started to think so. But we analyze, initialization work if it is a long code, the work to do a lot, put a lot of work into a method, the equivalent of putting a lot of eggs in a basket, it is very dangerous, it is back to the Java object-oriented principle, object-oriented encapsulation (encapsulation) and dispatch (delegation) tells us, as far as possible to the long code allocation "cut" into each paragraph, and then "encapsulated" each paragraph (reduce the coupling between segments and segments), so that the risk will be dispersed, if need to modify later, as long as the change in each paragraph, no longer take a move hundred things.
In this case, first, we need to separate the work of creating an instance from the work of using an instance, which means that the amount of initialization required to create an instance is separated from the student constructor.
In the construction of the factory class, in addition to the above factory class factory, there are abstract class student
Public abstract class Student {}
the subclass Laugh,cry class of student inherits the abstract class student common composition.
Let's take a look at the abstract factory model, which differs in the complexity of creating objects. If the method of creating an object becomes complex, as the above factory method is to create an object student_test, if the student still has the general not examination state Student_normal, what to do! We continue to the above analogy, a home analogy into a factory, their family has two children, two children still have to go to test it, there will be test results, but although it is a father and mother born, for the performance of the score is not the same bar. Following the above scenario, we are writing an abstract factory model.
Public abstract class Factory{public abstract Student_normal creator ();p ublic abstract Student_test Creator (String name) ;} public class Simplefactory extends Factory{public Student_normal creator () {.... return new Studenta}public Student_ Test Creator (String name) {..... return new Studenta_laugh|cry }}public class Bombfactory extends Factory{public Student_normal Creator () {.... return new Studentb}public Student_test creator (String name) {... return new studentb_ Laugh|cry}}}
From the above code analysis, we have a total of two abstract creator class Student_normal and Student_test class, representing the two states of ordinary students, examinations and not examination of the state, and each of the abstract class model below There are just said that two children, Student_ Under normal class Studenta, STUDENTB, and so on. So why don't we just build two factories right now, because they're linked to each other, and we're going to encapsulate the common part in an abstract class, using subclasses for different parts.
Abstract factory model more than ordinary factory model of a class of roles [abstract Factory class], the need for ordinary factory class to inherit the abstract factory class to achieve, abstract factory class encapsulated the production of goods common method, ordinary factory class is dedicated to its single class of specific product production.