• 转 面向对象设计(OOD)中的替换原则


    http://blog.csdn.net/starlee/article/details/945728

      我们知道,在面向对象语言中,公有继承是IS-A的关系,也就是说子类是一种基类,就像说轿车是一种汽车一样。但是,有时候逻辑上正确的公有继承却会违反替换原则。面向对象设计中的替换原则是:
        子类必须能够替换掉它们的基类。
        也就是说,代码中基类出现的地方都能用子类来替换,就跟汽车能用的地方都能用轿车一样。但是,如果设计不合理,就会违反这个原则,给开发带来隐患。
        下面就以一个C++的例子来说明:

    class CShape
    {
        
    public:
        
    virtual ~CShape() {};
        
    virtual double GetArea() = 0;
    };

    class CRectangle : public CShape
    {
    public:
        
    virtual ~CRectangle() {};
        
    double GetArea() { return m_dWidth * m_dHeight; };
        
    void SetWidth(double dWidth) { m_dWidth = dWidth; };
        
    void SetHeight(double dHeight) { m_dHeight = dHeight; };
        
    double GetWidth() const { return m_dWidth; };
        
    double GetHeight() const { return m_dHeight; };

    private:
        
    double m_dWidth;
        
    double m_dHeight;
    };

        现在,我们需要一个正方形类CSquare。我们知道,正方形是一种宽和高相同的长方形,也就是说正方形和长方形符合IS-A的关系。那我们需要的类CSquare是否应该从类CRectangle来继承呢?
        我们现在先来看看类CSquare从类CRectangle继承的情况:

    class CSquare : public CRectangle
    {
    public:
        
    virtual ~CSquare() {};
        
    void SetWidth(double dWidth) 
        { 
            CRectangle::SetWidth(dWidth); 
            CRectangle::SetHeight(dWidth);
        };
        
    void SetHeight(double dHeight) 
        { 
            CRectangle::SetHeight(dHeight); 
            CRectangle::SetWidth(dHeight);
        };
    };

        类CSquare中对方法SetWidth和方法SetHeight的重载解决了正方形的宽和高相等的问题。但是请考虑下面的代码:

    void FncTest(CRectangle& r)
    {
        r.SetWidth(
    13);
        assert(r.GetArea() 
    == 169);
    }

    void Test()
    {
        CSquare s;
        s.SetWidth(
    5);

        FncTest(s);
    }

        当我们向函数FncTest传递一个指向类CSquare对象的引用的时候,这个函数就会出现断言错误,因为类CSquare对象的高度没有改变。这就违反了替换原则,函数FncTest中的类型为基类CRectangle的参数不能用子类CSquare来替换。错误原因很简单,类CRectangle中的方法SetWidth和方法SetHeight不是虚函数,因此不具备多态性。在这种情况下,我们就必须去修改类CRectangle。下面是修改后的代码:

    class CRectangle : public CShape
    {
    public:
        
    virtual ~CRectangle() {};
        
    double GetArea() { return m_dWidth * m_dHeight; };
        
    virtual void SetWidth(double dWidth) { m_dWidth = dWidth; };
        
    virtual void SetHeight(double dHeight) { m_dHeight = dHeight; };
        
    double GetWidth() const { return m_dWidth; };
        
    double GetHeight() const { return m_dHeight; };

    private:
        
    double m_dWidth;
        
    double m_dHeight;
    };

    class CSquare : public CRectangle
    {
    public:
        
    virtual ~CSquare() {};
        
    virtual void SetWidth(double dWidth) 
        { 
            CRectangle::SetWidth(dWidth); 
            CRectangle::SetHeight(dWidth);
        };
        
    virtual void SetHeight(double dHeight) 
        { 
            CRectangle::SetHeight(dHeight); 
            CRectangle::SetWidth(dHeight);
        };
    };

        这样,上面提到的问题就解决了,修改也不是很复杂。然而,如果派生类的创建会导致我们修改基类,这就意味着设计是有缺陷的。我们先不管是否有设计缺陷,而是先接受上面的修改。现在类CRectangle和类CSquare都能工作,在代码中也可以接受指向类CRectangle的指针或引用的函数传递类CSquare,而类CSquare也可以很好的保持正方形的特性。看起来,这样的设计已经符合替换原则了。但是,等等,请看下面的函数:

    void FncTest(CRectangle& r)
    {
        r.SetWidth(
    5);
        r.SetHeight(
    6);
        assert(r.GetArea() 
    == 30);
    }

        对这个函数来说如果传递来的是类CRectangle的对象,则运行正确;如果传递来的是类CSque的对象,那就会出现断言错误!仍旧是违反了替换原则
        上面的例子说明了,即使两个类是IS-A的关系,也不一定要用公有继承来实现。
        其实,在面向对象设计(OOD)中,IS-A关系是就行为方式而言的。在本例中,虽然类CRectangle和类CSquare在逻辑上是IS-A关系,但是他们的行为是不同的,因此也就不能让类CSquare从类CRectangle公有继承,而是从类CShape公有继承。
        下面是正确的代码:

    class CShape
    {
    public:
        
    virtual ~CShape() {};
        
    virtual double GetArea() = 0;
    };

    class CRectangle : public CShape
    {
    public:
        
    virtual ~CRectangle() {};
        
    double GetArea() { return m_dWidth * m_dHeight; };
        
    void SetWidth(double dWidth) { m_dWidth = dWidth; };
        
    void SetHeight(double dHeight) { m_dHeight = dHeight; };
        
    double GetWidth() const { return m_dWidth; };
        
    double GetHeight() const { return m_dHeight; };

    private:
        
    double m_dWidth;
        
    double m_dHeight;
    };

    class CSquare : public CShape
    {
    public:
        
    virtual ~CSquare() {};
        
    double GetArea() { return m_dWidth * m_dWidth; };
        
    void SetWidth(double dWidth) { m_dWidth = dWidth; };
        
    double GetWidth() const { return m_dWidth; };

    private:
        
    double m_dWidth;
    }; 
  • 相关阅读:
    开发工具
    CPU知识
    GMAP.NET
    vtordisp
    Hadoop HBase 配置 安装 Snappy 终极教程
    asp.net Vs访问正常,iis访问出现各种问题的部分处理方法
    MVC项目小结:动态菜单
    视频的采集和动态显示
    《编程之美》
    Nginx实现简单的负载均衡web访问
  • 原文地址:https://www.cnblogs.com/kex1n/p/2304357.html
Copyright © 2020-2023  润新知