组合模式(Composite)
  TEZNKK3IfmPf 2024年03月30日 59 0

将所有对象组织在一个树状结构之下,用来实现对象间部分—整体之间的关系,使得用户对单个对象和组合对象的使用具有一致性。

#include <list>
#include <iostream>
#include <string>
using namespace std;
class Component{
protected:
string name;
public:
virtual void Add(Component *file)=0;
virtual void Remove(Component *file)=0;
virtual list <Component *>* Get()=0;
};
class Leaf:public Component{
public:
Leaf(string name)
{
this->name=name;
}
void Add(Component *file){
cout<<"Cannot add to a leaf"<<endl;
}
void Remove(Component *file)
{
cout<<"Cannot remove from a leaf"<<endl;
}
list <Component *> * Get()
{
return NULL;
}
};
class Composite:public Component{
private:
list <Component *> p;
public:
Composite(string name){
this->name=name;
}
void Add(Component *file){
p.push_back(file);
}
void Remove(Component *file){
p.remove(file);
}
list <Component *> * Get()
{
this->name;
}

};
class manu:public Compsite{
//类似于dos命令下cd之类的进出命令使用
};
int main(void)
{
//略
return 0;
}
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2024年03月30日 0

暂无评论

TEZNKK3IfmPf