Common Design Patterns knowledge summed up the interview (Java version)

Design Patterns

This summary is mainly based on my design patterns series of articles formed. The main point is the important knowledge with their own words again, there may be some errors, but also hope forgive me and pointing. Thank you

More details can on my cdsn blog View: https://blog.csdn.net/a724888

Finally, if you want to better complete the study of this part, I suggest that you take a look original.

Create a schema

Create a schema to create schema role is to create an object, when it comes to creating an object, the most familiar with is a new object, and then set the relevant attributes. However, in many scenarios, we need to provide more client-friendly way to create objects, especially the kind we define a class, but when the need to provide other developers to use.

Singleton

1. `singleton global singleton ensure only one instance, when so used to direct access, such as a connection to the database, Spring where the bean, can be a single embodiment. `

2.  

`Example 3. Single mode there are five general writing. `

4.  

`The first is starving mode first instantiate a single embodiment, can be directly acquired when acquired by the static method. The disadvantage is that after the completion of the loading class instantiation, wasteful portion of the space class. `

6.  

7. `The second mode is well-fed, first singleton set to null, then get re-instantiated when a single case of static method, but may be instantiated multiple threads at the same time, there will be concurrency issues. `

8.  

9. 'The third improved method is progressive, you may be synchronized by a start synchronized keyword, but the cost is too large, then changed to use a single volatile modified embodiment, then a check determines whether a single embodiment has been initialized, if not initialized on the use of synchronized code block, check again prevents single embodiment this period is initialized, and after the real initialized. `

10.  

11. `The fourth class is implemented using an internal static, static inner class initialization is performed only when in use, so instantiated in the interior of a single class of embodiments, will be used only when the code running instance . Then return the singleton class external to internal static class static method. `

12.  

13. `fifth class is an enumeration, enumeration class underlying implementation is actually inside the class. Enum class ensures that each class object is unique in the global. So to ensure that it is a single example, this method is the simplest. `

Factory Pattern

1. `simple factory generally used to create multiple instances of a class of plant. `

2.  

3. `factory mode generally refers to a factory service interface, for example for the implementation class of` interface

4.  

`The abstract factory pattern is a plant refers to a family of products and services, a product family may contain a plurality of interfaces, the interface would comprise a plurality of classes implemented through a plant can bind these together, it is very convenient. `

Prototype mode

1. `to obtain more general example of the prototype of the same cloned by an example. Examples of the method used to clone complete. `

Builder mode

1. `builder mode, there is a concept called chained calls, chain calls to instantiate a class of convenience, generally provides a series of examples of the method, in fact, is to look at the transformation of the set method, originally returned empty the set methods instead return this instance, in order to achieve chained calls. `

2.  

3. `the builder pattern on the basis of the builder added method, to provide an external call, the same call chain is accomplished using injection parameters. `

Structural model

1. Structure pattern ``

2.  

3. `created earlier schema describes the objects created some design patterns. `

4.  

5. `structural model described in this section are intended purpose by changing the code structure to achieve decoupling, make our code easier to maintain and extend. `

Bridge Mode

1. `a little complicated. Recommendations refer to the original `

Adapter mode

1. `adapter mode for adapting two different classes. `

2.  

3. `similarities and differences Adapter mode and proxy mode '

4.  

`5. Comparison of these two models, in fact, the adapter mode and a comparison target proxy mode, in the code structure,`

6. `They are similar, they are required to achieve a specific instance of the class. `

7. `but their purpose is not the same, do proxy mode is to enhance the living original method;`

8. `adapter is adapted to make a living, in order to provide a" duck packaged chicken, duck and then used as a "`

9. `The chickens and ducks had no inheritance relationship between them. `

10.  

`Adapter 11. Adapter mode can be divided into categories, the object adapter. `

12.  

13. `adapter class by inheriting a parent class you can fit themselves into the parent class. `

14. `objects and object adapter need to make another pass object's constructor, for packaging. `

Flyweight

1. `in that the core Flyweight Flyweight factory class,`

2.  

3. `Flyweight role is to provide a factory class for storing objects Flyweight Flyweight pool,`

4.  

5. `subject in need of the user, obtained from the first pool Flyweight,`

6.  

7. `If Flyweight pool does not exist, create a new shared flyweight object is returned to the user,`

8.  

9. Save the new object `Flyweight pool. `

Proxy mode

1. `We found no proxy mode it means to do" packaging method "or do" method of enhancing. " `

2.  

3. `In Aspect Oriented Programming, forget or do not tout this term, and in the AOP,`

4.  

5. `is actually a dynamic proxy process. Such as Spring, the `

6. `We do not define their own proxy class, but Spring will help us to define the dynamic agency,`

7.  

8. Then we define `@ Before, @ After, @ Around the code logic to dynamically add agent. `

Facade pattern

1. `conventional package appearance pattern specific implementation details, to provide users with a simpler interface. `

2.  

3. `you can get content needs of a single method call. `

Combined mode

