Design Patterns simple factory pattern, factory pattern, abstract factory pattern

Introduction

Intent: Defines a created object's interface, let subclasses decide which instance of a class factory, the factory model so that the process of creating the delay to subclasses.

Mainly to solve: the main problem of interface options.

When to use: When you create different instances in different conditions we definitely plan.

How to solve: let subclasses to implement the factory interface, it is an abstract of the returned product.

The key code: the creation process is executed in its subclasses.

Application examples:  1, you need a car, you can pick up directly from inside the factory, the car is how to do it, and this particular implementation inside the car without having tube. 2, Hibernate change just to change the database dialect and driving can be.

Advantages:  1, a caller wanted to create an object, just know that the name on it. 2, high scalability, if you want to add a product, as long as the expansion of a factory class can be. 3, concrete realization of shielding products, the caller only concerned with the interface of the product.

Disadvantages: Each time a product increases, increasing the need to achieve a concrete factory classes and objects, such that the number of classes in the system increases exponentially, increasing the complexity of the system to some extent, but also increases the system specific classes rely. This is not a good thing.

Usage scenarios:  1, Logger: record may be recorded to your local hard disk, system events, such as a remote server, the user can choose to log somewhere. 2, database access, when users do not know what kind of final system uses a database, and the database may have changes. 3, design a framework for connection to the server, you need three protocol, "POP3", "IMAP" , "HTTP", can these three products as a class, implement a common interface.

Note: As a class to create a pattern in any place need to generate complex objects, you can use the factory method pattern. Local One thing to note is that complex objects suitable for factory mode, and simple objects, in particular, need only to complete the objects created by new, without the use of the factory model. If the factory mode, it is necessary to introduce a factory class, it will increase the complexity of the system.

Simple factory implementation

Code Display

Package com.example.factory; 

// create interface objects 
public  interface Arithmetic {
     void Test ( int A, int B); 
}
Package com.example.factory; 

// implementation class subtractor 
public  class subTest the implements Arithmetic { 
    @Override 
    public  void Test ( int A, int B) { 
        System.out.println ( "addition result:" + (A- B)); 
    } 
}
Package com.example.factory; 

// implementation class adder 
public  class SumTest the implements Arithmetic { 
    @Override 
    public  void Test ( int A, int B) { 
        System.out.println ( "addition result:" + (A + B)); 
    } 
}
package com.example.factory;

//工厂
public class ProductFactory {
    public static Arithmetic getObj(String eq){
        if("sum".equals(eq)){
            return new SumTest();
        }else if("sub".equals(eq)){
            return new SubTest();
        }else{
            new Exception("错误");
            return null;
        }
    }
}
package com.example.factory;

//测试类
public class Test { public static void main(String[] args) { Arithmetic sum = ProductFactory.getObj("sum"); Arithmetic sub = ProductFactory.getObj("sub"); sum.test(1,2); sub.test(1,2); } }
Addition result: 3 
addition result: -1

 

Guess you like

Origin www.cnblogs.com/tysl/p/11127954.html