Read Head. First Design Patterns

Source: Internet
Author: User

Reading this book makes it easy to understand and understand, so that you can quickly understand the design model. It is indeed a good book. This allows you to inadvertently understand and deepen Oo, as well as the advantages of the design pattern. In the purification process at, you can improve your understanding of the design pattern and the mysteries of the design pattern.CodeThe essence of the design pattern is clearly written. However, I did not write well-developed code for the design model, but I wrote the essence of the design model. As a principle, it is easy for beginners to accept and understand the design model. It quickly leads readers into the world of design patterns.

Below we will write down the core idea of the design pattern, as a record of the essence of the design pattern; completely separated from the book, read the complete book after a week to write all the feelings, in order to deepen the essence of the design pattern memory.

 

I. Policy Mode

1.AlgorithmIt is encapsulated in various classes, but all classes inherit the same interface, which is responsible for calling implementation and algorithm output. When we need to add an algorithm, we only need to extend a class that implements this interface, and then pass the class as a parameter, and then call the interface in a unified manner to implement it, and pass in different classes, implement the same call to output the corresponding class algorithm;

2. It has three cores: A: implementing the same interface; B: Passing in different classes of encapsulated algorithms; C: unified interface call methods to implement different classes of input, different outputs are achieved for the same disconnection;

3. Objective: To encapsulate multiple algorithm policies.

Ii. Adapter Mode

1. Because a series of classes (1-n) are implemented, a method (they implement the same interface or abstract class), but now there is a new class, we also want to implement the same call methods for that series of classes. The introduced intermediate class is used to further encapsulate the new class so that it looks similar to the series of classes to achieve the same drop-down mode;

2. Two cores: A: There are two different classes, which must be called in the same way. B: introduce an intermediate class and encapsulate the new class to form a behavior similar to the class to be imitated;

3. purpose: to simulate the behavior of another class.

Iii. Singleton Mode

1. To implement an objectProgramOnly one instance is generated during running and introduced. That is to say, a production system produces the same output for one instance and multiple instances. Therefore, a mode is generated to control the waste caused by less instantiation. To prevent multiple instances caused by multiple threads, the lock is generally applied. A: The lock requires a double lock. B: introduce an object-Type Static instantiated object, which is a secondary object, use it for an external judgment (equivalent to a lock, with a delay effect), and add a lock in it; C: Introduce a static read-only instance variable in the form of direct initial, then, determine whether the object exists;

2. Two cores: A: only one instance can be generated; B: prevent multiple instances from exceptions caused by concurrency and multithreading.

3. Purpose: The system produces only one class instance at any time.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.