Jump to content
  • Advertisement
Sign in to follow this  
Chris81

What OOP pattern to use?

This topic is 4809 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I'm fairly new to C++ OOP, so I need help decided if/what pattern to use for my situation. This is probably very easy, and my brain is just cloudy. I have class A. Class A needs to perform operations on class X, so it has an aggregation relationship. Now, class Y and Z perform the same operations as class X, and class A may want to use Y or Z instead. Okay, easy solution. You have interface I that X, Y, and Z inherit from. Class A then has a member variable of type interface I. And then calls I->foo(). But, what if class A may need to call X and Y, or X and Z, or all three? The only way I can think of is to have a bit-field that has USE_X USE_Y and USE_Z bits. Then create an aggregate member variable of each class type X, Y, and Z. Then do an if on USES field against each bit, if true, call X->foo(), Y->foo(), or z->foo(). But there has to be a more OO way to do this. Unfortunately my GoF book isn't with me (of which I have read very little, which I now regret.) Thanks!

Share this post


Link to post
Share on other sites
Advertisement
Your post is a bit confusing, but if I understand what you are trying to do correctly, then I think you want something like this: X, Y, and Z inherit from I. Each instance of class A has a reference/pointer/handle (depending on the language) to an object that implements interface I. Each instance of A can then make use of X, Y, or Z simply by making the pointer point to an object of the appropriate type. If that is not what you are looking for, you will probably have to be clearer about what your problem is, as I can read your post several different ways. Hope that helps.

Share this post


Link to post
Share on other sites
Quote:
Original post by Chris81
Hello,

I'm fairly new to C++ OOP, so I need help decided if/what pattern to use for my situation. This is probably very easy, and my brain is just cloudy.

I have class A. Class A needs to perform operations on class X, so it has an aggregation relationship. Now, class Y and Z perform the same operations as class X, and class A may want to use Y or Z instead.

Okay, easy solution.

You have interface I that X, Y, and Z inherit from. Class A then has a member variable of type interface I. And then calls I->foo().

But, what if class A may need to call X and Y, or X and Z, or all three?

Will it? That's an odd thing for a class to be required to do, so I would double check your logic first.
Quote:

The only way I can think of is to have a bit-field that has USE_X USE_Y and USE_Z bits. Then create an aggregate member variable of each class type X, Y, and Z. Then do an if on USES field against each bit, if true, call X->foo(), Y->foo(), or z->foo().

But there has to be a more OO way to do this. Unfortunately my GoF book isn't with me (of which I have read very little, which I now regret.)

Thanks!


There is a more OO way to do this, and you should definantly look in GoF (it has at least one of the possible ways to do this). But, like I said, evaluate your logic first to decide if it will ever actually need to call upon all three.

Share this post


Link to post
Share on other sites
mumpo: sorry, I'll try to elaborate more clearly

washu: I'm pretty sure I'm using sound logic in this case, but you tell me.

Here is an example of what I'm trying to do.

I have a CFarmer class. The CFarmer class will have lots of operations such as FeedAnimals(). Then I have classes such as CCow, CChicken, and CDog. When I create a CFarmer class, I'm going to want to say CFarmer->SetAnimals( COW & CHICKEN ). Then when I call CFarmer->FeedAnimals() it may do some common stuff first like PutOnOveralls(), then call CCow->Feed() and CChicken->Feed() because COW & CHICKEN bits were set. Then CCows feed() may involve completely different steps than CChickens feed(), such as what type of food and the preparation process.

As you can see, I dont know which or how many of the common animal objects will be called until runtime.

Pretty stupid example, but I'm corny so it fits, heh.

Share this post


Link to post
Share on other sites
I just thought of a GoF pattern I remember reading the synopsis of in the front of the book cover - Chain of Command pattern? Would that fit?

Share this post


Link to post
Share on other sites
Ok, well, you don't need flags it would be more like this:


abstract class Animal { public void Feed(); }
class Chicken : Animal {
public override void Feed() {}
}
class Cow : Animal {
public override void Feed() {}
}
class Farmer {
public void Add(Animal a) { animals.Add(a); }
public void Feed() {
foreach(Animal a in animals) {
a.Feed();
}
}
}

Farmer f = new Farmer();
f.Add(new Chicken());
f.Add(new Cow());
f.Feed();


Note the example is in C#, but the principle applies to any object oriented language.

Share this post


Link to post
Share on other sites

class Animal
{
public:
virtual Feed();
};
class Chicken : public Animal
{
public:
virtual Feed() { GiveGrain(); }
}
class Farmer
{
std::vector<Animal*> AnimalsToFeed;
public:
void FeedAnimals()
{
std::vector<Animal*>::iterator begin, end;
begin = AnimalsToFeed.begin();
end = AnimalsToFeed.end();
while(begin < end)
{
(*begin)->Feed();
++begin;
}
}
}





No matter what animal is, he'll feed it.

Edit: Washu beat me...

Share this post


Link to post
Share on other sites
Hmm, well that's easy enough. However, there will be lots of operations on the animals, and there will be lots of places in the farmers logic that will call these operations. If everytime I need to call an animal operation I have to write a loop, that could get ugly, wouldn't it?

Share this post


Link to post
Share on other sites
If each animal will deal with each action differently, then they will need to have functions to perform the action anyways. If they share behavior, then you can move it to the base class.

Share this post


Link to post
Share on other sites
Yeah, they will, the objects will always share a type of action, just go about it different ways. But I'm not quite sure how that applies to having to write lots of loops.

If I use an array of animals, then every place I call the "shared logic" functions I will have to write a loop. If this is a large class then that will be a lot of loops. Still seems there shoud be a more OO way of doing this, don't you think?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!