简体   繁体   English

在C ++中如何最好地解耦两个必须相互维护引用集合的类

[英]In C++ how to best decouple 2 classes that must maintain collections of references to each other

I am looking for advice on the most elegant and secure way to decouple two C++ classes that maintain collections of pointers to each other's types. 我正在寻找有关解耦两个C ++类的最优雅,最安全的方法的建议,这些C ++类维护指向彼此类型的指针的集合。

I implemented it recently using a common base class for polymorphism and was told that it was an unsatisfactory solution. 我最近使用一个通用的多态基类来实现它,并被告知这不是一个令人满意的解决方案。 I am eager to learn other ways this can be achieved. 我渴望学习实现这一目标的其他方法。

Thanks in advance... 提前致谢...

I have added a simplified version of the class definitions below. 我在下面添加了类定义的简化版本。 I am aware that the SalesTeam class is not decoupled from SalesPerson here. 我知道,这里没有将SalesTeam类与SalesPerson分离。

// Global Vectors
vector<Customer *> v_Customer;          
vector<SalesPerson *> v_SalesPerson;  
vector<SalesTeam *> v_SalesTeam;


class Person {    // Base Class
};

class Customer: public Person {
private:
    const Person *contact;         // This is the SalesPerson that serves the Customer
public:
    Customer(const int aBirthYear);
    virtual ~Customer() {}
};

class SalesPerson: public Person {
private:
    vector<Person *> v_Client;  // These are the customers that the SalesPerson serves
public:
    SalesPerson();
    virtual ~SalesPerson(){};
};

class SalesTeam {
private:
    vector<SalesPerson *> v_TeamMember;  // These are the sales people in the SalesTeam
public:
    SalesTeam();
};

can you use mediator pattern to handle this decouple issue? 您可以使用调解器模式来解决此分离问题吗? code example: 代码示例:

class Mediator
{
    private:
        //store your relationship in this class
}

class SalesPerson: public Person {
private:
    Mediator mediator;
public:
    SalesPerson();
    virtual ~SalesPerson(){};
};

class SalesTeam {
private:
    Mediator mediator;
public:
    SalesTeam();
};

The unsatisfactory part about your first solution is that the Customer and SalesPerson classes contain references to each other, which gets really hairy to manage. 关于第一个解决方案的不令人满意的部分是,Customer和SalesPerson类包含彼此的引用,这确实很难管理。 I think the "best" solution largely depends on how you intend on customers and sales people interacting with each other. 我认为“最佳”解决方案很大程度上取决于您对客户和销售人员如何进行交互的期望。 From my assumptions about sales people and customers, I'm assuming that customers don't actually require the sales person and vice versa, instead their relationship would probably be defined in a third class ... such as a transaction. 根据我对销售人员和客户的假设,我假设客户实际上并不需要销售人员,反之亦然,相反,他们的关系很可能会在第三类中定义,例如交易。

class Transaction
{
  private:
    Customer *customer;
    SalesPerson *employee;
}

There's hundreds, if not thousands, of design patterns but there is no universal pattern for every situation. 有数百种(即使不是数千种)设计模式,但没有针对每种情况的通用模式。 I recommend drawing your objects on paper and connecting their references with arrows; 我建议在纸上画出对象,并用箭头连接它们的参考。 if you draw a bi-directional arrow ( such as your Customer and SalesPerson relationships ) then stop and re-evaluate. 如果您绘制双向箭头(例如Customer和SalesPerson关系),则停止并重新评估。 Once your diagram is complete, you can use the arrows to visualize your interfaces for each object to understand how everything fits together. 图表完成后,您可以使用箭头可视化每个对象的界面,以了解所有内容如何组合在一起。

You can eliminate the person class by using forward declaration. 您可以通过使用前向声明消除人员类别。

// Global Vectors
vector<Customer *> v_Customer;          
vector<SalesPerson *> v_SalesPerson;  
vector<SalesTeam *> v_SalesTeam;

class SalesPerson; //<-- forward declaration for contact

class Customer {
    const SalesPerson *contact;
public:
    Customer(const int aBirthYear);
    virtual ~Customer() {}
};

class SalesPerson {
    vector<Customer *> v_Client;
public:
    SalesPerson();
    virtual ~SalesPerson(){};
};

class SalesTeam {
    vector<SalesPerson *> v_TeamMember;
public:
    SalesTeam();
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM