设计模式解析 第二版

上传人:lis****211 文档编号:154871142 上传时间:2022-09-21 格式:DOCX 页数:65 大小:47.02KB
收藏 版权申诉 举报 下载
设计模式解析 第二版_第1页
第1页 / 共65页
设计模式解析 第二版_第2页
第2页 / 共65页
设计模式解析 第二版_第3页
第3页 / 共65页
资源描述:

《设计模式解析 第二版》由会员分享,可在线阅读,更多相关《设计模式解析 第二版(65页珍藏版)》请在装配图网上搜索。

1、设计模式解析(C+代码)个人(上传者)简述一Resources Design Patterns Explained C+ Code Examples - Chapter 4: A Standard Object-Oriented Solution资源 设计模式实例解释了 C+代码第四章:一个标准的面向对象的解决方案Example 4-1: Instantiating the V1 Features例 4:实例化的特征。V1TOCTOCModel模型V1ModelV1ModelFeature 特征V1V1本人第一次软件设计师考试失利,但是我没有放弃,也没有气馁因为我觉得是自己的基础不扎实,所 以

2、我还在努力学习,设计模式是非常重要的一门学科,所以我们都得学好它。.Example 4-2: Implementation of V1 Methods 例 4:实施 V1 方法TOCTOC CutoutFeatureCutoutFeature HoleFeatureHoleFeature IrregularFeatureIrregularFeature SlotFeatureSlotFeature SpecialFeatureSpecialFeature V1CutoutV1Cutout V1HoleV1Hole V1IrregularV1Irregular V1SlotV1Slot V1Sp

3、ecialV1SpecialExample 4-3: Instantiating the V2 Features 例 4:实例化 V2 飞弹的特点 TOCTOC V2ModelV2Model V2CutoutV2Cutout V2HoleV2Hole V2IrregularV2Irregular V2SpecialV2Special OOGFeatureOOGFeatureExample 4-4: Implementation of V2 Methods 例子:实施中 V2 的方法TOCTOC V2SlotV2SlotModel模型V1ModelV1ModelFeature 特征V1V1Exa

4、mple 4-1: Instantiating the V1 Features例 4:实例化的特征。V1Model模型TOP最高Model.hModel.h#pragma once#include Feature.h”class Modelpublic:Model(void);protected:Feature *features;int nElements;public:virtual Model(void);Model.cppModel.cpp#include Model.h”Model:Model(void)Model:Model(void)V1ModelV1ModelTOP最高V1Mo

5、del.hV1Model.h#pragma once#include model.h”#include using namespace std;class V1Model : public Modelpublic:Model(void);static int openModel( string ModelName);static Model* buildV1Model(string);void buildModel();private:void buildV1Model();int nElements;int myModelNumber;int getNumberOfElements();pu

6、blic:V1Model(void);V1Model.cppV1Model.cpp #include VIModel.h”#include Feature.h”#include Vl.h”#include VlSlot.h”#include VlHole.h”V1Model:V1Model(void)V1Model:V1Model(void)/ segment of code that instantiates the features/ no error checking providedfor illustration/ purposes onlyModel* V1Model:buildV

