第12章

更新时间:2024-07-05 02:56:02 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

CHAPTER12

UML模型的C++实现 12.1 概述

图12-1 Account类

// File : Account.h

class Account { public: // 默认的构造函数和析构函数 Account(); ~Account();

// 用户定义的方法

void PrintAmount(); private: //用户定义的属性 int amount;

};

// File: Account.cpp #include \Account::Account() { // 构造函数体 }

第12286 U ML与软件建模

Account::~Account() { // 析构函数体 }

void Account::PrintAmount() { //此处方法体由程序人员自己给出,或根据UML模型中的其他信息自动生成 }

12.2 属性和方法的映射

virtual 类型名 方法名([参数表]) = 0; 类别 形式参数名:类型表达式 = 默认值

图12-2 爆炸类、核爆炸类和爆炸碎片类之间的关系

// File:Detonation.h class Detonation {

public: virtual ~Detonation( );

virtual const Location& location( ) const; protected:

Detonation( );

Virtual void location(const Location& loc);

private: Location location; };

// File: NuclearDetonation.h

class NuclearDetonation : public virtual Detonation { public:

287 第12章 UML模型的C++实现

NuclearDetonation( );

NuclearDetonation(const NuclearDetonation&);

~NuclearDetonation( );

NuclearDetonation& operator = (const NuclearDetonation&);

protected: private: DebrisPatch* debrisPatch; }

// File: DebrisPatch.h class DebrisPatch { public:

DebrisPatch( );

DebrisPatch(const DebrisPatch&);

~DebrisPatch( );

DebrisPatch operator = (const DebrisPatch&);

protected: private: NuclearDetonation* nuclearDetonation; };

12.3 泛化与特化关系的映射

图12-3 Employee及其子类之间的关系

// File: VestedHourlyEmployee.h #include \#include \

... class VestedHourlyEmployee: virtual public VestedEmployee, virtual public HourlyEmployee { ...

288 U ML与软件建模

}

12.4 关联关系的映射

表12-1 关联关系映射表

关联多重性 1 * *{ordered} C++结构 指针 指针集合 有序的指针集合 12.4.1 单向关联的映射 12.4.2 双向关联的映射

Customer0..1cust1bookBook

图12-4 顾客和书之间的关系

// File: Customer.h ...

class Customer { friend class Book; public: ...

// Association accessor methods void setBook(Book* newBook); const Book* getBook() const;

protected: ... private: };

...

//File: Book.h ...

#ifndef CUSTOMER_H #include \#endif

...

// Association attribute storage Book* bookptr;

289 第12章 UML模型的C++实现

...

class Book {

friend class Customer; public:

...

// Association accessor methods void setCust(Customer* newCust); const Customer* getCust() const;

... protected: ... private: }; ...

...

// Association attribute storage Customer* custPtr;

12.4.3 强制对可选关联的映射

A1a0..1bB

图12-5 类A和B之间的可选关联

// File: B.h ...

class B { friend class A; public:

// Default constructor/destructor B(const A& a);

... }; ...

12.4.4 强制对强制关联的映射

A1a1bB

图12-6 A和B之间的强制对强制关联

290 U ML与软件建模

12.4.5 可选对可选关联的映射

A0..1a0..1bB

图12-7 A和B之间的可选对可选关联

(a) (b) (c)

图12-8 可选对可选关联关系的更新

12.4.6 可选对多关联的映射

A0..1a*bB

图12-9 可选对多关联

(a) (b) (c)

图12-10 可选对多关联关系的更新

// File: A.h #include #include %using namespace std; ...

291 第12章 UML模型的C++实现

class A { friend class B; public:

A( ); ~A( );

...

const set& getptrSet( ) const; void addB(B* b) ; void removeB(B* b); ...

private: set ptrSet; }; ...

12.4.7 强制对多关联的映射

A1a*bB

图12-11 强制对多关联

12.4.8 多对多关联的映射

