16. Design Patterns

Introduction 16.1 Design Patterns   

      1) design pattern is a programmer in the face of similar software engineering problems summed up useful experience, mode [design thinking] is not a code, but certain problems of common solutions, design patterns (Design pattern) on behalf of the best practice. These solutions are numerous software developers for quite some time through trial and error summed up

      Nature 2) design patterns to improve maintenance, versatility and scalability of the software, and reduce the complexity of software 

      3) << >> design pattern is a classic, the author is Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides Design (commonly known as the "Gang of Four GOF") 

      4) design pattern is not limited to a particular language, java, php, c ++ has a design pattern

16.2 Types of design patterns 

      Design patterns are divided into three types, a total of 23 kinds

      1) Create a schema: Singleton pattern, abstract factory pattern, the builder pattern, factory pattern, prototype model

      2) structural model: adapter mode, bridge mode, decorative patterns, combined mode, the appearance model, Flyweight, proxy mode

      3) type of model: template method pattern, command mode, the iterator pattern, observer pattern, intermediary model, the memo mode, the interpreter mode (Interpreter mode), state pattern, strategy pattern, duty chain (Chain of Responsibility pattern) , visitor pattern

16.3 Simple Factory

  16.3.1 Basic introduction 

      1) Simple factory pattern belongs create schema, but does not belong to one of 23 kinds of GOF design patterns. Simple factory pattern is determined by a factory object to create an instance of what kind of product class. Simple factory pattern is the factory model family is the most simple and practical mode

      2) simple factory pattern: creating a definition of object classes, the instance of the object class to encapsulate behavior (code)

      3) In software development, when we use a lot of create some kind, some kind, or a batch of objects, it will use the factory pattern  

16.4 Introduction factory method pattern 

      Factory Method pattern: a method of creating an abstract definition of the object is determined to be instantiated by subclasses. Factory method instantiate the object model will be postponed until the subclasses

16.5 abstract factory pattern 

  16.5.1 Basic introduction 

      1) abstract factory pattern: a definition of related trait for creating dependency objects or cluster, without having to specify a particular class

      2) abstract factory model can be simple factory mode and integration mode factory method

      3) From the design perspective, the abstract factory pattern is simple factory pattern of improvement (or called for further abstraction)

      4) The plant abstracted into two layers, AbsFactory (abstract factory) and plant specific implementation subclasses. Programmers can use the corresponding sub-class factory to create objects according to the type, such a simple single plant into a plant-based cluster, and more conducive to the maintenance of the spreading code

Summary 16.6 factory mode 

      1) the significance of the factory pattern: the code will instantiate the object is extracted, placed in a class unified management and maintenance, to achieve decoupling and dependencies of the main project. Thereby enhancing the expansion and maintenance of the project

      2) reliance on abstract principles of design patterns

        - When you create an object instance, not to direct new class, but this new class of action in a factory method, and return. There is also the book says, the variables do not hold a direct reference to specific classes

        - Do not let the class inherits concrete class, but inherits an abstract class or trait (Interface)

        - do not override methods in the base class has been achieved

16.7 singleton

  16.7.1 Basic introduction 

      Singleton mode is: to ensure that the entire software system, an object instance can only be one class

  Application scenario 16.7.2 Singleton pattern  

      Such as Hibernate's SessionFactory, it acts as a proxy data storage source, and is responsible for creating Session objects. SessionFactory not lightweight, under normal circumstances, a project requires only a SessionFactory enough, then it will use the Singleton pattern

  Example 16.7.3 Single Mode - Lanhan formula 

object boke_demo01 {

  def main(args: Array[String]): Unit = {
    val instance1 = SingleTon.getInstance
    val instance2 = SingleTon.getInstance
    if (instance1 == instance2) {
      println("相等")
    }
  }
}

//将SingleTon的构造方法私有化
class SingleTon private() {}

//懒汉式
//看底层
/*
  public SingleTon getInstance() {
    if (s() == null) {
      s_$eq(new SingleTon());
    }
    return s();
  }
 */
object SingleTon { //SingleTon$
  private var s: SingleTon = null

  def getInstance = {
    if (s == null) {
      s = new SingleTon
    }
    s
  }
}

  Example 16.7.4 Single Mode - starving formula 

