日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

设计模式复习-抽象工厂模式

發布時間:2025/6/17 57 豆豆
生活随笔 收集整理的這篇文章主要介紹了 设计模式复习-抽象工厂模式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

設計模式復習-抽象工廠模式

? ? ? ? 有兩種硬件,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;}

?

總結

以上是生活随笔為你收集整理的设计模式复习-抽象工厂模式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。