Java design pattern (Patterns) __java

Source: Internet
Author: User

Design pattern (Patterns)

--the foundation of reusable object-oriented software

Design patterns are a set of repeated use, most people know, through the classification of purpose, code design experience Summary. Design patterns are used in order to reuse code, make code easier for others to understand, and ensure code reliability. There is no doubt that design patterns in others in the system are more win, design patterns so that the code is really engineering, design pattern is the cornerstone of software engineering, like a block of bricks and stones in the building. The rational use of design patterns in the project can perfectly solve many problems, each pattern now has a corresponding principle in place, each of which describes a recurring problem around us and the core solution to the problem, which is why it is widely used. This chapter is the beauty of Java [from rookie to master evolution] series of design patterns, we will combine theory and practice to carry out this chapter of the study, hope that the vast number of program enthusiasts, learn design patterns, to do a good software engineer.


I. Classification of design Patterns

Overall, the design pattern is divided into three main categories:

The creation pattern, altogether five kinds: The factory method pattern, the abstract factory pattern, the single case pattern, the builder pattern, the prototype pattern.

Structure mode, a total of seven kinds: Adapter mode, adorner mode, agent mode, appearance mode, bridging mode, combination mode, enjoy meta mode.

There are 11 types of behavioral Patterns: Policy mode, template method pattern, observer mode, iterative sub mode, responsibility chain mode, Command mode, Memo mode, state mode, visitor mode, mediator mode, interpreter mode.

There are two other types of concurrency: concurrent and thread-pool mode. Use a picture to describe the whole:

the six principles of design pattern

1, opening and closing principle (Open close principle)

The opening and closing principle is said to be open to expansion, to modify the closure . When the program needs to expand, can not modify the original code, to achieve a hot-swappable effect. So the bottom line is: In order to make the program more scalable, easy to maintain and upgrade. To achieve this, we need to use interfaces and abstract classes, which we'll refer to later in the specific design.

2, the Richter substitution principle (Liskov substitution principle)

The principle of the Richter substitution (Liskov substitution principle LSP) is one of the basic principles of object-oriented design. The Richter substitution principle says that where any base class can appear, subclasses must be present. LSP is the cornerstone of inheritance reuse, only if the derived class can replace the base class, the function of the Software unit is not affected, the base class can be truly reused, and the derived class can add new behavior on the base class. The principle of the Richter substitution is a supplement to the "open-closed" principle. The key step in implementing the "open-closed" principle is abstraction. And the inheritance relation of the base class and subclass is the concrete realization of abstraction, so the principle of the Richter substitution is the specification of the concrete steps to realize abstraction. --from Baidu Encyclopedia

3. The principle of reliance reversal (dependence inversion principle)

This is the basis of the open and closed principle, the specific content: true to interface programming, rely on the abstract and not rely on the specific.

4. Interface Isolation principle (Interface segregation principle)

This principle means that using multiple isolated interfaces is better than using a single interface. or a reduction in the coupling between the class meaning, from here we see, in fact, design pattern is a software design idea, from large software architecture, in order to upgrade and maintenance convenience. So there are several times in the above: reduce dependence and reduce coupling.

5. Dimitri (minimum known principle) (Demeter principle)

Why is called the least known principle, that is to say: an entity should be as little as possible with other entities interaction, making the system functional modules relatively independent.

6. Synthetic reuse principle (composite reuse principle)

The principle is to use the synthesis/aggregation method as much as possible, rather than using inheritance.

Three, Java 23 design pattern

Starting from this piece, we introduce the concept of 23 design Patterns in Java, the application scenarios and so on, and combine their characteristics and design patterns of the principles of analysis.

1. Factory mode (Factory method)

The factory method model is divided into three kinds:

11, the Common factory model is to establish a factory class, the implementation of the same interface for some classes to create an instance. First look at the diagram:

Examples are as follows: (we give an example of sending emails and texting)

First, create a common interface between the two:

Java]   View plain copy public interface Sender {public void Send (); }

Second, create the implementation class: [Java]   View plain copy public class mailsender implements  sender {        @Override        public void  send ()  {           system.out.println ("this  Is mailsender! ");        }  }   [Java]   View plain copy public  class SmsSender implements Sender {          @ override       public void send ()  {            system.out.println ("this is sms sender!");        }  }  

Finally, build the factory class: [Java]   View plain copy public class sendfactory {          public sender produce (string type)  {            if  ("Mail". Equals (Type))  {                return new mailsender ();            } else if  ("SMS". Equals (type))  {               return new smssender ();   

Related Article

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.