The best way to learn OOPS is to relate it to the real world.In the world that we live everything that we see or feel is an object.e.g A person in car,The car he is driving, The engine of the car etc.
Lets consider an example of a simple game of card played between 2 players where the player who picks a card of higher rank is the winner.
Lets first identify the list of objects present in the above given problem -
1] The Player's playing the game.
2] A Deck of 52 cards.
3] Each card of a particular type(spade,club,diamond,heart) and rank(ace,one,two ...king).
4] The Game containing a rule(In this case the one who picks the card of high rank wins).
Lets now see the behaviour each of the above identified objects should exhibit -
1] Player -
Properties -
name of the player.
Behaviour -
pick a card from the deck of 52 cards - pick(Deck deck).
show the card which he has picked - getCard().
get the name of the player - getName().
2] Card
Properties -
rank of the card (ace,two,three .... king).
type of the card (spade,heart...).
Behaviour -
compare a card with another card - compareTo(Card card).
3] Deck
Properties -
list of 52 cards.
reference to the card already picked by the 1st palyer so that the 2nd palyer does
not pick the same card.
Behaviour -
pick a card - pick().
4] Game
Properties -
Rule of the game.
Behaviour -
Gets the winner of the game by applying the rule - getWinner(Player one, Player two).
Change the rule of the game - changeRule(Rule newrule)
5] Rule
Behaviour - Gets the winner according to the current rule
getWinner(Player one, Player two).
The following is the entire code of the Card Game -










1 comment:
Nice one this... So I would like think aloud here.... correct me if I am wrong.
1] Player -
Properties -
name of the player.
Behaviour -
pick a card from the deck of 52 cards - pick(Deck deck).
show the card which he has picked - getCard().
get the name of the player - getName().
-----------------------------------------
Should we add another attribute here to identify the player uniquely?
Should we generalize the Player class here? So that he can play any game?
Instead of getCard() and pickCard() --- play() or startPlay() (abstract method)
CardGamer can be an interface? likewise ChessGamer etc
Same player can later play Chess or MineSweeper....
--------------------------------------------------
2] Card
Properties -
rank of the card (ace,two,three .... king).
type of the card (spade,heart...).
Behaviour -
compare a card with another card - compareTo(Card card).
---------------------------------------------------------------------
compare is not the behaviour of this class. THe code part rightly implements Comparable interface though,
But I wouldn't put the compareTo as a behaviour of Card class.
--------------------------------------------------------------------
3] Deck
Properties -
list of 52 cards.
reference to the card already picked by the 1st palyer so that the 2nd palyer does
not pick the same card.
Behaviour -
pick a card - pick().
-------------------------------------------------------------------------------------------------
Your code has pickedCardIndex(which lastpickedIndex), is this used somewhere? purpose?
I don't see a validation in any of the methods in this class.
pick() -> pickCard() (might be better... but doesn't matter much really)
should we have list specifying the cards already picked? THe deck needs to have a status ain't it?
-list of picked cards
------------------------------------------------------------------------------------------
4] Game
Properties -
Rule of the game.
Behaviour -
Gets the winner of the game by applying the rule - getWinner(Player one, Player two).
Change the rule of the game - changeRule(Rule newrule)
-------------------------------------------------------------------------------------------------
attribute
name - Game should have a name (I would add this)
changeRule -> setRule(Rule r)
getWinner? this is already in the Rule class. I am still confused where is the right place to put getWinner() method (Game or Rule)
startGame(), stopGame() would be more appropriate here?
Should Game be a interface with the above methods? And CardPickerGame and concrete class?
------------------------------------------------------------------------------------------
5] Rule
Behaviour - Gets the winner according to the current rule
getWinner(Player one, Player two).
------------------------------------------------------------------------------------------
Rule can be generalised a bit more...
public interface Rule {
public abstract boolean execute();
}
Is Rule a Predicate class?? (not sure... maybe)
public class CardPickerGameRule implements Rule{
public abstract boolean execute(){
}
}
These are my initial taughts...I might be wrong in lot of places. Correct me please...
------------------------------------------------------------------------------------------
Post a Comment