Java basic knowledge of design patterns - the factory pattern

Java Design Patterns - Factory Mode

Disclaimer: This article is finishing according to Mu-class network Buqi teacher quality courses: Mu class network

 What is design patterns (Design Pattern)?

  Design patterns are set to be repeated use, known to most people, after cataloging, code design experience summary.

Using the factory pattern under what circumstances?

  There is a need to create a group of similar objects.

  Which you can not foresee the need to instantiate the class is created when encoding.

  The system needs to consider scalability, should not depend on how the product class instance is created, a combination of details and expression.

Classification of the factory pattern?

  Abstract factory pattern and factory pattern

Factory model common scenarios?

  JDBC implementation, spring frame instantiate the Bean.

Factory pattern to achieve what we have help?

  The system may introduce new products without modifying the specific role of the factory

  The client does not have to care about how the object is created, defined the responsibilities

  A better understanding of the principles of object-oriented, programming to interfaces, rather than program-oriented implementation

By way of example a factory pattern to a deep understanding of the application factory pattern

  1. First, create an interface, this interface is the base interface for all specific functions

  

. 1  public  interface HairInterface {
 2      / ** 
. 3       * interface methods defined
 . 4       * / 
. 5      public  void Draw ();
 . 6 }

  2. Create three to implement the interface

  

1 public class RightHair implements HairInterface {
2     @Override
3     public void draw() {
4         System.out.println("-------右偏分发型-----");
5     }
6 }

  

1 public class LeftHair implements HairInterface {
2     @Override
3     public void draw() {
4         System.out.println("-------左偏分发型------");
5     }
6 }

  

1 public class InHair implements HairInterface {
2     @Override
3     public void draw() {
4         System.out.println("-------中分发型-----");
5     }
6 }

  3. Create a configuration file to determine the mapping between

 

 

  4. Create reads the configuration file of utility classes

  

 1 public class PropertiesReader {
 2     public Map<String, String> getProperties() {
 3         Properties props = new Properties();
 4         Map<String, String> map = new HashMap<String, String>();
 5         try {
 6             InputStream in = getClass().getResourceAsStream("type.properties");
 7             props.load(in);
 8             Enumeration en = props.propertyNames();
 9             while (en.hasMoreElements()) {
10                 String key = (String) en.nextElement();
11                 String property = props.getProperty(key);
12                 map.put(key, property);
13                 // System.out.println(key + " " + property);
14             }
15         } catch (Exception e) {
16             e.printStackTrace();
17         }
18         return map;
19     }
20 }

  5. Create a factory method, for instance of the class of

  

. 1  public  class HairFactory {
 2      / ** 
. 3       * to create objects according to the type
 . 4       * @Description: the TODO
 . 5       * @param Key
 . 6       * @return 
. 7       * @returnType: HairInterface
 . 8       * / 
. 9      public HairInterface getHair (String Key) {
 10          the try {
 . 11              // acquired by reading a properties file based configuration tool 
12 is              the Map <String, String> Map = new new PropertiesReader () getProperties ();.
 13 is              // get the path according to a mapping class 
14             HairInterface hairInterface = (HairInterface) Class.forName(map.get(key)).newInstance();
15             if(hairInterface!=null){
16                 return hairInterface;
17             }
18         } catch (InstantiationException e) {
19             e.printStackTrace();
20         } catch (IllegalAccessException e) {
21             e.printStackTrace();
22         } catch (ClassNotFoundException e) {
23             e.printStackTrace();
24         }
25         return null;
26     }
27 }

  6. write the test class, functional realization

  

public  class the Test {
     public  static  void main (String [] args) {
         // create a factory class instance 
        HairFactory Factory = new new HairFactory ();
         // get specific example of a profile information 
        HairInterface InterfaceImpl = factory.getHair ( "in" );
         IF (InterfaceImpl! = null )
            interfaceImpl.draw();
    }
}

  7. The test results, a successful example of the object implementation

 

Guess you like

Origin www.cnblogs.com/wk-missQ1/p/12304521.html
Recommended