欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > asp.net >内容正文

asp.net

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

发布时间:2025/6/17 asp.net 18 豆豆
生活随笔 收集整理的这篇文章主要介绍了 设计模式复习-抽象工厂模式 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

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

        有两种硬件,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;}

 

总结

以上是生活随笔为你收集整理的设计模式复习-抽象工厂模式的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。