C + + Design pattern (first week)

Source: Internet
Author: User

Introduction to Part 1 design patterns

  Course Objectives

1. Understanding Loose coupling design ideas
2. Mastering object-oriented design principles
3. Mastering the reconstruction techniques to improve the design
4. Mastering the GOF core design pattern

  What is design mode?

  "Each pattern describes a problem that recurs around us, and the core of the solution to the problem. This way, you can use the program again and again without having to do repetitive work. " --christopher Alexander

Talking from object-oriented

Bottom thinking: downward, how to grasp the bottom of the machine from the microscopic understanding of the object construction

#语言构造
#编译转换
#内存模型
#运行时机制

Abstract thinking: upward, how to abstraction our surrounding world into program code
  #面向对象
#组件封装
#设计模式
#架构模式

  Encapsulation, inheritance, polymorphism are the underlying thinking.

  In-depth understanding of object-oriented

Down: Deep understanding of the three object-oriented mechanisms
  #封装, hide internal implementations
#继承, reusing existing code
#多态, overriding object behavior

Up: grasp the abstract meaning of the object-oriented mechanism, understand how to use these mechanisms to express the real world, and grasp what is "good object-oriented design".

  The inherent complexity of software design

"Builders never want to build a new small basement under an already built 100-storey building-it costs a lot and is doomed to fail." Surprisingly, however, users of software systems don't think about it when they ask for similar changes, and they believe it's just a matter of simple programming. " --object-oriented Analysis and Designwith applications

  The root cause of complex software design

Change: Changes in customer needs, technology platform changes, development team changes, market environment changes.

  How to solve complexity

#分解
There is a common approach to complexity: divide and conquer, decompose large problems into small problems, and decompose complex problems into simple problems.

#抽象
At a higher level, people deal with complexity with a common technique, namely abstraction. Due to the inability to master all the complex objects, we choose to ignore the non-essential details, and to deal with the generalized and idealized object model.

  The goal of software design

What is good software design? The golden rule of software Design: reuse!

In C + + class inheritance, private and protected methods are seldom used.

The subclass object can be copied to the parent class object, but will cause the cut to occur. So in the form of pointers (or references). Like what

classShape {};classRect: PublicShape { Public: Rect (intXinty): x (x), Y (y) {}intx; inty;};intmain () {vector<Shape>v; Rect R (1,2);    V.push_back (R); cout<< v[0].x; return 0;}

Compiler error: Class "Shape" has no member X.

But if you change to a pointer, the code looks like this:

classShape {};classRect: PublicShape { Public: Rect (intXinty): x (x), Y (y) {}intx; inty;};intmain () {vector<Shape*>v; V.push_back (NewRect (1,2)); //This is not the same place.rect* p = (rect*) v[0]; cout<< p->x; return 0;}

The pointer type conversion was used, but the object itself was not cut.

Part 2 object-oriented design principles

  Object-oriented design, why?

#变化是复用的天敌!

The biggest advantage of object-oriented design is that it resists change!

  Re-understanding Object-oriented
#理解隔离变化
From the macro level, the object-oriented construction method can adapt to the change of software, and can minimize the impact of the change.
#各司其职
At the micro level, the object-oriented approach emphasizes the "responsibility" of each class.
New types due to changes in demand should not affect the implementation of the original type--the so-called accountability
#对象是什么?
From a language implementation perspective, objects encapsulate code and data.
From a specification level, an object is a series of public interfaces that can be used.
From a conceptual level, an object is an abstraction of a certain responsibility.

The call pattern of C + + virtual function can better grasp 23 kinds of design patterns, and should return to eight design principles.

Eight design principles are more important than 23 design patterns!

1. Dependency inversion principle (DIP)
High-level modules (stable) should not rely on lower-layer modules (variations), both of which should be dependent on abstraction (stability).
Abstraction (stability) should not depend on implementation details (changes), and implementation details should depend on abstraction (stability).

2. Open closure principle (OCP)
Open to extensions, closed for changes.
The class module should be extensible, but not modifiable.

3. Single responsibility principle (SRP)
A class should have only one reason to cause it to change.
The direction of change implies the responsibility of the class.

(If a class has too many methods to--78-80-it indicates that the class's responsibilities are unclear)

4. Liskov replacement principle (LSP)
Subclasses must be able to replace their base class (IS-A).
Inherits the expression type abstraction.

5. Interface Isolation principle (ISP)
Client programs should not be forced to rely on methods that they do not use.
The interface should be small and complete.

6. Prioritize the use of object combinations rather than class inheritance
Class inheritance is usually "white box reuse", and the object combination is usually "black-box reuse."
Inheritance to some extent destroys the encapsulation, the subclass of the parent class coupling degree is high.
The combination of objects only requires a well-defined interface for the combined object, with a low degree of coupling.

7. Package Change Point
Using encapsulation to create a boundary layer between objects allows the designer to modify on one side of the boundary layer without adversely affecting the other side, allowing for loose coupling between layers.

8. Programming for interfaces, not for implementation
Instead of declaring a variable type as a specific class, you declare it as an interface.
The client does not need to know the exact type of the object, only the interface that the object has.
Reduce the dependencies of each part of the system, so as to achieve "high cohesion, loose coupling" type design scheme.

  Promote design principles to design experience

Designing Idioms Design idioms
Design idioms describes low-level patterns, techniques, and idioms related to specific programming languages.
Design Pattern Patterns
Design patterns mainly describes the relationship between classes and the objects that communicate with each other, including their roles, responsibilities, ways of collaboration, and so on.
Schema mode architectural Patterns
Architectural patterns describes the high-level patterns in the system that are closely related to the basic structure organization, including the division of subsystems, responsibilities, and rules for how to organize relationships between them.

C + + Design pattern (first week)

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.