DesignPattern/12.FlyweightPattern/2.Code/main.cpp

46 lines
1.1 KiB
C++
Raw Normal View History

2019-10-27 07:58:03 +00:00
#include <iostream>
#include "FlyweightPattern.h"
int main()
{
NetDeviceFactory *factory = NetDeviceFactory::getFactory();
NetDevice *device1, *device2, *device3, *device4;
2021-10-28 15:15:51 +00:00
// 客户端1获取一个hub
2019-10-27 07:58:03 +00:00
device1 = factory->getNetDevice('H');
device1->print(1);
2021-10-28 15:15:51 +00:00
// 客户端2获取一个hub
2019-10-27 07:58:03 +00:00
device2 = factory->getNetDevice('H');
device2->print(2);
2021-10-28 15:15:51 +00:00
// 判断两个hub是否是同一个
printf("判断两个hub是否是同一个:\n");
2019-10-27 07:58:03 +00:00
printf("device1:%p\ndevice2:%p\n", device1, device2);
printf("\n\n\n\n");
2021-10-28 15:15:51 +00:00
// 客户端3获取一个switch
2019-10-27 07:58:03 +00:00
device3 = factory->getNetDevice('S');
device3->print(1);
2021-10-28 15:15:51 +00:00
// 客户端4获取一个switch
2019-10-27 07:58:03 +00:00
device4 = factory->getNetDevice('S');
device4->print(2);
2021-10-28 15:15:51 +00:00
// 判断两个switch是否是同一个
printf("判断两个switch是否是同一个:\n");
2019-10-27 07:58:03 +00:00
printf("device3:%p\ndevice4:%p\n", device3, device4);
printf("\n\n");
2020-11-29 05:40:29 +00:00
delete factory;
delete device1;
delete device2;
delete device3;
delete device4;
2022-03-09 15:09:06 +00:00
factory = nullptr;
device1 = nullptr;
device2 = nullptr;
device3 = nullptr;
device4 = nullptr;
2020-11-29 05:40:29 +00:00
2019-10-27 07:58:03 +00:00
system("pause");
return 0;
}