A*a*bB

图12-12 多对多关联

(a) (b) (c)

图12-13 多对多关联关系的更新

12.4.9 有序关联的映射

A0..1a{ordered}*bB

292 U ML与软件建模

图12-14 有序关联

// File: A.h #include #include \

using namespace std; ...

class A { friend class B; public: A( ); ~A( ); ... const list& getptrSet( ) const; void addB(B* b) ; void removeB(B* b); ... private: list ptrSet; }; ...

12.4.10 关联类的映射

Customer0..1customer*bookBookCustomer1custBook1book

Purchase*Purchase0..1purchaseOfCustpurchaseOfBook (a) (b)

图12-15 关联类的映射

头文件Purchase.h //File: Purchase.h ...

class Purchase {

friend class Customer; friend class Book;

public: Purchase(const Customer& customer, const Book& book);

~Purchase( ); ...

// Association accessor methods

void setCustomer(Customer* newCustomer);

293 第12章 UML模型的C++实现

const Customer* getCustomer( ) const; void setBook(Book* newBook); const Book* getBook( ) const;

protected: ... private:

...

// Association attribute storage Customer* customPtr; Book* bookPtr;

}; ...

头文件Customer.h // File: Customer.h ...

class Customer { }; ...

头文件Book.h // File: Book.h ...

class Book {

friend class Purchase; ...

// Association accessor methods

void setCustomer(Purchase* newCustomer); const Purchase* getCustomer( ) const; public:

friend class Purchase; ...

//Association accessor methods

const CmapPtrToPtr& getBookSet( ) const; void addBook(Purchase* newBook); ... ...

//Association attribute storage CmapPtrToPtr bookOfPurchaseSet; public:

protected: private:

294 U ML与软件建模

protected: }; ...

... ...

// Association attribute storage Purchase* customerOfPurchasePtr; private:

12.5 受限关联关系的映射

CustomerBookID0..1customer*bookBook

图12-16 受限关联

表12-2 受限关联映射表

非限定符端的多重性 1,0..1 1,0..1{ordered} // File: Customer.h #include #include #include #include %using namespace std; ...

class Customer { public:

...

// Association accessor methods

const set& getBookSet(String BookID) const; void addBook(String BookID, Book* newBook); void removeBook(String BookID, Book* oldBook);

实现策略 Map Map 非限定符端的多重性 * *{ordered} 实现策略 Multimap Multimap ... private: }

...

// Association attribute storage

multimap bookDictSet;

295 第12章 UML模型的C++实现

...

12.5.1 强制对强制(可选/多)受限关联的映射

AQ1a0..1bB

图12-17 强制对可选受限关联

12.5.2 可选对可选受限关联的映射

AQ0..1a0..1bB

图12-18 可选对可选受限关联

(a) (b) (c)

图12-19 可选对可选受限关联的更新

12.5.3 可选对强制受限关联的映射

AQ0..1a1bB

图12-20 可选对强制受限关联

12.5.4 可选对多受限关联的映射

AQ0..1a*bB

图12-21 可选对多受限关联

296 U ML与软件建模

(a) (b) (c)

图12-22 可选对多受限关联的更新

12.5.5 多对可选受限关联的映射

AQ*a0..1bB

图12-23 多对可选受限关联

12.5.6 多对强制受限关联的映射

(a) (b) (c)

图12-24 多对可选受限关联的更新

AQ*a1bB

图12-25 多对强制受限关联

12.5.7 多对多受限关联的映射

AQ*a*bB

图12-26 多对多受限关联

297 第12章 UML模型的C++实现

(a) (b) (c)

图12-27 多对多受限关联的更新

有序受限关联的映射

CustomerBookID0..1{ordered}*custbookBook

图12-28 有序受限关联

聚合关系和组合关系的映射

IntList<>Int(from C++) + IntList()+first + insert(int) : voidIntLink+data + print() : void0..10..1+next图12-29 聚合和组合关系示例