7、1Model (string modelName)int modNum;modNum= V1Model:openModel( modelName);if (modNum myModelNumber= modNum;myModel-buildModel();return myModel;void V1Model:buildModel()/ each feature object needs to know the model number/ and feature ID it corresponds to in order to/ retrieve information when reques

8、ted. Note how/ this information is passed to each objects/ constructornElements= getNumberOfElements();features= new Feature* nElements;/ do for each feature in the modelint i;int ID;for (i= 0; i nElements; i+)/ determine feature present and create/ appropriate feature objectID= V1:getFeatureID( myM

9、odelNumber, i);switch( V1:getFeatureType( myModelNumber, ID)case FEATURE_SLOT:featuresi= new V1Slot( myModelNumber, ID); break;case FEATURE_HOLE:featuresi= new V1Hole( myModelNumber, ID); break;/ Other casesint V1Model:getNumberOfElements ()/ get the number of Elements from V1/ for code example, jus

10、t return 1;return 1;Feature 特征TOP最高Feature.hFeature.h#pragma once#include definitions.h”#include using namespace std;class Featurepublic:Feature(void);public:virtual Feature(void);Feature.cppFeature.cpp#include Feature.h”Feature:Feature(void)Feature:Feature(void)V1V1TOP最高Vl.hVl.h#pragma once#include

11、 definitions.h”class V1public:V1(void);static FEATURE_TYPES getFeatureType( int, int);static int getFeatureID( int, int );static double getXforHole( int, int);static double getX1forSlot( int, int);static double getX2forSlot( int, int);public:V1(void);V1.cppV1.cpp#include V1.hV1:V1(void)V1:V1(void)do

12、uble V1:getXforHole( int, int)return 0;double V1:getX1forSlot( int, int)return 0;double V1:getX2forSlot( int, int)return 0;Example 4-2: Implementation of V1 Methods 例 4:实施 V1 方法 CutoutFeatureCutoutFeature HoleFeatureHoleFeature IrregularFeatureIrregularFeature SlotFeatureSlotFeature SpecialFeatureSp

13、ecialFeature V1CutoutV1Cutout V1HoleV1Hole V1IrregularV1Irregular V1SlotV1Slot V1SpecialV1SpecialCutoutFeatureCutoutFeatureTOP最高CutoutFeature.hCutoutFeature.h#pragma once#include feature.h” class CutoutFeature :public Feature public:CutoutFeature(void);public:CutoutFeature(void);;CutoutFeature.cppCu

14、toutFeature.cpp#include CutoutFeature.h”CutoutFeature:CutoutFeature(void)CutoutFeature:CutoutFeature(void)HoleFeatureHoleFeatureTOP最高HoleFeature.hHoleFeature.h#pragma once#include feature.h”class HoleFeature :public Featurepublic:HoleFeature(void);public:HoleFeature(void);HoleFeature.cppHoleFeature.

15、cpp#include HoleFeature.h”HoleFeature:HoleFeature(void)HoleFeature:HoleFeature(void)IrregularFeatureIrregularFeatureTOP最高IrregularFeature.hIrregularFeature.h#pragma onceclass IrregularFeaturepublic:IrregularFeature(void);public:virtual IrregularFeature(void);;IrregularFeature.cppIrregularFeature.cpp

16、#include IrregularFeature.h”IrregularFeature:IrregularFeature(void)IrregularFeature:IrregularFeature(void)SlotFeatureSlotFeatureTOP最高SlotFeature.hSlotFeature.h#pragma once#include feature.h”class SlotFeature :public Featurepublic:SlotFeature(void);public:SlotFeature(void);SlotFeature.cppSlotFeature.

17、cpp#include SlotFeature.h”SlotFeature:SlotFeature(void)SlotFeature:SlotFeature(void)SpecialFeatureSpecialFeatureTOP最高SpecialFeature.hSpecialFeature.h#pragma once#include feature.h”class SpecialFeature :public Featurepublic:SpecialFeature(void);public:SpecialFeature(void);;SpecialFeature.cppSpecialFe

18、ature.cpp#include SpecialFeature.h”SpecialFeature:SpecialFeature(void)SpecialFeature:SpecialFeature(void)V1CutoutV1CutoutTOP最高V1Cutout.hV1Cutout.h#pragma once#include cutoutfeature.h”class V1Cutout :public CutoutFeaturepublic:V1Cutout(int, int);public:V1Cutout(void);private:int myModelNumber;int myI

19、D;V1Cutout.cppV1Cutout.cpp#include V1Cutout.hV1Cutout:V1Cutout(int modelNumber, int ID) myModelNumber = modelNumber;myID = ID;V1Cutout:V1Cutout(void)V1HoleV1HoleTOP最高V1Hole.hV1Hole.h#pragma once#include holefeature.h class V1Hole :public HoleFeature public:V1Hole(int, int);double getX();public:V1Hol

20、e(void);private:int myModelNumber;int myID;V1Hole.cppV1Hole.cpp#include V1Hole.h#include V1.hV1Hole:V1Hole(int modelNumber, int ID)myModelNumber = modelNumber;myID = ID;V1Hole:V1Hole(void)/ myModelNumber and myID are private members containing/ information about the model and feature (in V1) this/ f

21、eature corresponds todouble V1Hole:getX()return V1:getXforHole( myModelNumber, myID);V1IrregularV1IrregularTOP最高V1Irregular.hV1Irregular.h#pragma once#include irregularfeature.h”class V1Irregular :public IrregularFeaturepublic:V1Irregular(int, int);public:V1Irregular(void);private:int myModelNumber;

22、int myID;V1Irregular.cppV1Irregular.cpp#include V1Irregular.hV1Irregular:V1Irregular(int modelNumber, int ID) myModelNumber = modelNumber;myID = ID;V1Irregular:V1Irregular(void)V1SlotV1SlotTOP最高V1Slot.hV1Slot.h#pragma once#include slotfeature.h”class V1Slot :public SlotFeaturepublic:V1Slot(int, int)

23、;double getX1();double getX2();public:V1Slot(void);private:int myModelNumber;int myID;V1Slot.cppV1Slot.cpp#include V1Slot.h#include V1.hV1Slot:V1Slot(int modelNumber, int ID)myModelNumber = modelNumber;myID = ID;V1Slot:V1Slot(void)/ myModelNumber and myID are private members containing/ information

24、about the model and feature (in V1) this/ feature corresponds todouble V1Slot:getX1 ()return V1:getX1forSlot( myModelNumber, myID);double V1Slot:getX2 ()return V1:getX2forSlot( myModelNumber, myID);V1SpecialV1SpecialTOP最高V1Special.hV1Special.h#pragma once#include specialfeature.h”class VISpecial :pu

25、blic SpecialFeaturepublic:V1Special(int, int);public:V1Special(void);private:int myModelNumber;int myID;V1Special.cppV1Special.cpp#include V1Special.hV1Special:V1Special(int modelNumber, int ID)myModelNumber = modelNumber;myID = ID;V1Special:V1Special(void)Example 4-3: Instantiating the V2 Features

26、例 4:实例化 V2 飞弹的特点 V2ModelV2Model V2CutoutV2Cutout V2HoleV2Hole V2IrregularV2Irregular V2SpecialV2Special OOGFeatureOOGFeatureV2ModelV2ModelTOP最高V2Model.hV2Model.h#pragma once#include model.h”#include using namespace std;#include OOGFeature.h”#include definitions.h” class V2Model :public Modelpublic:V

27、2Model(void);static int openModel( string ModelName);static Model* buildV2Model(string);void buildModel();int getNumberOfElements();OOGFeature *getElement(int);public:V2Model(void);V2Model.cppV2Model.cpp#include V2Model.h#include V2Slot.h#include V2Hole.hV2Model:V2Model(void)V2Model:V2Model(void)/ s

28、egment of code that instantiates the features/ no error checking providedfor illustration/ purposes onlyModel* V2Model:buildV2Model (string modelName)/ open modelint modNum;modNum= V2Model:openModel( modelName);if (modNum openModel( modelName) return 0l;myModel-buildModel();return myModel;void V2Mod

29、el:buildModel()/ each feature object needs to know the feature in the/ V2 system it corresponds to in order to retrieve/ information when requested. Note how this information/ is passed into each objects constructornElements= getNumberOfElements();OOGFeature *oogF= new OOGFeature;/ do for each featu

30、re in the model int i;for (i= 0; i getType()case OOGFEATURE_SLOT:featuresi= new V2Slot( oogF); break;case OOGFEATURE_HOLE:featuresi= new V2Hole( oogF); break;/ other casesint V2Model:getNumberOfElements ()/ get the number of Elements from V1/ for code example, just return 1;return 1;V2CutoutV2Cutout

31、TOP最高V2Cutout.hV2Cutout.h#pragma once#include cutoutfeature.h”#include OOGFeature.h”class V2Cutout :public CutoutFeaturepublic:V2Cutout(OOGFeature *feature);public:V2Cutout(void);private:OOGFeature *myOOGFeature;V2Cutout.cppV2Cutout.cpp#include V2Cutout.h#include OOGFeature.h”V2Cutout:V2Cutout(OOGFe

32、ature *feature)myOOGFeature= feature;V2Cutout:V2Cutout(void)V2HoleV2HoleTOP最高V2Hole.hV2Hole.h#pragma once#include holefeature.h”#include OOGFeature.h”class V2Hole :public HoleFeaturepublic:V2Hole(OOGFeature *feature);double getX();public:V2Hole(void);private:OOGFeature *myOOGFeature;V2Hole.cppV2Hole

33、.cpp#include V2Hole.h#include OOGFeature.h”V2Hole:V2Hole(OOGFeature *feature)myOOGFeature= feature;V2Hole:V2Hole(void)double V2Hole:getX ()return myOOGFeature-getX1();V2IrregularV2IrregularTOP最高V2Irregular.hV2Irregular.h#pragma once#include irregularfeature.h”#include OOGFeature.h” class V2Irregular

34、 :public IrregularFeaturepublic:V2Irregular(OOGFeature *feature);public:V2Irregular(void);private:OOGFeature *myOOGFeature;;V2Irregular.cppV2Irregular.cpp#include V2Irregular.h#include OOGFeature.h”V2Irregular:V2Irregular(OOGFeature *feature)myOOGFeature= feature;V2Irregular:V2Irregular(void)V2Speci

35、alV2SpecialTOP最高V2Special.hV2Special.h#pragma once#include specialfeature.h”#include OOGFeature.h”class V2Special :public SpecialFeaturepublic:V2Special(OOGFeature *feature);public:V2Special(void);private:OOGFeature *myOOGFeature;V2Special.cppV2Special.cpp#include V2Special.h#include OOGFeature.h”V2

36、Special:V2Special(OOGFeature *feature)myOOGFeature= feature;V2Special:V2Special(void)OOGFeatureOOGFeatureTOP最高OOGFeature.hOOGFeature.h#pragma once#include definitions.h”class OOGFeaturepublic:OOGFeature(void);double getX1();double getX2();OOGFEATURE_TYPES getType();public:OOGFeature(void);OOGFeature

37、.cppOOGFeature.cpp#include OOGFeature.h”OOGFeature:OOGFeature(void)OOGFeature:OOGFeature(void)double OOGFeature:getX1()return 0;double OOGFeature:getX2()return 0;Example 4-4: Implementation of V2 Methods 例子:实施中 V2 的方法V2SlotV2SlotV2SlotV2SlotTOP最高V2Slot.hV2Slot.h#pragma once#include slotfeature.h”#in

38、clude OOGFeature.h”class V2Slot :public SlotFeaturepublic:V2Slot(OOGFeature *feature);double getX1();double getX2();public:V2Slot(void);private:OOGFeature *myOOGFeature;V2Slot.cppV2Slot.cpp#include V2Slot.h#include OOGFeature.h”V2Slot:V2Slot(OOGFeature *feature)myOOGFeature= feature;V2Slot:V2Slot(vo

39、id)double V2Slot:getX1 ()return myOOGFeature-getX1();double V2Slot:getX2 ()return myOOGFeature-getX2();Example 7-1: Implementing the Adapter PatternTOC Shape Circle XX_CircleShapeTOPShape.h#pragma onceclass Shapepublic:Shape(void);void virtual display()= 0 public:Shape(void);Shape.cpp#include Shape.

40、h”Shape:Shape(void)Shape:Shape(void)CircleTOPCircle.h#pragma once#include Shape.h”#include XX_Circle.hclass Circle : public Shapepublic:Circle(XX_Circle *xxCircle);void display();public:Circle(void);private:XX_Circle *myXX_Circle;Circle.cpp#include Circle.h”Circle:Circle(XX_Circle *xxCircle)myXX_Cir

41、cle = xxCircle;Circle:Circle(void)delete myXX_Circle;void Circle:display()myXX_Circle-displayIt();XX_CircleTOPXX_Circle.h#pragma onceclass XX_Circlepublic:XX_Circle(void);void displayIt();public:XX_Circle(void);XX_Circle.cpp#include XX_Circle.hXX_Circle:XX_Circle(void)XX_Circle:XX_Circle(void)Exampl

42、e 9-1: Implementing the Strategy PatternTOC TaxController SalesOrder CalcTax CanTax USTaxTaxControllerTOPTaxController.h#pragma once#include CalcTax.h”class TaskControllerpublic:TaskController(void);void process();CalcTax *getTaxRulesForCountry();public:TaskController(void);TaxController.cpp#include

43、 TaskController.h”#include SalesOrder.h”#include CalcTax.h”#include USTax.h”TaskController:TaskController(void)TaskController:TaskController(void)void TaskController:process ()/ this code is an emulation of a/ processing task controller/ ./ figure out which country you are in CalcTax *myTax;myTax= g

44、etTaxRulesForCountry();SalesOrder *mySO= new SalesOrder();mySO-process( myTax);CalcTax *TaskController:getTaxRulesForCountry() / In real life, get the tax rules based on / country you are in. You may have the/ logic here or you may have it in a/ configuration file/ Here, just return a USTax so this

45、/ will compile.return new USTax;SalesOrderTOPSalesOrder.h#pragma once#include CalcTax.h”class SalesOrderpublic:SalesOrder(void);void process (CalcTax *TaxToUse);public:SalesOrder(void);SalesOrder.cpp#include SalesOrder.h”SalesOrder:SalesOrder(void)SalesOrder:SalesOrder(void)void SalesOrder:process (

46、CalcTax *taxToUse)long itemNumber= 0;double price= 0;/ given the tax object to use/ ./ calculate taxdouble tax= taxToUse-taxAmount( itemNumber, price); CalcTaxTOPCalcTax.h#pragma onceclass CalcTaxpublic:CalcTax(void);double virtual taxAmount( long, double)= 0;public:CalcTax(void);;CalcTax.cpp#includ

47、e CalcTax.h”CalcTax:CalcTax(void)CalcTax:CalcTax(void)CanTaxTOPCanTax.h#pragma once#include calctax.h”class CanTax :public CalcTaxpublic:CanTax(void);double taxAmount( long, double);public:CanTax(void);CanTax.cpp#include CanTax.h”CanTax:CanTax(void)CanTax:CanTax(void)double CanTax:taxAmount (long it

48、emSold, double price)/ in real life, figure out tax according to/ the rules in Canada and return it/ here, return 0 so this will compilereturn 0.0;USTaxTOPUSTax.h#pragma once#include calctax.h class USTax :public CalcTax public:USTax(void);double taxAmount( long, double);public:USTax(void);USTax.cpp

