内部类创建一个内部版本
C++常要考虑这样一样问题:指针的初始化,对象何时创建,何时释放。这无疑给程序猿加重了负担,程序猿不仅要考虑业务逻辑,还要时时刻刻刻指针在何时创建和释放。
ShipboardAircraft
说明:AircraftCarrier和ShipboardAircraft两个类的构造函数和析构函数都定义成protected。且都定义AVSG为友元类,这样是从客观上保证了AircraftCarrier和ShipboardAircraft的对象不能在外面被创建和释放。仅仅能由AVSG内部来创建和释放,而AircraftCarrier和ShipboardAircraft的调用者不用考虑对象何时创建和释放。
那有没有什么办法能够解决问题呢?答案是肯定的。以下就通过一个样例来解说这样的模式。
如果如今要仿真模拟一艘航空母舰。
广义的航空母舰是指“航空母舰战斗群”,如果一支航空母舰战斗群(AVSG)由1艘航母(AircraftCarrier)。1艘补给舰 (DepotShip),2艘防空导弹驱赶舰(Destroyer)。4艘多功能护卫舰(Frigate),2艘093核潜艇(NuclearSubmarine)组成,若干舰载机(ShipboardAircraft)和预警机组成。
如今用简单的类来模拟这些战机。
如果有这样一个航空母舰类和一个舰载机类例如以下:
AircraftCarrier
/* 航空母舰 * File: AircraftCarrier.h * Author: luowf * * Created on July 31, 2014, 8:03 PM */ #ifndef AIRCRAFTCARRIER_H #define AIRCRAFTCARRIER_H #include "stdafx.h" class AircraftCarrier { protected: AircraftCarrier(); AircraftCarrier(const AircraftCarrier& orig); virtual ~AircraftCarrier(); public: void SetName(const string& name) { m_name = name; } string GetName() { return m_name; } private: string m_name; friend class CVSG; }; #endif /* AIRCRAFTCARRIER_H */
ShipboardAircraft
/* 舰载机 * File: ShipboardAircraft.h * Author: luowf * * Created on August 1, 2014, 9:26 AM */ #ifndef SHIPBOARDAIRCRAFT_H #define SHIPBOARDAIRCRAFT_H #include "stdafx.h" class ShipboardAircraft { protected: ShipboardAircraft(); ShipboardAircraft(const ShipboardAircraft& orig); virtual ~ShipboardAircraft(); public: void SetName(const string& name) { m_name = name; } string GetName() { return m_name; } private: string m_name; friend class CVSG; }; #endif /* SHIPBOARDAIRCRAFT_H */
CVSG.h
/* 航空母舰战斗群 * * File: CVSG.h * Author: luoweifu * * Created on July 31, 2014, 7:49 PM */ #ifndef CVSG_H #define CVSG_H #include "stdafx.h" #include <string> using namespace std; class AircraftCarrier; class ShipboardAircraft; class CVSG { protected: CVSG(); CVSG(const CVSG& orig); virtual ~CVSG(); public: //创建航母 AircraftCarrier* CreateAircraftCarrier(); //加入舰载机 ShipboardAircraft* AddShipboardAircraft(); void SetName(const string& name); string GetName(); private: typedef vector<ShipboardAircraft*> ShipAircVec; ShipAircVec m_vecShipboardAircraft; AircraftCarrier* m_pAircraftCarrier; string m_name; }; #endif /* CVSG_H */
CVSG.cpp
/* 航空母舰战斗群 * * File: CVSG.cpp * Author: luoweifu * * Created on July 31, 2014, 7:49 PM */ //================================================== #include "CVSG.h" #include "AircraftCarrier.h" #include "ShipboardAircraft.h" //================================================== CVSG::CVSG() : m_pAircraftCarrier(NULL), m_name("") { m_vecShipboardAircraft.empty(); } CVSG::CVSG(const CVSG& orig) { } CVSG::~CVSG() { if(m_pAircraftCarrier != NULL) { delete m_pAircraftCarrier; m_pAircraftCarrier = NULL; } for(ShipAircVec::iterator itr = m_vecShipboardAircraft.begin(); itr != m_vecShipboardAircraft.end(); itr ++) { if(*itr != NULL) { delete *itr; *itr = NULL; } } m_vecShipboardAircraft.clear(); } AircraftCarrier* CVSG::CreateAircraftCarrier() { if(m_pAircraftCarrier == NULL) { m_pAircraftCarrier = new AircraftCarrier(); } return m_pAircraftCarrier; } ShipboardAircraft* CVSG::AddShipboardAircraft() { ShipboardAircraft* pShipboardAircraft = new ShipboardAircraft(); if(pShipboardAircraft != NULL) { m_vecShipboardAircraft.push_back(pShipboardAircraft); } return pShipboardAircraft; } void CVSG::SetName(const string& name) { m_name = name; } string CVSG::GetName() { return m_name; }
说明:AircraftCarrier和ShipboardAircraft两个类的构造函数和析构函数都定义成protected。且都定义AVSG为友元类,这样是从客观上保证了AircraftCarrier和ShipboardAircraft的对象不能在外面被创建和释放。仅仅能由AVSG内部来创建和释放,而AircraftCarrier和ShipboardAircraft的调用者不用考虑对象何时创建和释放。
版权声明:本文博客原创文章。博主可能不无任何商业用途的同意才能使用。转载请注明出处。