Unsure how to come up with a good design

Posted by Mewzer on Stack Overflow See other posts from Stack Overflow or by Mewzer
Published on 2010-06-05T14:16:20Z Indexed on 2010/06/05 14:22 UTC
Read the original article Hit count: 153

Hello there,

I am having trouble coming up with a good design for a group of classes and was hoping that someone could give me some guidance on best practices. I have kept the classes and member functions generic to make the problem simpler.

Essentially, I have three classes (lets call them A, B, and C) as follows:

class A
{
  ...
  int GetX( void ) const { return x; };
  int GetY( void ) const { return y; };

private:
  B b;    // NOTE: A "has-a" B
  int x;
  int y;
};

class B
{
   ...
   void SetZ( int value ) { z = value };
private:
   int z;
   C c;  // NOTE: B "has-a" C 
};

class C
{
private:
  ...
  void DoSomething(int x, int y){ ... };
  void DoSomethingElse( int z ){ ... };
};

My problem is as follows:

  • Class A uses its member variables "x" and "y" a lot internally.
  • Class B uses its member variable "z" a lot internally.
  • Class B needs to call C::DoSomething(), but C::DoSomething() needs the values of X and Y in class A passed in as arguments.
  • C::DoSomethingElse() is called from say another class (e.g. D), but it needs to invoke SetZ() in class B!.

As you can see, it is a bit of a mess as all the classes need information from one another!. Are there any design patterns I can use?. Any ideas would be much appreciated ....

© Stack Overflow or respective owner

Related posts about c++

Related posts about design-patterns