{boke_demo01 Object 

  DEF main (args: the Array [String]): Unit = { 
    Val instance1 = SingleTon2.getInstance 
    Val SingleTon2.getInstance instance2 = 
    IF (instance1 == instance2) { 
      the println ( "! equal") 
    } 
  } 
} 


// will Singleton privatization constructor 
class SingleTon2 Private () {} 

// starving formula 
// see bottom 
/ * 
public SingleTon2 the getInstance () { 
    return S (); 
  } 
 * / 
Object SingleTon2 Singleton $ {// 
  Private S Val: SingleTon2 new new SingleTon2 = 

  DEF the getInstance = { 
    S 
  } 
}

16.8 decorator pattern 

  16.8.1 decorator pattern principle 

      1) the decorator pattern is like a courier package

        - Body: such as: ceramics, clothing (Component)

        - Packaging: For example: newspapers filled with plastic foam, cardboard, wood (Decorator)

      2) Component 

      3) ConcreteComponent和Decorator

        ConcreteComponent: specific subjects, such as single coffee

      4) Decorator: decorators, such as various spices

  16.8.2 decorator pattern definitions 

      1) Decorator: dynamic new functionality attached to the object, the object function expansion, it is more flexible than inheritance (recursive), the decorator pattern also reflects the principle of opening and closing (ocp)   

16.9 Observer pattern 

  16.9.1 observer mode principle 

      - Observer pattern similar set milk business

        1) milk station: Subject

        2) User: Observer

      -Subject: registration, removal and notification

        1) registerObserver registration

        2) removeObserver removed

        3) notifyObservers () notify all registered users, according to different needs, you can update the data, allowing users to take, it could be implemented to push, look at the specific needs of a given

        

      -Observer: receiving input 

        

      - Observer pattern: Many dependencies between objects one design, the dependent objects of Subject, object dependent Observer, the Subject Observer change notification, such as a station where milk Subject, one is 1. Observer is a multi-party user

16.10 agent mode (Proxy) 

  Basic introduction 16.10.1 agency model  

      1) proxy mode: provide a substitute for an object to control access to this object

      2) the proxy object may be a remote object, creating a large overhead object or objects need security control (Dynamic Proxy)

      3) proxy mode in different forms (such as a remote agent, the agent static, dynamic proxies), are subject to control access and management

  16.10.2 Dynamic Proxy 

      - Dynamic Agent: Forward create a dynamic proxy class (an object), and method call to the specified class (an object) is running

      - Figure dynamic proxy invocation mechanism

      

      1) Proxy and InvocationHandler combination acts as a proxy role

      2) RealSubject is a real object that implements the interface Subject

      3) In use, we do not want direct access RealSubject objects, such as: We have access to this object is controlled

      4) We use the dynamic proxy to create dynamic RealSubject in the program by proxy, and complete calls

      5) Dynamic proxies as needed, to create a variety of combinations

      6) Proxy will implement the interface method Subject, therefore, to use Proxy + Invocation can complete the dynamic of calls to RealSubject

      7) But by calling RealSubject Proxy method is successful, is controlled by the InvocationHandler (In fact, here is the protection agent)

      8) to understand: to create a real alternative proxy object called object, using reflection to achieve control

  16.10.3 several common proxy mode Introduction - Several variants 

      1) firewall proxy

        Within the network through a proxy through the firewall, enabling access to the public network.

      2) caching proxy

        For example: When requesting image files and other resources, first caching proxy to take, if resources are to take ok, if not get the resources, then take the public network or database and cache

      3) Static Proxy

        Static agents commonly used for expansion of existing business logic. For example, a class held by the second party package, and calls some of these methods. Such as logging, printing work. You can create a proxy class that implements the method of the second party and the same method, by having the proxy class to hold the real object, call the proxy class method to achieve the purpose of increasing the business logic

      4) Cglib

        Use cglib [Code Generation Library] dynamic agent, does not require the delegate class must implement the interface, using the underlying framework asm generated bytecode generated proxy class bytecode

      5) Synchronization Agent

        Mainly used in multi-threaded programming, a complete synchronization between multiple threads

 

  

Guess you like

Origin www.cnblogs.com/zhanghuicheng/p/10890129.html