Last active
August 24, 2021 15:43
-
-
Save 18520339/308c5a8c1b0ad0ebadee63f751c92fbb to your computer and use it in GitHub Desktop.
Design Patterns - Composite
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <vector> | |
using namespace std; | |
class Item { | |
protected: | |
string ten; | |
public: | |
Item(string ten): ten(ten) {} | |
virtual ~Item() {} | |
virtual string getKieu() = 0; | |
virtual int getDungLuong() = 0; | |
}; | |
class File: public Item { | |
private: | |
int dungLuong; | |
public: | |
File(string ten, int dungLuong): Item(ten), dungLuong(dungLuong) {} | |
string getKieu() override { return "File"; } | |
int getDungLuong() override { return dungLuong; } | |
}; | |
class Folder: public Item { | |
private: | |
vector<Item *> dsCon; | |
public: | |
Folder(string ten): Item(ten) {} | |
string getKieu() override { return "Folder"; } | |
void themCon(Item *con) { dsCon.push_back(con); } | |
int demFile() { | |
int dem = 0; | |
for (Item *con: dsCon) { | |
if (con->getKieu() == "File") dem += 1; | |
else dem += ((Folder *)con)->demFile(); | |
} | |
return dem; | |
} | |
int getDungLuong() override { | |
int tong = 0; | |
for (Item *con: dsCon) | |
tong += con->getDungLuong(); | |
return tong; | |
} | |
}; | |
int main() { | |
Folder *lapTrinh = new Folder("lap trinh"); | |
File *taiLieu = new File("tai lieu.pdf", 2000); | |
Folder *baiTap = new Folder("bai tap"); | |
File *bt01 = new File("bt01.cpp", 500); | |
File *bt02 = new File("bt02.cpp", 600); | |
lapTrinh->themCon(taiLieu); | |
lapTrinh->themCon(baiTap); | |
baiTap->themCon(bt01); | |
baiTap->themCon(bt02); | |
cout << lapTrinh->getDungLuong() << endl; | |
cout << lapTrinh->demFile() << endl; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <vector> | |
using namespace std; | |
class DienTro { | |
public: | |
virtual ~DienTro() {} | |
virtual double getR() = 0; | |
}; | |
class DienTroDon: public DienTro { | |
private: | |
double r; | |
public: | |
DienTroDon(double r): r(r) {} | |
double getR() override { return r; } | |
}; | |
class DienTroNoiTiep: public DienTro { | |
private: | |
vector<DienTro *> dsCon; | |
public: | |
void themCon(DienTro *con) { dsCon.push_back(con); } | |
double getR() override { | |
double tong = 0; | |
for (DienTro *con: dsCon) | |
tong += con->getR(); | |
return tong; | |
} | |
}; | |
class DienTroSongSong: public DienTro { | |
private: | |
vector<DienTro *> dsCon; | |
public: | |
void themCon(DienTro *con) { dsCon.push_back(con); } | |
double getR() override { | |
double tong = 0; | |
for (DienTro *con: dsCon) | |
tong += 1 / con->getR(); | |
return 1 / tong; | |
} | |
}; | |
int main() { | |
DienTroDon *r1 = new DienTroDon(1); | |
DienTroDon *r2 = new DienTroDon(2); | |
DienTroDon *r3 = new DienTroDon(6); | |
DienTroDon *r4 = new DienTroDon(4); | |
DienTroSongSong *r23 = new DienTroSongSong(); | |
r23->themCon(r2); | |
r23->themCon(r3); | |
DienTroNoiTiep *rAB = new DienTroNoiTiep(); | |
rAB->themCon(r1); | |
rAB->themCon(r23); | |
rAB->themCon(r4); | |
cout << rAB->getR() << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment