Sign in to follow this  

Which Design Pattern To Use?

This topic is 3858 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

Here is my problem. When I originally thought about this I was planning on using a observer pattern but not sure if that will work or if I need to change my design. Right now I have 2 classes, and player class and party class. My trouble is I want to be able to have X amount of players join a party and then have a method is the party classes called DisplayParty() which will print out all the stats of all the player in that party. What is the best way to go around this problem?

Share this post


Link to post
Share on other sites
That is one way and forgive becuase I am not too familiar with the vector type but what happens if i want to kick a certain player out of the party but I am not sure where he is in the vector.

Share this post


Link to post
Share on other sites
Quote:
Original post by RyanZec
That is one way and forgive becuase I am not too familiar with the vector type but what happens if i want to kick a certain player out of the party but I am not sure where he is in the vector.


Take a look at a Standard C++ library refernce. The ares you want to focus on in particular are part 5 in the above link, "Algorithms".

The std::vector<> reference will come in handy too, under section 2 "Containers".

Share this post


Link to post
Share on other sites
If Party is a vector of pointers to Character objects or subclasses of Character, and (anything derived from) Character can PrintStats() then use

std::for_each(Party.begin(), Party.end(), std::mem_fun(&Character::PrintStats));

Make PrintStats() the responsibility of the individual Character and it doesn't matter how many Characters there are in your party.

L
-

Share this post


Link to post
Share on other sites
well party in a class of its own becuase it does not need anything from the class character, a party is not a character but a group of characters linked together. I am going to have a vector of list of character pointers in the party class and interate through them to print the stats. I just need to add methods to party to add and kick player from the party. Ofcourse now I am getting some messed up results happening to one section of code when I change anything in another un related section od code(as in the one does not require the other to compile) so need to figure that out but I continue on adding the method for join/add/leave/kick to player/party classes.

Also this is not a homework assignment, just something I thought over that would be interesting to me to try to implement.

Share this post


Link to post
Share on other sites
This is array/list manipulation. Not much to be said here. Add element and remove element. Join and remove/kick are just the add/remove operations on that list.

If you're doing single-threaded simple aproach, then you don't need any observer pattern or anything complex, since you just call display party whenever you want.

If you're doing observer, you'll need a callback to let you know what has changed:

class PartyMembershipCallback
{
void new_party( Party *p );
void member_added( Party *party, Player *player );
void member_removed( Party *party, Player *player, bool was_kicked = false );
void party_disbanding( Party *p );
}



Then you call these methods from within your Party object as needed to notify all observers.

Share this post


Link to post
Share on other sites

This topic is 3858 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this