?1. 概述
在面向物件的程式設計中,類共有六種關系,它們分別是Composition、Aggregation、Association、Dependency、Generalization和Realization,理解類的六大關系對于面向物件的程式設計非常重要,也是理解設計模式的前提,本文給出概念介紹并結合C++代碼給出解釋,由于作者本身能力有限,難免有不當甚至錯誤,歡迎指出,文中給出的實體的完整工程詳見參考文獻1,
2. 組合(Composition)
Composition是一種 "part-of" 的關系,如下圖中的孕婦和嬰兒,它是一種強所屬關系,整體與部分往往具有相同的生命周期,整體的物件是在部分物件創建的同時或創建之后創建,在部分物件銷毀的同時或之前銷毀,

參考代碼如下,講的是游戲中每個生物體(Creature)都有自己的坐標位置,其中坐標位置用Point類表示,明顯是一種"part-of"的關系,
#include <string>
struct Point
{
Point() : x(0), y(0) { }
void setPoint(const int x, const int y)
{
this->x = x;
this->y = y;
}
int getPointX() const
{
return this->x;
}
int getPointY() const
{
return this->y;
}
private:
int x;
int y;
};
struct Creature
{
Creature() : name(""), location(location) { }
void setCreature(const std::string& name, const Point& location)
{
this->name = name;
this->location = location;
}
void move(int x, int y)
{
this->location.setPoint(x, y);
}
std::string getName() const
{
return this->name;
}
int getXLocation() const
{
return this->location.getPointX();
}
int getYLocation() const
{
return this->location.getPointY();
}
private:
std::string name;
Point location;
};
3. 委托(Aggregation)
Aggregation是一種"has-a"的關系,如圖中的Computer和CPU,相對于Composition,Aggregation是一種弱從屬的關系,整體與部分之間是可分離的,它們可以具有各自的生命周期,部分可以屬于多個整體物件,也可以為多個整體物件共享,

參考代碼如下,講的是房子(House)里有一個人(Person),House和Person是可以單獨存在的,只不過在目前這種狀態下Person在House中,它是一種"has-a"關系(當然此處Person可以是復數,可以采用指標也可以采用參考),
#include <string>
struct Person
{
Person(const std::string& name) : name(name) { }
const std::string& getName() const
{
return this->name;
}
private:
std::string name;
};
struct House
{
House(const Person& person) : person(person) { }
const std::string getPerson() const
{
return this->person.getName();
}
private:
const Person& person;
};
4. 關聯(Association)
Association是一種"use-a"的關系,可以分為雙向關聯和單向關聯,它體現的是兩個類、或者類與介面之間語意級別的一種強依賴關系,它使一個類知道另一個類的屬性和方法,比如我和我的朋友,老師和學生等都屬于關聯關系,

參考代碼如下,講的是醫生(Doctor)和病人(Patient)的關系,Doctor給Patient治病,Patient通過Doctor看病,它是一種"use-a"的關系,
#include <functional>
#include <string>
#include <vector>
struct Patient;
struct Doctor
{
Doctor(const std::string& name) : name(name) { }
void addPatient(Patient& patient)
{
this->patient.push_back(patient);
patient.addDoctor(*this);
}
const std::string& getName() const
{
return this->name;
}
std::vector<std::reference_wrapper<const Patient>> patient;
private:
std::string name;
};
struct Patient
{
Patient(const std::string& name) : name(name) { }
const std::string& getName() const
{
return this->name;
}
friend void addPatient(Patient& patient);
void addDoctor(const Doctor& doctor)
{
this->doctor.push_back(doctor);
}
std::vector<std::reference_wrapper<const Doctor>> doctor;
private:
std::string name;
};
代碼中的std::reference_wrapper詳見參考文獻2,此處不能使用std::vector<const Doctor&>,因為參考只能在建構式中被初始化且不能被重新賦值,一個可行的方案是采用std::vector<const Doctor*>,但是要注意nullptr和記憶體的正確釋放,這真的讓人頭痛,std::reference_wrapper提供給我們一個新思路,其實它和參考特別像,但是它允許被重新賦值和拷貝,
5. 依賴(Dependency)
Dependency是一種"depends-on"的關系,它比Association的關系若,它強調的是一種單向的關系,一個類的實作需要另一個類的協助,如某人要過河,人此時就會"depends-on"船,

參考代碼如下,講的是Point2D物件的列印輸出,它"depends-on" std::ostream,
#include <iostream>
struct Point2D
{
Point2D(int x, int y) : x(x), y(y) { }
int getX() const
{
return this->x;
}
int getY() const
{
return this->y;
}
friend std::ostream& operator<<(std::ostream& os, const Point2D& point)
{
os << "Point(" << point.x << ", " << point.y <<")";
return os;
}
private:
int x;
int y;
};
6. 繼承(Generalization)
對于public的Generalization來說,它是一種"is-a"的關系;對于private的Generalization來說,個人理解只是實作上避免重復代碼的一種手法而已,它是一種"is-implemented-in-terms-of"的關系(此處在參考文獻3 Effective C++的39條《審慎的使用private繼承》有詳細介紹);對于protected的繼承來說,個人沒有理解,可能僅僅為了統一形式吧,

參考代碼如下,講的是學生(Student)是一個人(Person),明顯是一種"is-a"的關系,
#include <string>
struct Person
{
Person(const std::string& name) : name(name) { }
const std::string& getName() const
{
return this->name;
}
private:
std::string name;
};
struct Student : Person
{
Student(const std::string& name, const int number)
: Person(name), number(number) { }
const int getNumber() const
{
return this->number;
}
private:
int number;
};
7. 實作(Realization)
Realization是一種面向物件中介面和類之間的"realization"關系,在這種關系中,類實作了介面,類中的操作實作了介面中所宣告的操作,在C++中,介面通過的純虛函式來實作,C++的多型就是通過虛函式來實作的,

參考代碼如下,講的是Dog和Bird都是Animal,但是它們的move()操作各不相同,通過在基類(Animal)中宣告介面和在Dog、Bird類中去"realization"介面,
#include <string>
struct Animal
{
virtual ~Animal() {}
virtual std::string move() const = 0;
};
struct Dog : Animal
{
std::string move() const override
{
return "run";
}
};
struct Bird : Animal
{
std::string move() const override
{
return "fly";
}
};
8. 總結
本文分享了類的六大關系,分析了各自的特點,并給出實體方便理解,本文所要強調的是要立足需求,根據抽象后的具體關系選用合適的"Relationship"去實作,而不是上來就是public繼承("is-a"的關系),當然現實中不可能只是簡單的六種關系中的某一種,更多的是多個關系的組合,相信對類的關系有了正確的認識后,再去理解設計模式就比較得心應手了,
9. 參考文獻
1.https://github.com/mzh19940817/ClassRelationship
2.C++11中std::reference_wrapper的理解,https://blog.csdn.net/guoxiaojie_415/article/details/80031948
3.Scott Meyers, Effective C++
本文結合實體分享了類的關系,文中可能有些許不當及錯誤之處,代碼也沒有用做到盡善盡美,歡迎大家批評指正,同時也歡迎大家評論、轉載(請注明源出處),謝謝!
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/308436.html
標籤:面向對象
上一篇:Java中集合和陣列的區別
