标签:
好久没写设计模式的blog了,这次重新回来填坑,先找一个最简单但是却最常用的设计模式来学习,外观模式。其实说是一个设计模式,其实我们在实际的编程中无时无刻不在用外观模式,可以说这个设计模式已经渗透到编程的各个方便,可能我们自己没感觉出来罢了。
// Design Pattern.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
//灰机公司
class AirlineCompany
{
private:
//是否有票
bool data[4];
public:
AirlineCompany()
{
data[0] = false;
data[1] = false;
data[2] = true;
data[3] = true;
}
void HasTicket(int day)
{
if (data[day])
cout << day << " 日有灰机票!" << endl;
else
cout << day << " 日木有灰机票了!" << endl;
}
void BookTicket(int day)
{
cout << "你订了灰机票" << endl;
}
void PayForTicket()
{
cout << "你付了机票钱" << endl;
}
};
//酒店类
class Hotel
{
private:
//是否有空房间
bool data[4];
public:
Hotel()
{
data[0] = true;
data[1] = false;
data[2] = false;
data[3] = true;
}
void HasTicket(int day)
{
if (data[day])
cout << day << " 日有空房间!" << endl;
else
cout << day << " 日木有空房间了!" << endl;
}
void BookTicket(int day)
{
cout << "你预定了房间" << endl;
}
void PayForTicket()
{
cout << "你交了酒店订金" << endl;
}
};
//景点类
class TouristSights
{
private:
//ticket
int data[4];
public:
TouristSights()
{
data[0] = false;
data[1] = true;
data[2] = false;
data[3] = true;
}
void HasTicket(int day)
{
if (data[day])
cout << day << " 日可以参观!" << endl;
else
cout << day << " 日我们要闭馆!" << endl;
}
void BookTicket(int day)
{
cout << "你预定了门票" << endl;
}
void PayForTicket()
{
cout << "你付了门票钱" << endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
//航空公司
AirlineCompany* airlineCompany = new AirlineCompany();
//酒店
Hotel* hotel = new Hotel();
//景点
TouristSights* touristSight = new TouristSights();
//恩,我先查看一下0日有木有各种票
airlineCompany->HasTicket(0);
hotel->HasTicket(0);
touristSight->HasTicket(0);
//....此处省略若干行,我终于找到3号各个地方都有票
airlineCompany->HasTicket(3);
hotel->HasTicket(3);
touristSight->HasTicket(3);
//我要开始订票了
airlineCompany->BookTicket(3);
hotel->BookTicket(3);
touristSight->BookTicket(3);
//我要付款了
airlineCompany->PayForTicket();
hotel->PayForTicket();
touristSight->PayForTicket();
//终于完事了,累死我了...
cout << endl << "心好累,还是宅着好" << endl;
system("pause");
return 0;
}
结果如下:// Design Pattern.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
//灰机公司
class AirlineCompany
{
private:
//是否有票
bool data[4];
public:
AirlineCompany()
{
data[0] = false;
data[1] = false;
data[2] = true;
data[3] = true;
}
void HasTicket(int day)
{
}
void BookTicket(int day)
{
}
void PayForTicket()
{
}
};
//酒店类
class Hotel
{
private:
//是否有空房间
bool data[4];
public:
Hotel()
{
data[0] = true;
data[1] = false;
data[2] = false;
data[3] = true;
}
void HasTicket(int day)
{
}
void BookTicket(int day)
{
}
void PayForTicket()
{
}
};
//景点类
class TouristSights
{
private:
//ticket
int data[4];
public:
TouristSights()
{
data[0] = false;
data[1] = true;
data[2] = false;
data[3] = true;
}
void HasTicket(int day)
{
}
void BookTicket(int day)
{
}
void PayForTicket()
{
}
};
//旅游公司
class TravelCompany
{
private:
AirlineCompany* m_AirlineCompany;
Hotel* m_Hotel;
TouristSights* m_TouristSights;
public:
TravelCompany()
{
m_AirlineCompany = new AirlineCompany();
m_Hotel = new Hotel();
m_TouristSights = new TouristSights();
}
void CheckSuitableDay()
{
//恩,旅游公司帮你查,怎么累都跟我们木有半毛钱关系
m_AirlineCompany->HasTicket(0);
m_Hotel->HasTicket(0);
m_TouristSights->HasTicket(0);
//....此处省略若干行,
m_AirlineCompany->HasTicket(3);
m_Hotel->HasTicket(3);
m_TouristSights->HasTicket(3);
cout << "找到了合适的行程" << endl;
}
void BookTravelPackage()
{
//帮你搞定一切
m_AirlineCompany->BookTicket(3);
m_Hotel->BookTicket(3);
m_TouristSights->BookTicket(3);
cout << "您已经预定好了行程" << endl;
}
void PayForPackage()
{
//一站式服务,包您满意
m_AirlineCompany->PayForTicket();
m_Hotel->PayForTicket();
m_TouristSights->PayForTicket();
cout << "付款完成!" << endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
TravelCompany* travelCompany = new TravelCompany();
travelCompany->CheckSuitableDay();
travelCompany->BookTravelPackage();
travelCompany->PayForPackage();
cout << endl << "轻松加愉快,外观模式万岁!" << endl;
system("pause");
return 0;
}
结果:标签:
原文地址:http://blog.csdn.net/puppet_master/article/details/50984939