设计模式复习-抽象工厂模式
生活随笔
收集整理的這篇文章主要介紹了
设计模式复习-抽象工厂模式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
設計模式復習-抽象工廠模式
? ? ? ? 有兩種硬件,PC和Phone,有兩種系統,Windows和Linux,現在假設PC和Phone上全都能安裝這兩個系統,并且將來硬件不會在變化,但是系統可能需要擴展,比如擴展一個Android系統,處理這樣的問題,盡量滿足OCP。
?
//FactoryMode.h
#pragma?once#include<iostream>using?namespace?std;class?CinterfaceOS?{public:virtual?void?StartOs()?=?0;virtual?void?CloseOs()?=?0;};class?CWindows?:?public?CinterfaceOS?{public:void?StartOs()?{cout?<<?"Start?Windows?OS"?<<?endl;}void?CloseOs()?{cout?<<?"Close?Windows?OS"?<<?endl;}};class?CLinux?:?public?CinterfaceOS?{public:void?StartOs()?{cout?<<?"Start?Linux?OS"?<<?endl;}void?CloseOs()?{cout?<<?"Close?Linux?OS"?<<?endl;}};class?CWindowsPC?:?public?CWindows?{public:void?StartOs()?{cout?<<?"StartWindowsPC:";CWindows::StartOs();}void?CloseOs()?{cout?<<?"CloseWindowsPC:";CWindows::CloseOs();}};class?CWindowsPhone?:?public?CWindows?{public:void?StartOs()?{cout?<<?"StartWindowsPhone:";CWindows::StartOs();}void?CloseOs()?{cout?<<?"CloseWindowsPhone:";CWindows::CloseOs();}};class?CLinuxPC?:?public?CLinux?{public:void?StartOs()?{cout?<<?"StartLinuxPC:";CLinux::StartOs();}void?CloseOs()?{cout?<<?"CloseLinuxPC:";CLinux::CloseOs();}};class?CLinuxPhone?:?public?CLinux?{public:void?StartOs()?{cout?<<?"StartLinuxPhone:";CLinux::StartOs();}void?CloseOs()?{cout?<<?"CloseLinuxPhone:";CLinux::CloseOs();}};class?CInterfaceFactory?{public:virtual?CinterfaceOS?*?CreatePcOs()?=?0;virtual?CinterfaceOS?*?CreatePhoneOs()?=?0;};class?CCreateFactoryWindows?:?public?CInterfaceFactory?{CinterfaceOS?*?CreatePcOs()?{return?new?CWindowsPC();}CinterfaceOS?*?CreatePhoneOs()?{return?new?CWindowsPhone();}};class?CCreateFactoryLinux?:?public?CInterfaceFactory?{CinterfaceOS?*?CreatePcOs()?{return?new?CLinuxPC();}CinterfaceOS?*?CreatePhoneOs()?{return?new?CLinuxPhone();}};#include?"stdafx.h"#include?"FactoryMode.h"#include?<iostream>using?namespace?std;int?main()?{CInterfaceFactory?*pHashFactoryWindows?=?NULL;CinterfaceOS?*pHashOsWindowsPC?=?NULL;CinterfaceOS?*pHashOsWindowsPhone?=?NULL;CInterfaceFactory?*pHashFactoryLinux?=?NULL;CinterfaceOS?*pHashOsLinuxPC?=?NULL;CinterfaceOS?*pHashOsLinuxPhone?=?NULL;pHashFactoryWindows?=?new?CCreateFactoryWindows();pHashOsWindowsPC?=?pHashFactoryWindows->CreatePcOs();pHashOsWindowsPhone?=?pHashFactoryWindows->CreatePhoneOs();pHashFactoryLinux?=?new?CCreateFactoryLinux();pHashOsLinuxPC?=?pHashFactoryLinux->CreatePcOs();pHashOsLinuxPhone?=?pHashFactoryLinux->CreatePhoneOs();pHashOsWindowsPC->StartOs();pHashOsWindowsPC->CloseOs();pHashOsWindowsPhone->StartOs();pHashOsWindowsPhone->CloseOs();pHashOsLinuxPC->StartOs();pHashOsLinuxPC->CloseOs();pHashOsLinuxPhone->StartOs();pHashOsLinuxPhone->CloseOs();delete?pHashOsWindowsPC;delete?pHashOsWindowsPhone;delete?pHashFactoryWindows;delete?pHashOsLinuxPC;delete?pHashOsLinuxPhone;delete?pHashFactoryLinux;//getchar();return?0;}?
總結
以上是生活随笔為你收集整理的设计模式复习-抽象工厂模式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 设计模式复习-工厂方法模式
- 下一篇: 神经网络与机器学习 笔记—LMS(最小均