Overview of Java Design Patterns

Source: Internet
Author: User

Design Pattern Overview 1. What is the design pattern?

We usually write code in the process, will encounter a variety of problems, think about a lot of problems to solve the idea of roughly the same, at this time you can solve the problem of the idea of clear, become a problem-solving core model, you can use this model to solve similar problems, Instead of racking your brains to find the best solution. After many years of the contributions and practice of the road, Daniel has formed a set of system software design mode.

2. Categories of Design Patterns

The design pattern is divided into 3 types, a total of 23 kinds.

1) Create pattern: The creation Pattern abstracts the instantiation process. They help a system to be independent of how to create, group, and represent his objects.

Design patterns that belong to the creation type:

Singleton mode (Singleton), abstract Factory mode (abstract Factory), Factory mode (Factory Method), builder mode (builder), prototype mode (Prototype).

2) Structural patterns: Structural patterns involve the combination of classes and objects to draw larger structures, and structural patterns that use inheritance or combination mechanisms to combine interfaces or implementations.

A design pattern that belongs to a structural type:

Adapter mode (Adapter), bridge mode (bridge), decorative mode (Decorator), combined mode (Composite), appearance mode (facade), enjoy meta mode (Flyweight), Agent mode (proxy).

3) Behavioral patterns: behavioral pattern design to algorithms and assignment of duties between objects.

A design pattern that belongs to a behavioral type:

Module method mode, command mode, iterator mode (ITERATOR), observer mode (OBSERVER), Mediator mode (mediator), Memo mode (Memento), Interpreter mode ( interpreter), Status mode (state), policy mode (strategy), responsibility chain mode (Chain of Responsibility), visitor mode (Visitor).

Learn any design pattern from the following 4 points:

A. Pattern name: A help-person, he uses one or two words to describe the problem, solution and effect of the pattern.

B. Issue: Describes when design patterns should be used.

C. Solution: Describes the composition of the design, the interrelationships between them, their responsibilities, and the way they collaborate.

D. Effect: Describes the application of the model and the use of patterns should weigh the problem.

3. Intent of the design pattern

Each design pattern is dedicated to solving a problem, and the following table lists the effects of these design patterns.

Design Patterns intention/function
Create pattern
Single-Case mode Ensure that a class has only one instance and provides a global access point to access it.
Abstract Factory mode Provides an interface to create a series of related or interdependent objects without specifying their specific classes.
Factory mode Defines an interface for creating objects, so that subclasses decide which class to instantiate, and Factory mode defers to its subclasses using the instantiation of a class.
Builder mode Separating the construction of a complex object from his presentation allows the same build process to create different representations.
Prototype mode Use the prototype instance to specify the kind of object to create and create a new object by copying the prototypes.
Structural mode
Adapter mode Transform the interface of one class into another interface required by the customer. The adapter mode makes it possible for those classes that would otherwise not work together because of incompatible interfaces to work together.
Combination mode Combines objects into a tree structure to represent a "partial-whole" hierarchy. Composite makes the user consistent with the use of individual objects and composite objects.
Proxy mode For other objects that provides a proxy to control access to this object.
Bridging mode Separate the abstract and implementation parts so that they can change independently.
Decoration mode Dynamically add some additional responsibilities to an object. For added functionality, the decorator mode is more flexible than generating subclasses.
Appearance mode Provides a consistent interface for a group of subsystems, and the facade schema defines a high-level interface that makes this subsystem easier to use.
Enjoy meta mode Use shared technology to effectively support a large number of fine-grained objects.
Behavioral patterns
Observer pattern Defines a one-to-many dependency between objects, and when an object's state changes, all objects that depend on it are notified and automatically updated.
Broker mode Encapsulates a series of object interactions with a mediation object. Intermediaries make the various objects not need to explicitly reference each other, so that they are loosely coupled, and can independently change the interaction between them.
Responsibility chain Model Enables multiple objects to have the opportunity to process requests, thus avoiding the coupling between the sender and receiver of the request. Chain these objects together and pass the request along this chain until an object is processed by him.
Command mode Encapsulates a request into an object so that you can parameterize the customer with different requests, queue requests or log requests, and support revocable operations.
Interpreter mode Given a language, define a representation of its syntax and define an interpreter that uses that representation to interpret sentences in the language.
Iterator mode Provides a way to sequentially access individual elements in an aggregation object without exposing the object's internal representation.
Memo Mode captures the internal state of an object without destroying the package, and saves the state outside the object. The object can then be restored to its previously saved state.
State mode Allows an object to change its behavior when its internal state changes. The object seems to have modified its kind.
Policy mode Define a series of algorithms that encapsulate them one by one and allow them to replace each other. This mode allows the algorithm to be independent of the customers who use it.
Template method Mode Defines the skeleton of an algorithm in an operation, and delays some steps into subclasses. Templatemethod allows subclasses to redefine some specific steps of the algorithm without altering the structure of an algorithm.
Visitor mode Represents an operation that acts on each element in an object structure. It allows you to define new actions that act on these elements without changing the class of each element.

If many design patterns, beginners must be mastered, only need to master the more commonly used several:

Innovative mode: Singleton mode, abstract Factory mode, builder mode, Factory mode.

Structural mode: Adapter mode, combined mode, proxy mode.

Behavioral patterns: Observer patterns, mediator patterns.

The above is an overview of 23 design patterns, the road of learning is a long way to go!

Overview of Java Design Patterns

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.