首页 > 代码库 > 外观模式之C++实现
外观模式之C++实现
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
class Hand
{
public:
void Get()
{
cout << "取东西" << "\t";
}
};
class Leg
{
public:
void Run()
{
cout << "奔跑" << "\t";
}
};
class Eyes
{
public:
void See()
{
cout << "看" << "\t";
}
};
class Mouth
{
public:
void Eat()
{
cout << "吃东西" << "\t";
}
};
/*
人体是由各种器官组成,人如果要完成各种动作,则需要各个器官配合。
正好契合了外观模式
*/
class Person
{
private:
Hand *hand;
Eyes *eyes;
Mouth *mouth;
Leg *leg;
public:
Person()
{
eyes = new Eyes;
hand = new Hand;
mouth = new Mouth;
leg = new Leg;
}
void Eat()
{
cout << "开始进食" << endl;
hand->Get();
mouth->Eat();
cout << endl;
}
void Run()
{
cout << "开始跑步" << endl;
eyes->See();
leg->Run();
cout << endl;
}
};
int main()
{
Person *person = new Person;
person->Eat();
person->Run();
return 0;
}
#include <string>
#include <iostream>
using namespace std;
class Hand
{
public:
void Get()
{
cout << "取东西" << "\t";
}
};
class Leg
{
public:
void Run()
{
cout << "奔跑" << "\t";
}
};
class Eyes
{
public:
void See()
{
cout << "看" << "\t";
}
};
class Mouth
{
public:
void Eat()
{
cout << "吃东西" << "\t";
}
};
/*
人体是由各种器官组成,人如果要完成各种动作,则需要各个器官配合。
正好契合了外观模式
*/
class Person
{
private:
Hand *hand;
Eyes *eyes;
Mouth *mouth;
Leg *leg;
public:
Person()
{
eyes = new Eyes;
hand = new Hand;
mouth = new Mouth;
leg = new Leg;
}
void Eat()
{
cout << "开始进食" << endl;
hand->Get();
mouth->Eat();
cout << endl;
}
void Run()
{
cout << "开始跑步" << endl;
eyes->See();
leg->Run();
cout << endl;
}
};
int main()
{
Person *person = new Person;
person->Eat();
person->Run();
return 0;
}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。