Design pattern one-people summary

Personal summary of design patterns.

Keep updating

 

In fact, in ordinary learning and doing projects, design patterns can be encountered consciously or unconsciously;

After choosing the "UML" course last year, it is equivalent to adopting a clear way to re-understand these design patterns;

Undergraduates actually have this course, but due to the latter semester and busy with internships, projects and other matters, it is limited to the current level, and the importance of this course is not fully realized;

Later, I gradually realized that I can encounter everywhere in ordinary learning and doing projects, so I suggest that you don’t need to learn specifically, because the core is to learn the idea of design patterns . After you have systematic theoretical knowledge, you can accumulate it in ordinary times. can;

Here are the accumulated design patterns, and general introduction and application scenarios:

Common design patterns:

   [1] Singleton mode: (Of course there are many other implementations derived, 8 currently known)

      Lazy style :

       public class Singleton {

                     private static Singleton instance;

                     private Singleton (){}

                     public static Singleton getInstance() {

                            if (instance == null) {

                                   instance = new Singleton();

                            }

                            return instance;

                     }

              }

 

      Drunkard style : Initialize when the class is loaded, wasting memory.

       public class Singleton {

                     private static Singleton instance = new Singleton();                                       private Singleton (){}

                 public static Singleton getInstance() {

                            return instance;

                 }

              }

 

    [ 2 ] Factory mode : ConnectionFactory SessionFactory...

    [ 3 ] Proxy mode : The underlying implementation of Spring AOP is a dynamic proxy

    [ 4 ] Strategy mode : one interface provides multiple different implementation classes

                  For example, List interface ArrayList LinkedList implementation class;

                  spring的context

                  In fact, the evolution process of lambda expressions has also used strategy mode: garbage code --> strategy mode --> anonymous inner class --> Lambda expression

                 

    [5 ] Packaging mode : IO flow, node flow is surrounded by layers of filtering flow (packaging flow, processing flow);

    [ 6 ] Command mode : Struts <result>;

           Command Pattern (Command Pattern) is a data-driven design pattern, which belongs to the behavioral pattern. The request is wrapped in the object in the form of a command and passed to the calling object. The calling object looks for a suitable object that can handle the command, and passes the command to the corresponding object, which executes the command.

           How to solve : call the receiver to execute the command through the caller, the order is: caller→command→receiver.

           Key code : Define three roles: 1. Received the real command execution object 2. Command 3. Invoker uses the entry of the command object

           Application example : There is only one action core controller ActionServlet in struts 1, which is equivalent to Invoker, and the model layer class will have different model classes with different applications, which are equivalent to specific Commands.

   [7] Adapter mode :

           The adapter mode serves as a bridge between two incompatible interfaces.

           Convert the interface of a class to another interface that the customer wants.

           How to solve: inheritance or dependency (recommended).

           Key code: The adapter inherits or relies on existing objects to achieve the desired target interface.

   [8] Facade Pattern: Facade Pattern (just called Facade Pattern, no other name)

           The appearance mode hides the complexity of the system and provides the client with an interface through which the client can access the system.

           When to use : 1. The client does not need to know the complicated internal connections of the system, the entire system only needs to provide a "receptionist". 2. Define the entrance of the system.

           How to solve : The client is not coupled with the system, and the appearance class is coupled with the system.

           Key code : Add another layer between the client and the complex system. This layer handles the calling sequence and dependencies.

           Application examples : 1. To go to the hospital to see a doctor, you may have to go to registration, outpatient service, pricing, and medicine, which makes the patient or the patient's family feel very complicated. If there is a reception staff, it is very convenient to let the reception staff handle it. 2. JAVA's three-tier development model (performance, business, durability)

           Typical representative : RockectMQ 's Producer entry class DefaultMQProducer uses the appearance model.

   [9] Observer mode:

           Common: publish-topic-subscribe;

[ 10 ] java8 parallel stream: the difference between the Fork/Join framework and the traditional thread pool: the "work stealing" mode is adopted ;

[11] Prototype mode:

The creation of beans in Spring is the prototype mode;

beans.xml
<bean id="id01" class="com.atguigu.spring.bean.Monster" scope="prototype"/>

[12] Interpreter mode:

SpelExpressionParser in the Spring framework uses the interpreter mode;

[13] Template method design mode:

AQS design;

 

 

 

//

 

 

 

 

    *: Resource pool mode Future mode      

 

 

 

Guess you like

Origin blog.csdn.net/ScorpC/article/details/113793592