06, factory mode - Factory Method pattern

Factory method pattern than in front of a simple factory pattern learning.
In simple factory it has been said, and there is a simple factory pattern drawbacks:
If I want to expand the system, you need to add the product class, in addition to a new product category, but also need to adjust the factory class, which does not meet the OCP.
So then look factory method pattern is how to solve this problem.
Still using the example above:
First, the definition of abstract class and create an abstract class People People factory class - tentatively called PeopleCraetor it, Factory this word is no longer reflected. as follows:
   
  1. package com.yp.learn.designmodel;
  2. public abstract class People {
  3. abstract void say();
  4. }
  5. package com.yp.learn.designmodel;
  6. public abstract class PeopleCreator {
  7. public abstract People createPeople();
  8. }
After defining class Chinese, Americans, and these two classes inherit the People category. as follows:
   
  1. package com.yp.learn.designmodel;
  2. public class Chinese extends People {
  3. @Override
  4. void say() {
  5. System.out.println("说汉语");
  6. }
  7. }
  8. package com.yp.learn.designmodel;
  9. public class American extends People {
  10. @Override
  11. void say() {
  12. System.out.println("Say American English");
  13. }
  14. }
Then these two classes create factories that generated specifically these two classes of factories, and the factories inherited PeopleCreator class. as follows:
   
  1. package com.yp.learn.designmodel;
  2. public class AmericanCreator extends PeopleCreator {
  3. @Override
  4. public People createPeople() {
  5. // TODO Auto-generated method stub
  6. return new American();
  7. }
  8. }
  9. package com.yp.learn.designmodel;
  10. public class ChineseCreator extends PeopleCreator {
  11. @Override
  12. public People createPeople() {
  13. // TODO Auto-generated method stub
  14. return new Chinese();
  15. }
  16. }
Screenshot the entire list of categories is as follows:

Finally, look at the client code:
   
  1. package com.yp.learn.designmodel;
  2. public class Start {
  3. public static void main(String[] args) {
  4. // TODO Auto-generated method stub
  5. People p ;//定义抽象类变量
  6. PeopleCreator peopleCreator ;//定义抽象类工厂变量
  7. peopleCreator = new ChineseCreator();//依据实际业务而言所调用的实际产品工厂类。
  8. p = peopleCreator.createPeople();
  9. peopleCreator = new AmericanCreator();
  10. p.say();
  11. p = peopleCreator.createPeople();
  12. p.say();
  13. }
  14. }

operation result:


analysis:
Now look at the client, which comes to the class:
1, the abstract class People: This class contains abstract product class method, through such polymorphic features combined with Java, to achieve a variable based on different implementations, different behaviors. The example is implemented twice p, but the result of execution in the same manner but different say.
2, create an abstract factory class People: People with abstract class defines the production plant, such as the example, is responsible for defining abstract class reference to an instance. But to achieve it by its sub-class is completed.
3, create the actual product factory class for generating concrete realization of factory products category.

If I now need to expand the software, you need to add the French, then I need to do the following:
1, add a Franch, inheritance People
2, add a FranchCreator, inheritance PeopleCreator
3, the client code, assigned to the variable peopleCrator adjusted to peopleCreator = FranchCreator ();
The rest do not change.

Now compare, the original business logic that is related to the Chinese people and the factory class and its American factory class code, no change had occurred, and the system's development, but is achieved by way of the new class. This has the drawbacks of simple factory pattern to solve



You may be in doubt:
1, the client code is changed, or does not comply with OCP
A: The client, with the actual demand changes, it is impossible to control the client code change does not occur unless customer demand will never change, therefore, OCP within the meaning of the code is the code for the service layer rather than the client. In order to achieve the same (layer mode business software) status quo (customer needs).
2, client code, to Chinese and Americans were, for example, that is to say, the client needs to know which instance to be, then this time, why not just use the new keyword instance object, but too much trouble?
A: This question is normal, but this time to consider is from software design point of view to look at the problem rather than look up the code implementation. Intent is to decouple the factory model, that is, objects that are created should not be reflected by the client . Because the client concerned, is to use rather than created. But there must be created before use, then the factory method pattern is created to the part of the factory class, client code uses directly related to product category on the line. But it also introduces a factory class created this situation in the client. By comparison, however, is created in the client factory class and create class products at the client, you will find the former more flexibility good.
Learning must pay attention, not to achieve learning code, but the understanding of the design.
3, although compared with the simple factory pattern to solve the OCP, however, a simple factory pattern, only need to create a factory class in the client on the line, and pass parameters by means of factory class, the class of products available, but the factory method pattern , seemingly not such a simple way, but must instantiate an abstract factory class by specifying product factory class, then the client would not still exist to determine which products the factory class in the end choose? In other words, the client code also need to maintain a mapping relationship between the customer and the selected content corresponding to the selected content product category?
A: This problem is the factory method pattern of abuses. Indeed, relatively simple factory pattern, factory method pattern does exist such a problem, either mode is absolutely impossible to eliminate conditional, can only say that the conditional transfer.



to sum up:
1, the static factory pattern to achieve a decoupling, but does not follow the OCP
2, the factory method pattern to achieve the OCP, but the decoupling is not static factory pattern done good

If you want to common solutions for these two problems, it will be realized through the Java reflection technology. This performance is very prominent in the spring.






Reproduced in: https: //my.oschina.net/u/1182369/blog/406569

Guess you like

Origin blog.csdn.net/weixin_34377919/article/details/92083695
Recommended