Java Design Pattern 1: An Introduction to design patterns

Source: Internet
Author: User
Tags html form

What is the design pattern?

Design patterns are a set of reusable, most known, categorized purposes, code design experience Summary. Design patterns are used to reuse code, make code easier for others to understand, and ensure code reliability. There is no doubt that design patterns in their own systems are multi-win, design patterns so that code is truly engineering, design patterns are the cornerstone of software engineering, as the structure of the building.

Overall, the design pattern is divided into 3 major categories totaling 23 kinds:

1, create a model, a total of 5

Factory method mode, abstract Factory mode, singleton mode, builder mode, prototype mode

2. Structure mode, total 7 kinds

Adapter mode, adorner mode, appearance mode, bridging mode, combined mode, enjoy meta mode

3, behavioral mode, a total of 11

Policy mode, template method mode, observer pattern, iterator mode, responsibility chain mode, Command mode, Memo mode, state mode, visitor mode, mediator mode, interpreter mode

There are actually two classes, the concurrency pattern and the thread pool pattern. The module of design pattern does not explain each kind of design pattern, but chooses some important, commonly used design pattern to explain, heavy quality not the weight.

Six Principles of design pattern

1. Open and closed principle OCP

The open and closed principle is that opening to the extension and closing the modification . When the program needs to be extended, it is not possible to modify the original code, which is also to make the program more extensible, easy to upgrade and maintenance.

2, the Richter substitution principle LSP

Replace a base class object with its subclass object in the software, and the program will not produce any errors or exceptions, which in turn is not true. If a software entity is using a subclass object, it must not be able to use the base class object. The procedural manifestation of the principle of substitution of the Richter scale is to use the base class type to define the object in the program, and then determine its subclass type at run time, substituting the parent class with the subclass object.

3. Control reversal Principle IOC

Programming for interfaces, relying on abstraction rather than on specific

4. Interface Isolation principle ISP

Using multiple isolated interfaces is better than using a single interface

5, Dimitri Law DP

An entity should interact with other entities as little as possible, making the system function modules relatively independent

6. Principle of synthetic reuse

Try to use the combination/aggregation approach instead of using inheritance

Three keywords in a design

1. Abstraction

Extracting common and essential features in many things, discarding non-essential features, is abstraction. The process of abstraction is also a process of tailoring, which, when abstracted, is different, depending on what angle it is abstracted from. The abstract angle depends on the purpose of the analysis problem.

2, realization of the

The concrete implementation of abstract class is realization.

An instance of a class is an instantiation of this class, and a concrete subclass is an instantiation of its abstract superclass.

3. Decoupling

This is more important, usually we always agreed that the code should be "high cohesion, low coupling", then what is the coupling?

The so-called coupling is a strong association of the behavior of two entities . and removing the strong association between them is decoupling. Decoupling is to untie the coupling between abstraction and implementation, or to change the strong association between them into weak associations.

The so-called Strong Association, refers to the compile time has been determined, can not be dynamically changed during the runtime of the Association, so-called Weak association, it can be dynamically determined and dynamic change in the run time of the association . From this definition, the inheritance relationship is strongly correlated, and the aggregation relationship is a weak association.

MVC design Pattern

The MVC design pattern does not belong to one of the 23 design patterns, as for why it is said later, but because it is very common and more important, let's talk about it.

The full name of MVC is the Model View controller, which is the abbreviation for Models-View-controller, a software design paradigm. MVC organizes code in a way that separates business logic, data, and interfaces, and aggregates business logic into a single invisible, without the need to rewrite business logic while improving and personalizing the interface and user interaction.

The MVC design pattern also provides full control over HTML, CSS, and JS, followed by a look at several parts of the MVC design pattern:

1. Model

The model identifies enterprise data and business rules, and in the three parts of MVC, the model has the most processing tasks. The data returned by the model is neutral, that is, the model is independent of the data format, so that a model can provide data for multiple views, since the code applied to the model can be reused by multiple views only once, thus reducing the duplication of code

2. Views view

A view is an interface that the user sees and interacts with. For older Web applications, views are an interface of HTML elements, and in modern Web applications, HTML still plays an important role in the view, but new technologies are emerging, including some identity languages such as XHTML, Xml/xsl, WML, and web Service.

The benefit of MVC is that it can handle many different views of the application, in which the view is not actually processed, and the view is simply a way of outputting data and allowing the user to manipulate it.

3. Controller controllers

The controller accepts the user's input and invokes the model and view to complete the user's needs, so when the hyperlinks in a stand-alone Web page and the HTML form are sent, the controller itself does not output anything and does any processing, it simply receives the request and decides which model component to call to process the request. Then decide which view to use to display the returned data

Using the MVC layering model helps manage complex applications because developers can focus on one aspect at a time. For example, developers can focus on view design without relying on business logic, while also making application testing easier.

Using the MVC layering model also simplifies group development, and different developers can develop views, controller logic, and business logic at the same time.

The most typical MVC is jsp+servlet+javabean.

Why MVC Design Patterns do not belong to one of 23 design patterns

Look at the problem from two angles:

1, MVC design pattern we call that, so is MVC really a design pattern? Or is MVC a framework? Although the framework is similar to the design pattern, it is fundamentally different. The design pattern is a description of the recurring problems in an environment and the solution to the problem, which is more abstract than the framework, the framework can be expressed in code, can be directly executed or reused, and only instances can be represented by code in the design pattern, and the design pattern is a smaller element than the framework. A framework often contains one or more design patterns, and the framework is always targeted at a particular application area, but the same design pattern can be applied to a variety of applications. It can be said that the framework is software, the design pattern is the knowledge of software, the framework is a great wisdom, used for the division of Software design, design mode is a small skill, to solve specific problems, to improve code reuse rate, reduce coupling degree. In this sense, MVC should belong to a framework rather than a design pattern

2, 1th said, "A framework often contains one or more design patterns," and indeed it is. "GOF", the author of design patterns, does not refer to MVC as a design pattern, in their view, MVC is actually the evolution of the design pattern of the other three attractions: The Observer mode + the strategy mode + the combination mode, depending on the MVC in the framework of different implementations may also use the Factory mode and adorner mode. For example, the model is the data for the application, and the view handles the content that is presented to the user on the screen, so that MVC is based on the push/subscribe model on the core communication, and when a model changes it notifies the application of other modules for updates. The subscriber----is usually a controller and then updates the corresponding view, a natural observation relationship that facilitates multiple view associations to the same model

Java Design Pattern 1: An Introduction to design patterns

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.