49、#include USTax.h”USTax:USTax(void)USTax:USTax(void)double USTax:taxAmount (long itemSold, double price)/ in real life, figure out tax according to/ the rules in the US and return it/ here, return 0 so this will compilereturn 0.0;Resources Design Patterns Explained C+ Code Examples - Chapter 10: The

50、Bridge Pattern Example 10-1TOC Rectangle VIRectangle V2Rectangle DPI DP2Example 10-2TOC Shape Circle Rectangle VICircle VIRectangle V2Circle V2Rectangle DPI DP2Example 10-3TOC Client Factory Shape Circle Rectangle Drawing V1Drawing V2Drawing DP1 DP2Example 10-1 Rectangle V1Rectangle V2Rectangle DP1

51、DP2RectangleTOPRectangle.h#pragma onceclass Rectanglepublic:Rectangle(double, double, double, double);virtual void drawLine(double, double, double, double)=0;void draw();public:Rectangle(void);private:double _x1, _x2, _y1, _y2;Rectangle.cpp#include Rectangle.h”Rectangle:Rectangle(double x1, double y

52、1, double x2, double y2)_x1= x1; _y1= y1; _x2= x2; _y2= y2;Rectangle:Rectangle(void)void Rectangle:draw()drawLine( _x1, _y1, _x2, _y1);drawLine( _x2, _y1, _x2, _y2);drawLine( _x2, _y2, _x1, _y2);drawLine( _x1, _y2, _x1, _y1);VIRectangleTOPVIRectangle.h#pragma once#include Rectangle.h”class V1Rectang

