GeekBand 随笔分享(三)

//..................................................................................  专题一  ...........................................................................................................

一、composition:

     首先,我一句简单的话去理解:

     1》 composition:是将一个对象(部分)放到另一个对象里(组合)。它是一种 has-a 的关系。组合使软件在开发过程中可以聚合已存在的组件而不用去新建一个。

     2》 多层composition可以嵌套,生命周期同单层组合一样,整个对象周期。

     代码例:                                                                           

 class A{
    ...
    protected:
    B a; //A中有B   
  }
class B{
C c;
}
class C{
...
}

   3》composition的构造与析构过程:

    构造过程:  先构造类内部其他类的构造函数,再构造自已的构造函数。这点在实际编程中也很有帮助,特别是大型项目里头。

A::B(...): B(){...};

 

    析构过程:先调用类里其他类的析构函数,再调用自身的析构函数。

 A::~A(...) {...~A()};

  

   4》生命周期: 随对象的存在而存在,消失而消失。

二、Delegation:

    1》 Delegation:利用对象指针方法,实时调用其他类的各种方法。

    2》 基本用法:

          在一个类中,使用指针,调用其他类的方法。

     3》生命周期:

         随指针调用创建而存在,消失而消失。

     4》扩展:

    多目标Delegation,起始就是一个容器,在这个容器里可以存放多个对象,当调用委托的时候一次调用每个对象。容器里的对象应该都是相同的类型,这样才能够放到强             类型的容器中;而且Delegation调用方不应该知道具体的调用目标是什么,所以这些对象也应该要隐藏具体的细节。

 

三、Inheritance

       解释:

      One of the most important concepts in object-oriented programming is that of inheritance. Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and fast implementation time.

      When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class. This existing class is called the baseclass, and the new class is referred to as the derived class.

        其实,Inheritance,在面象对象囫开发多领域中,得到强大的应用,特别是与虚函数的巧妙应用,是经典用法!

        代码例子:  

// Base class
class Shape 
{
   public:
      void setWidth(int w)
      {
         width = w;
      }
      void setHeight(int h)
      {
         height = h;
      }
   protected:
      int width;
      int height;
};

// Derived class
class Rectangle: public Shape
{
   public:
      int getArea()
      { 
         return (width * height); 
      }
};

//----------------------------------------------------------------------------- 专题二 ----------------------------------------------------------------------------------------------------------------------------

一、虚函数与纯虚函数:
1、虚函数:那些被virtual关键字修饰的成员函数,就是虚函数。虚函数的作用,用专业术语来解释就是实现多态性(Polymorphism),多态性是将接口与实现进行分离;用形象    的语言来解释就是实现以共同的方法,但因个体差异而采用不同的策略。

方法:virtual ReturnType FunctionName(Parameter);

2、纯虚函数:

 那些被virtual关键字修饰的成员函数,就是虚函数, 后面赋值为0.

 方法:virtual ReturnType Function()= 0; 

代码实现例子:

include<iostream>
using namespace std;
class CDocument{
    pulic:
    void OnFileOpen()
    {
       //----
       Serialize();
       //----
    }
    virtual void Serialize(){};
}
class CMyDoc :public CDocument{
   public:
         virtual void Serialize(){
            cout<<"CMyDoc :: Serialize()"
           }
}

//----------------------------------------------------------------------------- 专题三 ----------------------------------------------------------------------------------------------------------------------------

几个经典设计模式:

1、delegation + inheritance(这个组合是最强大的)

2、Compsite

3、Prototype

 

   

      

     

 

   
   

  

 

 

    

     

posted on 2016-03-19 14:57  MSM-敏  阅读(346)  评论(0编辑  收藏  举报

导航