如何在保持其使用一致的同时分配责任?

时间:2013-02-05 01:18:32

标签: c++

我有许多类形成具有基类及其继承的类层次结构。虽然这些责任没有结合,但每个班级都承担了很多责任。我把不同的职责放在一个班级,因为我可以很容易地使用它们(我不能很清楚地表达它,请参阅说明的代码)。但这似乎不是一个好的设计。如何在保持易用性的同时拆分大类?感谢您的建议和意见!请在下面找到说明代码。

// Base class.
class Base
{
public:
    // For responsibility A
    virtual void A1();
    virtual void A2();
    ...

    // For responsibility B
    virtual void B1();
    virtual void B2();
    ...

    // More responsibilites.
    ...
};

// Derived class 1.
class Derived_1 : public Base
{...};

// More derived classes.
...

// A function use it.
void Fun()
{
    Base* p = new Derived_1;
    p->A1(); // Here A1 and B1 are binded in the class Base, thus it make sure
    p->B1(); // their uses are consistent. If they are separated, how to ensure it? 
}

2 个答案:

答案 0 :(得分:2)

通过在课堂上承担多项责任,您将破坏设计的Single Responsibility Principle

通常,好的设计遵循SOLID原则。

分手。

  1. 为每项责任定义一个界面。
  2. 尽量不要使用多重继承 - use composition instead
  3. 我添加了一些示例代码来说明这个概念:

    #include <iostream>
    
    //Interface for A
    class ResponsibilityA
    {
        public:
        virtual ~ResponsibilityA(){};
            virtual void A1() = 0;
            virtual void A2() = 0;
    };
    
    
    //Interface for B
    class ResponsibilityB
    {
            public:
        virtual ~ResponsibilityB(){};
            virtual void B1() = 0;
            virtual void B2() = 0;
    };
    
    //Concrete classes for each interface
    class ResponsibilityAExecutor : public ResponsibilityA
    {
            public:
            virtual void A1(){ std::cout<<"A1 executed\n"; };
            virtual void A2(){ std::cout<<"A2 executed\n"; };
    };
    
    class ResponsibilityBExecutor : public ResponsibilityB
    {
            public:
            virtual void B1(){ std::cout<<"B1 executed\n"; }
            virtual void B2(){ std::cout<<"B2 executed\n"; }
    };
    
    // Now we use the interface implementations in a class that needs
    // to combine both responsibilities
    class PlanExecutor
    {
    public:
            PlanExecutor( ResponsibilityA& a, ResponsibilityB& b ) : 
            a_(a),
            b_(b)
            {
    
            }
            void ExecutePlan()
            {
                    std::cout<<"Executing first part of the plan:\n";
                    a_.A1();
                    b_.B1();
                    std::cout<<"Executing second part of the plan\n";
                    a_.A2();
                    b_.B2();
            }
    private:
            ResponsibilityA &a_;
            ResponsibilityB &b_;    
    };
    
    int main()
    {
            ResponsibilityAExecutor a;
            ResponsibilityBExecutor b;
            PlanExecutor p(a,b);
            p.ExecutePlan();
            return 0;
    }
    

    最后,我还需要强调命名的重要性。

答案 1 :(得分:0)

我想说如果A和B的责任完全不同,为两者创建单独的类,并让孩子实现他们想要的方式,这样一些孩子只想要A或只有B或两者兼而有之,它使其易于管理和维护

// Base class.
class Base
{
public:
    // For responsibility A
**Goes to class A**
    virtual void A1();
    virtual void A2();
    ...


**//Goes to class B**
    // For responsibility B
    virtual void B1();
    virtual void B2();
    ...

    // More responsibilites.
    ...
};

// Derived class 1.
class Derived_1 : public Base **//inherits from A**
{...}; 

class Derived_2:Public A,Public B