// File: IntLink.h ...

class IntLink { public: int data;

IntLink *next; ...

}; ...

// File: IntList.h #include \

12.5.8 12.6

298 U ML与软件建模

...

class IntList { public: IntLink *first; }; ...

...

12.7 特殊类的映射

12.7.1 枚举类的映射

<>PrimaryColour Red Blue Green

图12-30 枚举类型PrimaryColour

// File: PrimaryColour.h ...

enum PrimaryColour { }; ...

Red, Blue, Green

12.7.2 模板的映射

图12-31 模板示例

299 第12章 UML模型的C++实现

// File: Farray.h ...

template class Farray { public: Farray( );

~Farray( );

void insert(T x, int k);

... };

// File: AdressList.h #include \

...

typedef Farray AddressList; // File: AdressList.h #include \...

class AddressList : public virtual Farray { public: AddressList( ); virtual ~AddressList( ); protected: private: };

12.7.3 接口类的映射

图12-32 接口类示例

// File: Aircraft.h ...

300 U ML与软件建模

class Aircraft { public: Aircraft( ); virtual ~Aircraft( ) = 0; virtual void takeoff( ) = 0; virtual void fly( ) = 0; virtual void land( ) = 0; ... };

// File: Airplane.h #include \...

class Airplane : virtual public Aircraft { public: void takeoff( ); void fly( ); void land( ); void bank(int degrees); ... };

//File: Pilot.h

#include \...

class Pilot { public: ...

private: Aircraft* myAircraft; };

12.7.4 实用工具类的映射

<>Math Math + random() : Double + sin(: Angle) : Double + land(: Angle) : Double图12-33 实用工具类Math

// File: Math.h ...

301 第12章 UML模型的C++实现

class Math { public: static double random( ); static double sin(const Angle&); static double cos(const Angle&); protected: private: Math( ); };

12.8 包的映射

User InterfaceBusiness LogicDatabase

图12-34 UML包示例

namespace Database { }

class Query {...}; class Table {...}; ...

namespace BusinessLogic { }

namespace UserInterface { using namespace BusinessLogic;

class DialogBox {...}; class Menu {...};

using namespace Database; class Transaction {...}; class Customer {...}; ...

302 U ML与软件建模

}

...

12.9 项目管理系统实例

添加项目分配资源 管理项目项目经理

更新项目项目经理释放资源删除项目

图12-35 项目管理系统的用案图(初始版) 图12-36 项目管理系统的用案图(详细版)

12.9.1 类图的映射

WorkEffortProject11..*Activity1..*1..*WorkProductResourceXSkill- YearsExpr : float11..*Task0..51Assigned toResource**Skill

图12-37 项目管理系统的类图

template

class CollectionByVal { ... };

303 第12章 UML模型的C++实现

template

class CollectionByRef { ... };

class WorkEffort; class Project; class Activity; class Task;

class WorkProduct; class Resource; class Skill;

class ResourceXSkill; class WorkEffort { ... };

class Project : public WorkEffort { private:

CollectionByVal theActivity; };

class Activity : public WorkEffort { private: Project *theProject;

CollectionByVal theTask;

CollectionByRef theWorkProduct;

};

class Task: public WorkEffort { private: Resource *theResource; };

class WorkProduct {

... };

class Resource { private: Task *theTask[6]; CollectionByRef theResourceXSkill; };

class Skill { private: CollectionByRef theResourceXSkill; };

class ResourceXSkill { private:

304 U ML与软件建模

Resource *theResource; Skill *theSkill;

float YearsExpr; };

Project Name : char * {private} ? Descr : char * ? StartDate : Date ? NumberOfProjects : int = 0 + <> Project(Name : char *) : Project + <> Project(void) : Project + <> ~Project(void) + getName(void) : char * + setName(theName: char *) : void setDescr(Descr : char *) : void {public} getDescr(void) : char * {public} + setStartDate(theStartDate : Date) : void getStartDate(void) : Date {public} # hasActivities(void) : bool + addActivity(theActivity : const Activity &) : void + getAllActivities(void) : CollectionByRef + getNumberOfProjects(void) : int + save(void) : void + load(Name : char *) : void 图12-38 项目管理系统中的Project类

