05, the factory model - a simple factory pattern (static factory mode)

Factory pattern comprising a plurality of forms, each of different forms corresponding to actual conditions, particularly the following three categories:
Simple factory pattern simple factory
Factory Method pattern Factory method
Abstract factory pattern abstract factory
The purpose of using the factory pattern :
Examples of transfer to the client code from the non-client object code - that is, to achieve decoupling of the client and service side
If seen in public static void main client code, factory class model says that in the main process, if you want to use a class A or Class B, then A is not visible in the new main () method or a new B () code, etc., but in the creation of these class factory class can see.

Simple factory pattern (simple factory)  belongs to the class creation mode, also known as static factory pattern (static factory).
Use simple factory pattern, in the actual environment in general, there are four participants (role), respectively:
Factory class roles: the role of the caller direct product
Abstract Product role: an interface or an abstract class that defines the role of responsible for specific products, and interaction with the client.
Role of specific products: the objects created by the factory, but also the client practice target
Client: Call instance is generated factory class, and calls the corresponding working example of a method
Comprising the following Examples:
    
  1. package com.yp;
  2. /**
  3. * 定义 People 抽象类
  4. * @author Administrator
  5. */
  6. public abstract class People{
  7. abstract void say();
  8. }
     
  1. package com.yp;
  2. public class Chinese extends People {
  3. void say() {
  4. System.out.println("说汉语");
  5. }
  6. }

    
  1. package com.yp;
  2. public class American extends People {
  3. void say() {
  4. System.out.println("Speak in English");
  5. }
  6. }
     
  1. package com.yp;
  2. public class PeopleFactory {
  3. public static People create(int type) {
  4. People p = null;
  5. if (1 == type) {
  6. p = new Chinese();
  7. } else if (2 == type) {
  8. p = new American();
  9. }
  10. return p;
  11. }
  12. }
The last test class, also the client code:
    
  1. package com.yp;
  2. public class t {
  3. public static void main(String[] args) {
  4. People p = PeopleFactory.create(1);
  5. People p2 = PeopleFactory.create(2);
  6. p.say();
  7. p2.say();
  8. }
  9. }
Execution results are as follows:


analysis:
In this mode, the role of the client, can only see the abstract factory class role and the role of products, and the role of product realization is invisible.

This mode of disadvantages :
If a new class in the client, take the example above, the increase was added to the British, the French, the new addition to the actual class to inherit People, factory class must also be adjusted because the current factory class matching responsible for mapping between the foreground and the actual value of the incoming class.








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

Guess you like

Origin blog.csdn.net/weixin_34137799/article/details/92083698