1. `combined mode for representing data having hierarchical structure, so that we have a consistent access to individual objects and combinations of objects. `

2.  

3. `direct look at an example, each employee has a name, department, salary these attributes`

4.  

5. `subordinate staff as well as a collection (although it may set is empty),`

6. `The subordinate employees and their structure is the same,`

7.  

8. `there name, department these attributes`

9. `but also their subordinates collection. `

10.  

11.  `class  Employee  {`

12.  `private  String name;`

13.  `private  String dept;`

14.  `private  int salary;`

15.  `private  List
 
 
  
  
   subordinates; // subordinate `
  

  
16.  `}`
  

  

  
 
 

Decorator Pattern

Decorator

Decorator Pattern to enhance each class inherits most senior parent. Then when required enhancements to enhance the incoming class to class instance, and then enhance the class when you can use to enhance the function of the original class.

And a proxy mode are different, each decorative pattern decorator class inherits the parent class, and may be multi-level package.

Behavioral patterns

1. `` behavioral patterns

2.  

3. `behavioral patterns concern is the interaction between the various classes, the clear segregation of duties, to make our code more clear. `

Strategy Mode

1. `Strategy pattern to a general strategy as a class, and pass instances when you need to specify the policy, so we can pass the specified algorithm where necessary the use of algorithms. `

Command Mode

1. `command initiator is generally divided into command mode, commands and the three roles of the recipient. `

2.  

3. When using the `command originator command instances be injected. Then execute the command calls. `

4.  

5. `command actually calls the method call recipient's command carried out the actual call. `

6.  

7. `For example, a remote control button is equivalent to a command, the command runs when you click the button, automatically call the methods your TV. `

Template Method Pattern

1. Template method 'refers generally to a method of providing a template, and wherein some of the implementation class and some abstract classes, and specifies the execution order. `

2.  

3. `implementing class is a good way to provide a template. An abstract class is implemented by users themselves. `

4.  

5. `template method defines the order of execution of a template method is very suitable for some development framework, so the template method is also widely used in the open source framework. `

Observer mode and event listeners mechanism

1. `observer data subscription mode may be used between the subscriber and the publisher message. `

2.  

3. `generally divided into themes and observer, the observer subscribe to topics, the instances registered on the list maintained by the observer theme. `

4.  

5. `automatically push data to the viewer or observer notification data has been updated theme update data. `

6.  

7. However, since such a manner `message push tight coupling relationship. And it is difficult to know without opening the data of what type of data Yes. `

8.  

9. `know later in order to make the data more flexible format, using events and event listener pattern, event packed event type and event data, decoupled from the subject and observer. `

10.  

11. The theme of the event `When an event occurs, the trigger all the listeners of the event, the event is sent to each listener by listener list, after listening to get the event, according to event type corresponding to first find their own support in the treatment process device, then the processing corresponding to the event processor. `

Chain of Responsibility pattern

1. `chain of responsibility usually need to establish a singly linked list, then the caller only need to call the head node on it, will automatically flow back down. `

2.  

3. `such approval process is a good example, as long as the end-user application submitted, in accordance with the content of information application, automatically establish a chain of responsibility, then you can start the transfer. `

Micro-channel public number

Personal Public Number: Huang oblique

Huang oblique cross-examination 985 Master of software engineering, self-learning Java two years, got the BAT nearly ten manufacturers offer, from a technical white grow as Ali Engineer.

JAVA authors focus on the back-end technology stack, keen to share programmers dry, learning experience, job experience and life program is currently small yellow diagonal CSDN blog a million + views, fans know almost 2W +, the whole network has 10W + reader .

Huang is a little oblique slash youth, adhere to the study and writing, believe in the power of lifelong learning, hope and more programmers make friends, progress and grow together!

Original eBook:
public attention after [number] Huang oblique reply [original] to collect my original e-book e-book "Programmer's rookie practice Manual: From white to Alibaba Java technology engineer"

Programmers 3T Technology Learning Resources: Some programmers learning resources spree technology, the number of public attention, background replies keyword "data" can get free no routine.

PubMed review the information:
Computer PubMed spree, are some of the review materials for my own use when PubMed review, including public courses and professional video review, recommend it to everyone here, the number of public attention, background replies keyword "PubMed" that is, available free of charge.

No public technology: Java technology rivers and lakes

If you want to focus on my real time updated articles and dry sharing, you can focus on my public number of rivers and lakes] [Java technology Java, a technical engineer Ali station, the author Huang oblique, focused Java related technologies: SSM, SpringBoot , MySQL, distributed, middleware, cluster, Linux, network, multi-threaded, occasionally speaking point Docker, ELK, as well as dry goods and technology to share the learning experience, committed to the full Java stack development!

Java engineers required Learning Resources: Some Java engineers common learning resources, the number of public attention, background replies keyword "Java" to get free no routine.

My public number

This article from the blog article multiple platforms OpenWrite release!

Guess you like

Origin www.cnblogs.com/AliCoder/p/11700748.html