Objective
In the programming process summed up a programming experience, so that the formation of design ideas called design patterns.
There are 23 types of design patterns. It is suitable for all programming languages.
Commonly used innovative design patterns: Simple factory, abstract factory and singleton mode, behavioral design pattern: template design mode, observer mode and command mode, Structural design pattern: Adapter design mode, proxy mode (static and dynamic, Typical for use in spring AOP programming) and decorator design patterns.
Body Singleton mode (singleton)
Ensure that a class can only create one instance in memory.
1. Implementation steps:
1) Privatization of the constructor, i.e. using the private modifier constructor
2) provides a static private property that is of type object type to be created.
3) provides a static method that returns objects created inside the class.
Note:
1. A full-Chinese singleton pattern is to create an object when the outside world first requests an object.
Scenario: objects are relatively small and the creation process consumes less time.
A full-Chinese style is a typical time to exchange space.
Full-Han code example:
2. The A hungry man type is when the program is started and the object is immediately loaded into memory.
Scenario: objects are larger and the creation process consumes more time.
A full-Chinese style is a typical space exchange for time.
A hungry Man-code example:
Simple Factory
Provides the product to the caller to encapsulate the object's creation process.
Use the factory-encapsulated object creation process.
1. Implementation steps:
1) Define the interface specification.
This interface is an interface that must be implemented by objects created in the factory.
When defining the method of the interface, avoid the characteristics of the system technology, such as: for database operation, avoid adapting SqlException in the interface.
2) defining the implementation class
The class is a template for the objects that the factory needs to create, implementing the defined interfaces.
3) Building a factory: Using abstract classes
A static method is provided in the factory class, and the return value type of the method is the interface type used to produce various objects in the dynamic binding factory.
2. Application Scenario:
Different objects are produced according to the different needs of the user, and the objects are of the same type and have the same behavior
3. Role
Can adapt to the different needs of different users, according to different needs to produce the corresponding objects, to achieve the purpose of the creation of the shielding object.
4. Steps to implement simple plant-to-radiation use
1) Define the interface
2) Provide the specific implementation class
3) Provision of configuration files
Basic information for the configuration class: Key= "class's Fully qualified name"
4) Provide the implementation of the factory class
According to the configuration file information, through the external key, to obtain the corresponding fully qualified name.
code example:
Abstract Factory
Abstract factories are used for the realization of different products, such as some easy-to-exchange product families, they can only change the specific factory.
For example, a database that may have MySQL in development and a database for Oracle.
1. Specific example steps:
1) Define the interface
Define methods for accessing the database
Iorderdao-->addorder method//user access mode
Iuserdao-->adduser method////How orders are accessed
Note:
Access to the database is independent of the database product and is business-only.
2) Provide the implementation of the interface: abstract product implementation of the specific classification
Different database products He has different ways of doing things.
1)) Implementation for Oracle
Oracleuserdaoimpl Implements Iuserdao
Oracleorderdaoimpl Implements Iorderdao
2)) for MySQL implementation Mysqluserdaoimpl implements Iuserdao
Mysqlorderdaoimpl Implements Iorderda
3) provide abstract factory class
The class should contain all the abstract methods of the factory that created the database product
4) Factories that provide databases for different database products
5) Test: Typically create an instance of the Concretefactory class at run time
Daofactory daofacotry=daofactory.getinstance ("MySQL");
Userdao Userdao=daofactory.getuserdao ();Note:Create a specific factory, and then create a product object with a specific implementation. That is, in order to create different product objects, the client should use a different specific factory.
Template Design Patterns (templates)
Define the skeleton of the operation in a class, and give the details to the subclass to complete
Development steps:
1. Define an abstract class
|-Define the skeleton of the algorithm (semi-finished products implemented by the method)
|-Defines an abstract method that is implemented by an instance method invocation in an abstract class at the same time as a subclass.
Application Scenarios:
Update and delete of JDBC operations database
Principle:
Subclasses overriding methods of the parent class
Define algorithm framework in one method, some steps are deferred to subclasses. The template method design pattern allows subclasses to define certain steps of the algorithm without altering the structure of the algorithm.
code example:
2) skeleton of abstract class
1) Some steps for subclasses to implement a skeleton
Decorator design pattern (decorator)
1. Intent to create new changes at run time by combining operations. Input and output in IO stream is a typical case
The adorner dynamically adds responsibility to the object. Adorners provide a more resilient solution than inheritance.
code example:
2. The decorator can add its behavior before or after the object being entrusted in order to complete the remaining tasks.
Of course, its disadvantage is caused by a large number of small classes
3. Principle: The adorner provides decorative material for the decoration device.
Example: a node file stream is an adorner, the file stream gets the data to populate the buffer pool in a buffered stream, and the data in the file is decorated with the buffer pool.
code example:
1) class structure
2) Person Interface
3) man realizes the
4) Decorator Adapter class
5) Sub-class Deca of the adapter
6) Sub-class DECB of the adapter
7) Test class
Adapter design mode (adapter)
Some support or adapter words are often used to represent the class of the adapter.
The design pattern was applied to event listeners in the JDK's swing.
1. Working together with classes that were originally incompatible with the interface
code example: incompatible interfaces for Class A and Class B, examples of working together
1) interface of Class A
2) A class of code3) Class B code4) Test class
2. Class of Adapter
* |-Initialize some resources and provide them directly to the sub-class using
* |-subclasses can only care about functions, what functions need to be implemented to rewrite what method
code example:
1) The interface contains a number of methods
2) by inheriting the class that implements the interface, to solve the problem that all interface methods must be rewritten3) Subclasses can only care about functions, what functions need to be implemented to override what methods
Summarize
1. Briefly describe the other design patterns:
1) State design mode: Allows an object to change its behavior when its internal state changes. Reaching an object looks like the effect of modifying its class.
2) Policy mode: Define a set of algorithm families that encapsulate each algorithm and make it interchangeable. The policy model allows the customer to independently respond to the changes in the algorithm.
3) Observer pattern: Define a one-to-many relationship, and when an object changes, all dependents are automatically notified and updated. Its application in the JDK has JavaBean and swing.