[关闭]
@smilence 2013-10-24T23:42:13.000000Z 字数 3724 阅读 7077

Chapter 6 Object Oriented Design


"The Rules"


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.

  1. //Example for singleton pattern
  2. //class definition
  3. class MySingleton
  4. {
  5. private:
  6. // Private Constructor
  7. MySingleton();
  8. // Stop the compiler generating methods of copy the object
  9. MySingleton(const MySingleton & copy); // Not Implemented
  10. MySingleton& operator=(const MySingleton & copy); // Not Implemented
  11. static MySingleton* m_pInstance;
  12. public:
  13. static MySingleton* getInstance()
  14. {
  15. if(!m_pInstance)
  16. m_pInstance = new MySingleton;
  17. return m_pInstance;
  18. }
  19. };
  20. //in the source file
  21. 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.

  1. //with abstract factory method;
  2. class Product{
  3. };
  4. class ConcreteProduct: public Product{
  5. };
  6. class Creator{
  7. public:
  8. Creator(){
  9. Product *p = factoryMethod();
  10. }
  11. protected:
  12. virtual Product* factoryMethod() = 0;
  13. };
  14. class ConcreteCreator: public Creator{
  15. protected:
  16. Product* factoryMethod(){
  17. return new ConcreteProduct();
  18. }
  19. };
  1. //with concrete factory method;
  2. class Stooge
  3. {
  4. public:
  5. // Factory Method
  6. static Stooge *make_stooge(int choice){
  7. if (choice == 1)
  8. return new Larry;
  9. else if (choice == 2)
  10. return new Moe;
  11. else
  12. return new Curly;
  13. }
  14. virtual void slap_stick() = 0;
  15. };
  16. class Larry: public Stooge{};
  17. class Moe: public Stooge{};
  18. 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::mapstd::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 )

  1. class ParkingLot{
  2. private:
  3. level* lvl;
  4. int max_level;
  5. static ParkingLot* pInstance;
  6. public:
  7. static ParkingLot* getInstance();
  8. pair<int,int> parkVehicle( Vehicle* v){
  9. //iterate all levels, invoke parkFreeSpot(v);
  10. //return the level and spot starting position
  11. int index;
  12. for( int j = 0 ; j < max_level ; j++){
  13. index = lvl[j].parkFreeSpot(v);
  14. if( index != -1){
  15. return pair<int,int>(j,index) ;
  16. }
  17. }
  18. return -1;
  19. }
  20. };
  21. class level{
  22. private:
  23. Spot* spots;
  24. int max_num;
  25. int remain;
  26. int freeIndex;
  27. public:
  28. int parkFreeSpot( Vehicle* v){
  29. int n = v->numSpots();
  30. int index = findFreeSpots( n);//find continuous n spots,return the index;
  31. if(index = -1) return -1;
  32. else
  33. v->parkVehicle( spots+index); //park the vehicle at the spot(s);
  34. return index;
  35. }
  36. int findFreeSpots( int n){
  37. //find continuous n spots,return the index;
  38. //if not found, return -1;
  39. }
  40. };
  41. class Spot{
  42. private:
  43. int length;
  44. int width;
  45. bool state;
  46. int index;
  47. int lvl;
  48. public:
  49. bool isFree();
  50. };
  51. class Vehicle{
  52. private:
  53. int length;
  54. int width;
  55. bool parked;
  56. public:
  57. // no need for virtual functions here because subclasses have different attributes but same "behavior"
  58. int getlength();
  59. int getWidth();
  60. int numSpots(Spot* s); //return how many such spots this vehicle need
  61. void parkVehicle( Spot* s);//park starting from spot S;
  62. void removeVehicle( Spot* s); //remove the vehicle away;
  63. };
  64. //every type of vehicle has default value of length and width;
  65. class motor:public Vehicle{};
  66. class car:public Vehicle{};
  67. 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

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注