A professional programmer, always put the code of clarity, compatibility, portability in a very important position. They always define a large number of macros to enhance the clarity and readability of the code without increasing the length of the compiled code and the efficiency of the code; they always encode and take into account future code maintenance and upgrades. Even if you analyze 1% of the code, you will have a deep understanding of what kind of code is written by a professional programmer, what kind of code is written by a hobbyist. And this is something that no one in the real analysis of standard code can appreciate.
This article will introduce some of the classic design pattern ideas:
Proxy mode
Proxy mode: Provides a proxy for other objects to control access to this object.
The method of accessing a Class (object) can be controlled in detail, with pre-processing done before calling this method (the uniform Process code is placed in the agent). Call this method and do a post-processing.
Proxy Mode classification:
1. Static proxies (statically defined proxy classes, our own statically defined proxy classes. For example, we define a star's broker class)
2. Dynamic Proxy (the proxy class is dynamically generated by the program, and the proxy class is not defined by us.) But by the program automatically generated) is more important!!
The dynamic agent that comes with the JDK
Javaassist Byte Code Operation library Implementation
CGLIB
ASM (Low-level usage directives, poor maintainability)
Structure composition
The proxy pattern mainly involves three roles: abstract role, Proxy role, real role (the role being delegated).
Abstract role: A common interface for declaring real objects and proxy objects. That is, the real object and the proxy object together to achieve the behavior of action (like the landlord and intermediary to be able to achieve the behavior of renting, can rent to you).
Proxy role: The proxy role contains a reference to a real role, allowing you to manipulate the real object while the proxy object provides an interface to the real object so that it can replace the real object at all times. At the same time, the proxy object can attach its own operation when performing the operation of the real object, which is equivalent to the encapsulation of the real object.
Real role: The object that the proxy role proxies, that is, the object that we will eventually refer to.
Factory Factory mode
The factory model provides a transition interface for creating objects to isolate the concrete process masks for creating objects to achieve greater flexibility.
The factory model can be divided into three categories:
Simple Factory mode (Factory)
Factory mode (Factory method)
Abstract Factory mode (Factory)
These three modes are progressively abstracted from top to bottom and more general. In design mode, GOF divides the factory model into two categories: The Factory method Model (Factory) and the Abstract Factory mode (abstraction Factory). The simple Factory is seen as a special case of the factory method pattern, which is grouped into one category.
Difference:
Factory method Mode:
An abstract product class that can derive a number of specific product classes.
An abstract factory class that can derive a number of specific factory classes.
Each specific factory class can only create an instance of a specific product class.
Abstract Factory mode:
Multiple abstract product classes, each abstract product class can derive multiple specific product classes.
An abstract factory class that can derive a number of specific factory classes.
Each specific factory class can create instances of more than one specific product class.
Difference:
The factory method pattern has only one abstract product class, and the abstract factory pattern has multiple.
Factory-mode-specific factory classes can only create instances of a specific product class, while abstract Factory mode may create multiple.
Singleton single-Case mode
Singleton mode can have only one instance.
The Singleton class must create its own unique instance.
The Singleton class must provide this instance to other objects.
Ensure that a class has only one instance and provides a global access point to access it.
The singleton mode (Singleton) is the most antagonistic of several creation patterns, and its main feature is not to generate a new instance based on the user program invocation, but rather to control the instance uniqueness of a type by knowing that it contains only one of the roles, that is, Singleton, which has a private constructor , which ensures that users cannot instantiate it directly through new. In addition, the schema contains a static private member variable instance with the static Public method instance (). The Instance () method is responsible for verifying and instantiating itself, and then storing it in a static member variable to ensure that only one instance is created.
There is a requirement for using the singleton mode: Singleton mode should be used when a system requires only one instance of a class. Conversely, if a class can have several instances coexisting, do not use singleton mode.
Do not use singleton mode to access global variables. This violates the intent of the singleton pattern and is best placed in the static member of the corresponding class.
Do not make the database connection a singleton, because a system may have multiple connections to the database, and in the case of a connection pool, the connection should be released as promptly as possible. Singleton mode because of the use of static member storage class instances, it can cause resources can not be released in time, causing problems.
Delegate delegation mode
The delegation mode (Delegate) is a common pattern in object-oriented design patterns. The principle of this pattern is that Class B and Class A are two classes that have no relation to each other, B has a method and a property that is exactly the same as a, and the method in B is called, and the property is called a method and a property of the same name in a. b seems to be an intermediary entrusted by a mandate. Third-party code does not need to know the existence of a, nor a direct contact with a, through B can directly use the function of a, so that both can use a variety of public energy, but also a good protection of a.
Advantages of the delegation mode:
Single one by one classes cannot represent complex designs
Design Split
Easy to reuse
Relatively independent
Clear definition of function, separation of behavior interface
Loosely coupled, easy to scale
Strategy Policy mode
Define a series of algorithms that encapsulate each algorithm and make them interchangeable with each other. This mode allows the algorithm to be independent of the customers who use it. Also known as policy mode. (Definea family of Algorithms,encapsulate each one, andmake them interchangeable. Strategy lets the algorithmvary independently from clients. )
The strategy pattern distinguishes between the object itself and the arithmetic rules, and its function is very powerful, because the core idea of the design pattern itself is the idea of the polymorphism of object-oriented programming.
Use strategy mode when the following conditions are present
? Many of the related classes are simply behavior-specific. Policy provides a way to configure a class with one behavior in multiple behaviors. That is, a system needs to dynamically select one of several algorithms.
? You need to use different variants of an algorithm. For example, you might define algorithms that reflect different spatial/temporal tradeoffs. When these variants are implemented as a class hierarchy of an algorithm, the policy mode can be used.
? The algorithm uses data that the customer should not know about. You can use the policy mode to avoid exposing complex, algorithmic-related data structures.
? A class defines a variety of behaviors, and these behaviors appear as multiple conditional statements in the operation of this class. The related conditional branches are moved into their respective strategy classes in place of these conditional statements.
Prototype prototype mode
The main idea of prototype mode is to clone a new object based on an existing object, which is generally a method of providing a clone within an object, which returns a copy of an object, which is a different way of creating objects than the several types of creation patterns we said before. The previously described factory model and the abstract factory are the process of factory encapsulation of specific new operations, returning a new object, sometimes it is not worthwhile to create objects from such a factory, especially in the following scenarios, it may be simpler and more efficient to use prototype mode.
? Use prototype mode when a system should be independent of its product creation, composition, and presentation
? When the class to be instantiated is specified at run time, for example, by dynamic loading;
? To avoid creating a factory class hierarchy that is parallel to the product class hierarchy
? When an instance of a class can have only one of several different state combinations. Building the corresponding number of prototypes and cloning them may be more convenient than manually instantiating the class each time you use the appropriate state. (That is, when we are dealing with some objects is relatively simple, and the difference between objects is very small, it may be a very fixed number of properties are different, maybe we use the prototype model more appropriate).
Template templates Mode
A template method pattern is a behavioral pattern of a class, in which there is only an inheritance relationship between classes and no object affinity.
Board method pattern is based on the inheritance of code reuse Basic Technology, template method pattern structure and usage is also one of the core object-oriented design. In the template method pattern, you can put the same code in the parent class and put different method implementations in different subclasses.
In the template method pattern, we need to prepare an abstract class that implements some of the logic in the form of concrete methods and concrete constructors, and then declares some abstract methods for subclasses to implement the remaining logic. Different subclasses can implement these abstract methods in different ways, thus having different implementations of the remaining logic, which is the intent of the template method pattern. The template method pattern embodies many important ideas of object-oriented, and is a mode with high frequency.
The template method applies to the following situations:
One-time implementation of an invariant part of the algorithm, and the variable behavior is left to subclass to achieve.
The public behavior in each subclass should be extracted and centralized into a common parent class to avoid code duplication. First identify the differences in the existing code and separate the differences into new operations. Finally, replace these different code with a template method that invokes these new operations.
Controls the subclass extension. The template method invokes the "hook" operation only at a specific point, so that only those points are allowed to be extended.
The source code for these design patterns is not to be made clear by a few words, so I created a Java Architecture Technology Exchange Group: 688583154 There are Java engineering, distributed, high performance, performance tuning, Spring,mybatis, Netty Source design mode analysis and other knowledge points and it technology, it career, online courses, learning resources to share, special attention: We are free to share learning resources, Ali architects to share knowledge, many years of work experience combing and summary, with everyone comprehensive, scientific to establish their own technical system and technical knowledge. Access to the group for free:
Object-oriented:
Work 1-5 years need to break through the technical bottleneck;
Java working mechanism, common design ideas, common Java Development framework to master proficiency;
Ten years Java "veteran" talking about the seven design patterns of source code