template class CollectionByRef { ...

public: addToCollection(T *elementRef); };

class Date;

class Project;

class Activity;

class Date { ... };

removeFromCollection(char *criteria);

305 第12章 UML模型的C++实现

class Project {

private: char *Name;

char *Descr; Date StartDate;

static int NumberOfProjects; public: Project(char *Name);

Project(void); ~Project(void);

char *getName(void);

void setName(char *theName); void setDescr(char *Descr); char *getDescr(void);

void setStartDate(Date theStartDate);

Date getStartDate(void);

void addActivity(const Activity &theActivity); CollectionByRef getAllAcitivities(void); static int getNumberOfProjects(void); void save(void);

void load(char *Name);

protected: bool hasActivities(void); };

int Project::NumberOfProjects = 0; class Activity { ... };

306 U ML与软件建模

12.9.2 对象图的映射

图12-39 项目管理系统的对象图

class Project;

class Activity;

class Task;

class WorkProduct;

class Resource;

class Project { public: void addActivity(const Activity &theActivity); };

class Activity { public: void setProject(const Project &theProject); void addTask(const Task &theTask); void addWorkProduct(const WorkProduct &theWorkProduct); };

class Task { public:

307 第12章 UML模型的C++实现

void assignTo(const Resource &theResource); };

class WorkProduct { ... };

class Resource { public: void addTask(const Task &theTask); };

void main(void) { Project RMS; WorkProduct ReqDoc, ArchDoc; Activity Scope, AnalysisDesign; Task GatherReq, Review, Analyze, Design; Resource Phillip, Nora, Si; RMS.addActivity(Scope); RMS.addActivity(AnalysisDesign); Scope.setProject(RMS); Scope.addWorkProduct(ReqDoc); AnalysisDesign.setProject(RMS); AnalysisDesign.addWorkProduct(ReqDoc); AnalysisDesign.addWorkProduct(ArchDoc); Scope.addTask(GatherReq); Scope.addTask(Review); AnalysisDesign.addTask(Analyze); AnalysisDesign.addTask(Design); GatherReq.assignTo(Phillip); Review.assignTo(Nora); Analyze.assignTo(Nora); Design.assignTo(Si); Phillip.addTask(GatherReq); Nora.addTask(Review); Nora.addTask(Analyze); Si.addTask(Design); }

308 U ML与软件建模

12.9.3 序列图的映射

:Project Manger Project Manger :User Interface 提 供 项 目 的 名 称 、 描 述 及 开 工 日 期 Project() setName(Name) setDescr(descr) setStartDate (StartDate) Save() ~Project() :Project

图12-40 项目管理系统的序列图

class Date;

class Project;

class Date { };

class Project { public:

Project(void);

~Project(void);

void setName(char *theName); void setDescr(char *Descr); ...

309 第12章 UML模型的C++实现

};

void main(void) { }

char *Name; char *Descr; Date StartDate;

//提供项目的名、描述及开工日期 Project aProject; aProject.setName(Name); aProject.setDescr(Descr);

aProject.setStartDate(StartDate); aProject.save();

void setStartDate(Date theStartDate); void save(void);

12.9.4 协作图的映射

图12-41 项目管理系统的协作图

310 U ML与软件建模

12.9.5 状态图的映射

HiredUnassigned[NumberOfAssignedTasks = 1]Un-assign Taskfrom Resource /DecrementNumberOfAssignedTasksTerminatedAssign Task to Resource /IncrementNumberOfAssignedTasksAssigned[NumberOfAssignedTasks > 1]Un-assign Tasks from Resource /DecrementNumberOfAssignedTasksAssign Task to Resource /IncrementNumberOfAssignedTasks

