[Article that understands object orientation 5000% (practice)](/ gorillab / items / fab2a6637f681221f687 ) Was very interesting, so I wanted to put it into practice, so I posted it for the first time.
I think of the word object-oriented object as an object in the antonym of subject, that is, an object.
However, for those who are not object-oriented, I often explain that "objects are'things'." However, it is called "thing" instead of "thing". It is not limited to physical existence, but it can also be an abstract concept, so I often explain that it is quite difficult.
I'm sorry for my convenience, but the implementation example is written in Java, which I'm used to writing. I don't have an environment to pass the compiler at hand, so if there is an error, I will fix it later. I haven't written Javadoc either. We apologize in advance.
Based on the above, let's tackle the problem.
I'm sorry for the full text of the problem, but please refer to the source article. Three players, A to C, play a game like Indian poker with cards 1 to 5.
I think it's a straightforward approach to imagine a scene where you play this game in the real world and identify the class from there. However, in my sense, the real world is a little unwieldy.
What I want to say is that when you look at this problem, it's probably natural to think of "three" as the characters. However, strictly speaking, in addition to the player, "the person who manages the progress of the game" and "the person who judges whether the answer is correct" should also be needed. (In the first place, it may not be a "person".) Unfortunately, I don't think there are usually such people in the real world. Each player will manage the progress and judge the correctness of the answer.
In the world of programming, you can think about things without being bound by the constraints of reality, so I think it would be happier to think of an ideal world a little more than the real world.
First of all, you need a player and a card.
Make sure the player has a name. In addition, each player has one card, so have a field and getter / setter ready.
Player.java
public class Player {
private String name;
private Card card;
public Player(String name) {
this.name = name;
}
public void getName() {
return name;
}
public void getCard() {
return card;
}
public void setCard(Card card) {
this.card = card;
}
}
The number on the card will not change after you decide it first, so set it in the constructor and prepare only the getter. By doing this, it becomes a so-called immutable object.
Card.java
public class Card {
private int number;
public Card(int number) {
this.number = number;
}
public int getNumber() {
return number;
}
}
Next, the set of cards is represented by another class called "Deck". Of course, it can be expressed as a simple list of Cards, but since the act of "shuffling and pulling one card at a time" is imagined, it should be convenient to extract the "concept of deck" as a class. is.
Deck.java
public class Deck {
private List<Card> cards;
public Deck() {
//For simplicity, make a set of 1 to 5 cards in the constructor.
cards = new ArrayList<>();
for(int i = 0; i < 5; i++) {
cards.add(new Card(i + 1));
}
}
public void shuffle() {
//Shuffled with the Collection API for simplicity
Collections.shuffle(cards);
//If you like, you can implement shotgun shuffle etc.
}
//My turn! If you call this method, you can draw one card from the top
public Card draw() {
return cards.remove(0);
}
}
Next, let's introduce "the person who manages the progress of the game". This person will deal cards to all players and let Mr. A answer in order. The name of the class seems to be a better name, but I'll imagine something like a casino table and leave it as a dealer. Proceed with the image of the dealer surrounding three players.
Dealer.java
public class Dealer {
private List<Player> players;
public Dealer() {
//Enclose 3 people in the constructor for simplicity
players = new ArrayList<>();
players.add(new Player("A"));
players.add(new Player("B"));
players.add(new Player("C"));
}
//Deal cards to players (I'm wondering if the method name is deal)
public void deal() {
//Shuffle your new deck
Deck deck = new Deck();
deck.shuffle();
//Distribute one to each player
for(Player player: players) {
Card card = deck.draw();
player.setCard(card);
}
}
At this point, all you have to do is determine the outcome of the game. And if you think about it carefully, even if each player does not judge the result by looking at the numbers of the cards of all the other players, even if the dealer judges by looking at the contents of the cards in order from Mr. A, the result You will notice that does not change. (It's not very good as a game, but ...)
Dealer.java
public class Dealer {
//(The above content is omitted)
//Strictly speaking, this method should be devised so that it can only be called after the deal method has been called.
public void judge() {
//In this, create a process to judge in order from Mr. A's card
int currentPlayerIndex = 0;
while(true) {
Player currentPlayer = players.get(currentPlayerIndex);
int currentPlayerNumber = player.getCard().getNumber();
//Specific judgment processing is omitted.
//(Seriously, it seems a little strange to make this, but
//It feels like I just need to convey the atmosphere. )
//If the result is known, the iterative process is exited (condition judgment variables are appropriate).
if(result != UNKNOWN) {
break;
}
//If you don't know the result, go to the next player
currentPlayerIndex++;
currentPlayerIndex %= players.size();
}
}
}
That is an example of the so-called "my thoughts" implementation. Today, it's a lie. sorry. Depending on the scale of the programming target and interests, I think about the particle size of class division and which method to implement in which class each time, but if the scale is about the given subject, think as above. I will.
By thinking about various things in my head and outputting them in the form of articles, I was able to gain awareness within myself, so I think that my understanding of object-oriented programming has deepened a little. I don't think this is the correct answer, but I hope it will be helpful to you as one of the ideas.
Recommended Posts