學習記錄:《C++設計模式——李建忠主講》1.設計模式


1.學習目標

1)理解松耦合設計思想;

2)掌握面向對象設計原則;

3)掌握重構技法改善設計;

4)掌握GOF核心設計模式;

2.定義

每個設計模式描述了一個在我們周圍不斷重復發生的問題,以及該問題解決方案的核心。這樣,你就能一次又一次地使用該方案而不必做重復勞動。

3.思維模型:

1)底層思維:向下,如何把握機器底層從微觀理解對象構造。內容包括語言構造、編譯轉換、內存模型、運行時機制。

向下需要深入理解三大面向對象機制。

封裝:隱藏內部實現

繼承:復用現有代碼

多態:改變對象行為

2)抽象思維:向上,如何將我們的周圍世界抽象為成程序代碼。內容包括面向對象、組件封裝、設計模式、架構模式。

向上需要深刻把握面向對象機制所帶來的抽象意義,理解如何使用這些機制來表達現實世界,掌握什么是“好的面向對象設計”。

4.軟件設計復雜性的根本原因:變化

客戶需求的變化、技術平台的變化、開發團隊的變化、市場環境的變化。

5.如何解決復雜性

分解:分而治之,將大問題分解為多個小問題,將復雜問題分解為多個簡單問題。

//Shape1.h
class Point{ public: int x; int y; }; class Line{ public: Point start; Point end; Line(const Point& start, const Point& end){ this->start = start; this->end = end; } }; class Rect{ public: Point leftUp; int width; int height; Rect(const Point& leftUp, int width, int height){ this->leftUp = leftUp; this->width = width; this->height = height; } }; //增加 class Circle{
};
//MainForm1.cpp
class MainForm : public Form { private: Point p1; Point p2; vector<Line> lineVector; vector<Rect> rectVector; //改變 vector<Circle> circleVector; public: MainForm(){ //... } protected: virtual void OnMouseDown(const MouseEventArgs& e); virtual void OnMouseUp(const MouseEventArgs& e); virtual void OnPaint(const PaintEventArgs& e); }; void MainForm::OnMouseDown(const MouseEventArgs& e){ p1.x = e.X; p1.y = e.Y; //... Form::OnMouseDown(e); } void MainForm::OnMouseUp(const MouseEventArgs& e){ p2.x = e.X; p2.y = e.Y; if (rdoLine.Checked){ Line line(p1, p2); lineVector.push_back(line); } else if (rdoRect.Checked){ int width = abs(p2.x - p1.x); int height = abs(p2.y - p1.y); Rect rect(p1, width, height); rectVector.push_back(rect); } //改變 else if (...){ //... circleVector.push_back(circle); } //... this->Refresh(); Form::OnMouseUp(e); } void MainForm::OnPaint(const PaintEventArgs& e){ //針對直線 for (int i = 0; i < lineVector.size(); i++){ e.Graphics.DrawLine(Pens.Red, lineVector[i].start.x, lineVector[i].start.y, lineVector[i].end.x, lineVector[i].end.y); } //針對矩形 for (int i = 0; i < rectVector.size(); i++){ e.Graphics.DrawRectangle(Pens.Red, rectVector[i].leftUp, rectVector[i].width, rectVector[i].height); } //改變 //針對圓形 for (int i = 0; i < circleVector.size(); i++){ e.Graphics.DrawCircle(Pens.Red, circleVector[i]); } //... Form::OnPaint(e); }

抽象:人們處理復雜性問題的通用技術,即抽象。由於不能掌握全部的復雜對象,我們選擇忽視它的非本質細節,而去處理泛化和理想化了的對象模型。

//Shape2.h
class Shape{
public: virtual void Draw(const Graphics& g)=0; virtual ~Shape() { } }; class Point{ public: int x; int y; }; class Line: public Shape{ public: Point start; Point end; Line(const Point& start, const Point& end){ this->start = start; this->end = end; } //實現自己的Draw,負責畫自己 virtual void Draw(const Graphics& g){ g.DrawLine(Pens.Red, start.x, start.y,end.x, end.y); } }; class Rect: public Shape{ public: Point leftUp; int width; int height; Rect(const Point& leftUp, int width, int height){ this->leftUp = leftUp; this->width = width; this->height = height; } //實現自己的Draw,負責畫自己 virtual void Draw(const Graphics& g){ g.DrawRectangle(Pens.Red, leftUp,width,height); } }; //增加 class Circle : public Shape{ public: //實現自己的Draw,負責畫自己 virtual void Draw(const Graphics& g){ g.DrawCircle(Pens.Red, ...); } };
class MainForm : public Form {
private: Point p1; Point p2; //針對所有形狀 vector<Shape*> shapeVector; public: MainForm(){ //...  } protected: virtual void OnMouseDown(const MouseEventArgs& e); virtual void OnMouseUp(const MouseEventArgs& e); virtual void OnPaint(const PaintEventArgs& e); }; void MainForm::OnMouseDown(const MouseEventArgs& e){ p1.x = e.X; p1.y = e.Y; //...  Form::OnMouseDown(e); } void MainForm::OnMouseUp(const MouseEventArgs& e){ p2.x = e.X; p2.y = e.Y; if (rdoLine.Checked){ shapeVector.push_back(new Line(p1,p2)); } else if (rdoRect.Checked){ int width = abs(p2.x - p1.x); int height = abs(p2.y - p1.y); shapeVector.push_back(new Rect(p1, width, height)); } //改變 else if (...){ //...  shapeVector.push_back(circle); } //... this->Refresh(); Form::OnMouseUp(e); } void MainForm::OnPaint(const PaintEventArgs& e){ //針對所有形狀 for (int i = 0; i < shapeVector.size(); i++){ shapeVector[i]->Draw(e.Graphics); //多態調用,各負其責  } //...  Form::OnPaint(e); }

在繪制點、線、矩形的問題上,分解和抽象兩種思維下的解決方案是不一樣的。在出現了新需求(畫圓)后,兩種方案應對策略也是不同的。

3.6 軟件設計的目標:復用!

參考:《李建忠C++設計模式》視頻

https://www.bilibili.com/video/av64805906/?p=1


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM