@smilence
2013-10-25T07:42:13.000000Z
字数 3724
阅读 7249
1.Singleton Design Pattern
The singleton pattern ensures that a class has only one instance, which is accessible throughout the application.It can be useful where you have a "global" object with exactly one instance.
//Example for singleton pattern
//class definition
class MySingleton
{
private:
// Private Constructor
MySingleton();
// Stop the compiler generating methods of copy the object
MySingleton(const MySingleton & copy); // Not Implemented
MySingleton& operator=(const MySingleton & copy); // Not Implemented
static MySingleton* m_pInstance;
public:
static MySingleton* getInstance()
{
if(!m_pInstance)
m_pInstance = new MySingleton;
return m_pInstance;
}
};
//in the source file
MySingleton *MySingleton::m_pIntance = NULL;
Reference: http://www.yolinux.com/TUTORIALS/C++Singleton.html
2.Factory Design Pattern
The factory method offers an interface for creating an instance of class,with its subclasses deciding which class to instantiate.Creator class will be abstract or provides an implementation for factory method, which takes a type parameter to instantiate.
//with abstract factory method;
class Product{
};
class ConcreteProduct: public Product{
};
class Creator{
public:
Creator(){
Product *p = factoryMethod();
}
protected:
virtual Product* factoryMethod() = 0;
};
class ConcreteCreator: public Creator{
protected:
Product* factoryMethod(){
return new ConcreteProduct();
}
};
//with concrete factory method;
class Stooge
{
public:
// Factory Method
static Stooge *make_stooge(int choice){
if (choice == 1)
return new Larry;
else if (choice == 2)
return new Moe;
else
return new Curly;
}
virtual void slap_stick() = 0;
};
class Larry: public Stooge{};
class Moe: public Stooge{};
class Curly: public Stooge{};
处理OOD问题的方法论
Step 1 先不考虑需要的功能
Step 1.1 分析静态、基本的实体( entities ), 并考虑他们之间的"has"关系,如"A party has some guests."; 如果其中有全局的实体,则可以考虑使用Singleton Pattern。
Step 1.2 考虑衍生的实体类型,考虑他们与基本实体之间的"is"关系(Inheritance ),如"A dog is an animal.", 分析基本属性( base class variables )与衍生属性( sub class variables )。
Step 1.3 如果需要检索,在基本属性之间则可能需要建立mapping,如map<int,int> userid_to_machine
,可以使用std::map
或std::unordered_map
。
Step 2 根据问题所需要的功能,分析行为的主体与路径(谁调用谁去做某事,最终是谁进行操作,注意这里的"谁"都应该用指针来表示)。如果路径所经过的主体,存在subclass,则应该用Virtual Function来实现当前主体及其不同subclass的不同行为,实现Polymorphism。
如果路径经过的主体,需要创建不同类型的物件,则可以考虑使用Factory Pattern,即在该主体中定义一个virtual factoryMethod()
, 用于根据需要,创建不同类型的物件;或者为这些物件定义一个基本类Base
,并定义一个成员函数static Base *make_base( int typeChoice );
, 方便其他主体创建该类型物件。
e.g.1 Design a parking lot using object-oriented principles. ( CtCI 8.4 )
class ParkingLot{
private:
level* lvl;
int max_level;
static ParkingLot* pInstance;
public:
static ParkingLot* getInstance();
pair<int,int> parkVehicle( Vehicle* v){
//iterate all levels, invoke parkFreeSpot(v);
//return the level and spot starting position
int index;
for( int j = 0 ; j < max_level ; j++){
index = lvl[j].parkFreeSpot(v);
if( index != -1){
return pair<int,int>(j,index) ;
}
}
return -1;
}
};
class level{
private:
Spot* spots;
int max_num;
int remain;
int freeIndex;
public:
int parkFreeSpot( Vehicle* v){
int n = v->numSpots();
int index = findFreeSpots( n);//find continuous n spots,return the index;
if(index = -1) return -1;
else
v->parkVehicle( spots+index); //park the vehicle at the spot(s);
return index;
}
int findFreeSpots( int n){
//find continuous n spots,return the index;
//if not found, return -1;
}
};
class Spot{
private:
int length;
int width;
bool state;
int index;
int lvl;
public:
bool isFree();
};
class Vehicle{
private:
int length;
int width;
bool parked;
public:
// no need for virtual functions here because subclasses have different attributes but same "behavior"
int getlength();
int getWidth();
int numSpots(Spot* s); //return how many such spots this vehicle need
void parkVehicle( Spot* s);//park starting from spot S;
void removeVehicle( Spot* s); //remove the vehicle away;
};
//every type of vehicle has default value of length and width;
class motor:public Vehicle{};
class car:public Vehicle{};
class bus::public Vehicle{};
e.g.2 Design an in-memory file system. (CtCI 8.10)
e.g.3 Design an elavator bank.
http://stackoverflow.com/questions/493276/modelling-an-elevator-using-object-oriented-analysis-and-design/12457431#12457431