An overview of the object-oriented thought design principles and template design Patterns of Java 28-1 design Patterns

Source: Internet
Author: User

In the previous Java 23, you learned about the singleton mode and Factory mode of design mode. Here, the design pattern is introduced

Principle of object-oriented thought design

In the actual development, we want to understand the object-oriented thinking more deeply, we must be familiar with the design principles of the object-oriented thought that the predecessors have summed up.

    1. Single principle of responsibility
    2. Opening and closing principle
    3. The principle of the Richter replacement
    4. Dependency Injection principle
    5. Interface Separation principle
    6. Dimitri Principles

Single principle of responsibility

In fact, developers often say , "high cohesion, low coupling"

In other words, each class should have only one responsibility, and only one function can be provided externally, and the reason for the class change should be only a single cause. In design mode, all design patterns follow this principle.

Opening and closing principle

  The core idea is that an object (class) is open to the extension and closed to the modification.

The open and closed principle means that changes to the class are made by adding code rather than modifying the existing code.

In other words, once the software developer writes out the code that can be run, it should not change it, but to ensure that it can continue to run, how can this be done?

This requires the use of abstraction and polymorphism, that is, to abstract the content of the possible changes, so that the abstract part is relatively stable, and the specific implementation can be changed and extended.

The principle of the Richter replacement

The core idea: wherever the parent class appears, it can be substituted with its subclasses .

In fact, it is said that the objects in the same inheritance system should have common behavioral characteristics.

Dependency Injection principle

The core idea: to rely on abstraction, do not rely on concrete implementation .

In fact, in an application, all classes, if used or dependent on other classes, should rely on the abstract classes of these other classes instead of the concrete classes of those other classes.

To implement this principle, we are required to program for abstract classes or interfaces when programming, rather than for specific implementations.

Interface Separation principle

Core idea: The program should not be forced to rely on methods that they do not need to use .

In fact, it is said: an interface does not need to provide too much behavior, an interface should only provide an external function, should not be all operations are encapsulated in an interface.

Dimitri Principles

Core idea: one object should know as little as possible about other objects

In fact, it is said: reduce the coupling between the various objects, improve the maintainability of the system.

The module should be programmed only through the interface, regardless of the internal workings of the module, it can reduce the coupling degree of each module to a minimum, promote the reuse of software

Design Pattern Overview

Design patterns are a set of reusable, most known, categorized purposes, code design experience Summary.

Design patterns are used in order to reuse code, make code easier for others to understand, and ensure code reliability .

Design patterns are not a method or a technique, but a thought

The design pattern is independent of the specific language, the learning design pattern is to establish the object-oriented thought, as far as possible interface programming, low-coupling, cohesion, is the design of the program can be reused

Learning Design patterns can promote an understanding of object-oriented thinking and vice versa. They complement each other

Design Patterns

Several elements of the design pattern

The name must have a simple, meaningful name

Problem description when to use pattern

Solution describes the components of the design and how to solve the problem

Effect description mode effect and pros and cons

Classification of design Patterns

Creation of Creation mode objects

Composition of structured pattern objects (structure)

Behavior of behavioral Pattern objects

Simple Factory mode

  Simple Factory mode Overview

Also called the static Factory method pattern, which defines a specific factory class that is responsible for creating instances of some classes

  Advantages

Clients do not need to be responsible for the creation of objects, thus clarifying the responsibilities of each class

  Disadvantages

This static factory class is responsible for the creation of all objects, if new objects are added, or some objects are created in different ways, it is necessary to constantly modify the factory class, not conducive to later maintenance

Factory method Mode

  Factory Method Mode Overview

In factory method mode, the abstract factory class is responsible for defining the interfaces that create objects, and the creation of specific objects is implemented by the concrete classes that inherit the abstract factory.

  Advantages

The client does not need to be responsible for the creation of objects, thus clarifying the responsibilities of each class, if there are new objects added, only need to add a specific class and the specific factory class, do not affect the existing code, late maintenance easy, enhance the system extensibility

  Disadvantages

Additional writing code required, increased workload

Single Case design mode

  An overview of the single-instance design pattern

The singleton pattern is to make sure that the class has only one object in memory and that the instance must be created automatically and provided externally.
  Advantages

There is only one object in system memory, so system resources can be saved, and for some object singleton patterns that need to be created and destroyed frequently, the performance of the system can be improved.

  Disadvantages

There is no abstraction layer, so the extension is difficult.

Too heavy a duty to violate a single duty in a certain procedure

Template design mode

  Overview of template Design Patterns

The template method pattern is defined as the skeleton of an algorithm, and the specific algorithm is deferred to the subclass to realize

  Advantages

Using the template method mode, in defining the skeleton of the algorithm, we can implement the concrete algorithm flexibly, and satisfy the user's flexible and changeable demands.

  Disadvantages

If the algorithm skeleton is modified, you need to modify the abstract class

Decorative design pattern

  Overview of Decorative Design Patterns

Decorating mode is the use of an instance of a subclass of a decorated class, where the client gives an instance of the subclass to the adornment class. is an alternative to inheritance

  Advantages

Using the adornment mode, you can provide more flexible extension objects than inheritance, it can dynamically add the function of objects, and can be arbitrarily combined with these features

  Disadvantages

Because they can be assembled at will, there may be some unreasonable logic.

Adapter design mode

  Adapter Design Pattern Overview

Transforms the interface of one class into another interface that the customer wants. So that the interface that cannot be called directly can be called.

  Advantages

Make interfaces that are not intended for use suitable for use

  Disadvantages

Only one class can be adapted at a time, with certain limitations

An overview of the object-oriented thought design principles and template design Patterns of Java 28-1 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.