图12-42 项目管理系统的状态图

class Task;

class Resource;

class Task {

// Details omitted };

class Resource {

private:

int NumberOfAssignedTasks; bool Assigned;

public: };

void addTask(const Task &theTask); // Assign Task to Resource void removeTask(const Task &theTask); // Un-assign Task from Resource

311 第12章 UML模型的C++实现

void Resource::addTask(const Task &theTask) { }

void Resource::removeTask(const Task &theTask) { }

// Details omitted

if (NumberOfAssignedTasks == 1) Assigned = false;

NumberOfAssignedTasks--; // Details omitted // Details omitted if (!Assigned) Assigned = true;

NumberOfAssignedTasks++; // Details omitted

312 U ML与软件建模

12.9.6 活动图的映射

图12-43 项目管理系统的活动图

313 第12章 UML模型的C++实现

12.10 小结 习题12

EmployeeDepartmentEloyeemployeeEmployeeDepartmentDepartmentDepartmentSchedScheduleScheduleule0..*0..*0..*0..*0..*0..*0..*1111111anagerManagerManagerManagerSectionSectionSectionSectionStudStudententStudent (a) (b) (c) (d) (a) (b) (c) (d) (a) (b) (c) (d) 图12-44 四个类图

class Vehicle

{

public:

Vehicle(int weight = 0); void SetWeight(int weight); virtual void ShowMe() = 0; protected:

int weight; };

class Car: virtual public Vehicle {

public:

Car(int weight=0,int aird=0); void ShowMe(); protected: int aird; };

class Boat: virtual public Vehicle {

uleGroundVehicleGroundVehicleGroundVehicleGroundVehicle-licenseNum-licenseNumber : Integer0..*-licenseNum-licenseNumber : Integerber : Integer+register()ber : Integer+register()+register()+register()1entCarCarCarCar-noOfPassenger : Integer-noOfPassenger : Integer-noOfPassenger : Integer-noOfPassenger : Integer+getpassengerCapacity()+getpassengerCapacity()+getpassengerCapacity()+getpassengerCapacity()

SchedEmpMStud (a) (b) (c) (d) 314 U ML与软件建模

public:

Boat(int weight=0,float tonnage=0); void ShowMe(); protected:

float tonnage; };

class AmphibianCar: public Car,public Boat {

public:

AmphibianCar(int weight,int aird,float tonnage); void ShowMe();

void ShowMembers(); };

Person+assign(d : Department)+setCompensation(s: Salary)1..10employee1employerCompany

图12-45 雇员和雇主的关系

class Shape : public Object { protected:

double area;

double perimeter; public:

double compArea() { area = 0.0; return 0.0; }

virtual String getName() = 0; }

class Rectangle:Shape{ private:

double length; double width; public:

Rectangle(double l, double w) { length = l; width = w; }

double compArea(){

315 第12章 UML模型的C++实现

area = length * width; return area; }

string getName() { return \ }

}

class Triangle : Shape{ private:

double length; double height; public:

Triangle(double l, double h){ length = l; width = w; }

double compArea() {

area = 0.5 * length * height; return area; }

string getName() {

return \ } }

class Student{ public:

Schedule* theSchedule;

Student(){

theSchedule = new Schedule[5]; } }

class Schedule{ public:

Student theStudent; Schedule(){} }

315 第12章 UML模型的C++实现

area = length * width; return area; }

string getName() { return \ }

}

class Triangle : Shape{ private:

double length; double height; public:

Triangle(double l, double h){ length = l; width = w; }

double compArea() {

area = 0.5 * length * height; return area; }

string getName() {

return \ } }

class Student{ public:

Schedule* theSchedule;

Student(){

theSchedule = new Schedule[5]; } }

class Schedule{ public:

Student theStudent; Schedule(){} }

本文来源:https://www.bwwdw.com/article/rfq.html

Top