53、le : public Rectanglepublic:V1Rectangle(double, double, double, double);void drawLine( double, double, double, double);public:V1Rectangle(void);VIRectangle.cpp#include V1Rectangle.h#include DP1.hV1Rectangle:V1Rectangle(double x1, double y1, double x2, double y2):Rectangle(x1, y1, x2, y2)V1Rectangle:

54、V1Rectangle(void)void V1Rectangle:drawLine( double x1, double y1, double x2, double y2)DP1:draw_a_line( x1, y1, x2, y2);V2RectangleTOPV2Rectangle.h#pragma once#include Rectangle.h”class V2Rectangle : public Rectanglepublic:V2Rectangle(double, double, double, double);void drawLine( double, double, do

55、uble, double);public:V2Rectangle(void);V2Rectangle.cpp#include V2Rectangle.h#include DP2.hV2Rectangle:V2Rectangle(double x1, double y1, double x2, double y2):Rectangle(x1, y1, x2, y2)V2Rectangle:V2Rectangle(void)void V2Rectangle:drawLine( double x1, double y1, double x2, double y2)DP2:drawline( x1,

56、y1, x2, y2);DPITOPDPl.h#pragma onceclass DP1public:DP1(void);static void draw_a_line( double, double, double, double);static void draw_a_circle( double, double, double);public:DP1(void);DPl.cpp#include DP1.hDP1:DP1(void)DP1:DP1(void)void DP1:draw_a_line( double x1, double y1, double x2, double y2)/ draw the linevoid DP1:draw_a_circle( double x, double y, doub

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!