The biggest benefits of object-oriented design patterns:

Resist change!

 

 

Rethinking object orientation:

Understand isolation changes:

At the macro level, object-oriented construction is more adaptable to software changes and can minimize the impact of changes

To perform their respective duties:

First, from the micro level, object-oriented approach is more robbed of each class “responsibility”

Second, new types due to changing requirements should not affect the implementation of the original type

What is the object?

First, in terms of language implementation, objects encapsulate code and data.

At the specification level, an object is a set of public interfaces that can be used.

Third, from the conceptual level, the object is a certain kind of object with responsibility

 

Eight principles of object-oriented design:

1. Dependency Inversion Principle (DIP)

① High-level modules (stability) should not depend on low-level modules (change), but both should depend on abstraction (stability).

② Abstraction (stability) should not depend on implementation details (change), implementation details (change) should depend on abstraction (stability).

2. Closed Development Principle (OCP)

① Open to extension, closed to change

② Class modules should be extensible, but not modifiable

3. Single Responsibility Principle (SRP)

① A class should have only one cause for its change

② The direction of change implies the responsibility of the class

4. Liskov substitution Principle (LSP)

① Subclasses must be able to replace their base class (IS-A).

(2) Inherited expression type abstraction

5. Interface Isolation Principle (ISP)

Clients should not be forced to rely on methods they do not use

② Interfaces should be small and complete

Use object composition in preference to class inheritance

① Class inheritance is usually “white box reuse”, object combination is usually “black box reuse”

(2) Inheritance destroys encapsulation to some extent, and the coupling degree of subclass parent is high

(3) Object combination only requires that the combined object has a well defined interface with low coupling degree

7. Change point of encapsulation

(1) Use encapsulation to create a boundary layer between objects, so that designers can make changes on one side of the boundary layer without adverse effects on the other side, thus achieving loose coupling between layers

8. Program for interfaces, not implementations

① Instead of declaring the type in variable 2 as a specific concrete class, declare it as an interface

② The client program does not need to know the specific type of the object, only needs to know the interface of the object

③ Reduce the dependency of each part of the system, so as to realize the type design scheme of “high cohesion, loose coupling”

 

 

Gof-23 pattern classification

In terms of purpose:

The Creational pattern: Defer part of the creation of an object to subclasses or other objects to cope with the impact of changing requirements to create a concrete type for an object implementation.

Structural pattern: A more flexible structure can be obtained through class inheritance or object composition to cope with the impact of changing requirements on the structure of objects.

Behavioral mode: Divides the responsibilities of classes among objects through class inheritance or object combination, thus coping with the impact of changing requirements on multiple interacting objects.

 

In terms of scope:

Class objects handle static relationships between classes and subclasses.

The object pattern deals with dynamic relationships between objects.

Classification of patterns from the perspective of encapsulation change:

 

Component collaboration: Single responsibility: Object creation: Object Performance:

Template Method                      Decorator                      Factory Method                 Singleton

Strategy                                    Bridge                            Abstract Factory                Flyweight

Observer/Event                                                               Prototype

                                                                                        Builder

Interface isolation: State change: Data structure: Behavior change: domain problem

Façade                                     Memento                         Composite                         Command                    Interpreter

Proxy                                       State                                Iterator                                Visitor

Mediator                                                                           Chain of Resposibility

Adapter

 

Refactoring results in the pattern Refactoring to Patterns

 

1, object-oriented design pattern is “good object-oriented design”, the so-called “good object-oriented design” refers to those can meet the “response to change, improve reuse” design

2. Modern software design is characterized by “frequent changes in requirements.” The point of design patterns is to “find points of change” and then apply them at those points to better respond to changing requirements. “When and where to apply design patterns” is more important than “understanding the structure of design patterns themselves.”

3, the application of design pattern should not be preconceived, the use of design pattern is the biggest misuse of design pattern. There is no one-step design pattern. Agile software development’s promotion of “Refactoring to Patterns” is now generally accepted as the best way to use design Patterns

 

Key techniques for refactoring:

Static -> Dynamic

Early binding -> late binding

Inheritance -> Composition

Compile time dependencies -> run time dependencies

Tight coupling -> loose coupling