I tried to deepen my understanding of object orientation by n%

Introduction

[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.

My own object-oriented way of thinking

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.

Apologies in advance

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.

Try to practice

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.

Before washing out the class

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.

While identifying the class, add the condition and behavior from time to time

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.

in conclusion

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

I tried to deepen my understanding of object orientation by n%
I tried to summarize object orientation in my own way.
[Must see !!!] I tried to summarize object orientation!
[Personal memo] I tried to study object orientation lightly
I tried to make a parent class of a value object in Ruby
I tried to chew C # (basic of encapsulation)
I was swallowed by the darkness of the romaji, trying to convert my name to romaji
I tried to summarize the state transition of docker
05. I tried to stub the source of Spring Boot
I tried to reduce the capacity of Spring Boot
I tried to investigate the mechanism of Emscripten by using it with the Sudoku solver
I tried to implement the like function by asynchronous communication
I tried to summarize the basics of kotlin and java
I tried to verify this and that of Spring @ Transactional
[Swift] I tried to implement the function of the vending machine
I tried to summarize the basic grammar of Ruby briefly
I tried to build the environment little by little using docker
I tried to build the environment of WSL2 + Docker + VSCode
I tried to make a client of RESAS-API in Java
The illusion of object orientation
I tried to verify yum-cron
I tried to make it possible to set the delay for the UDP client of Android by myself
I tried to solve the problem of "multi-stage selection" with Ruby
I tried to implement Ajax processing of like function in Rails
I tried to build the environment of PlantUML Server with Docker
Summary of results of research on object orientation [Updated from time to time]
I want to limit the input by narrowing the range of numbers
I tried to check the operation of gRPC server with grpcurl
I tried to summarize the methods of Java String and StringBuilder
[Java] I tried to make a maze by the digging method ♪
I tried to solve the problem of Google Tech Dev Guide
By checking the operation of Java on linux, I was able to understand compilation and hierarchical understanding.
I tried to summarize iOS 14 support
I tried to interact with Java
I tried to explain the method
I tried to summarize Java learning (1)
I tried to understand nil guard
I tried to summarize Java 8 now
I tried to chew C # (polymorphism: polymorphism)
I tried to explain Active Hash
I tried using GoogleHttpClient of Java
I tried connecting the dot counter to the MZ platform by serial communication
I tried to summarize the key points of gRPC design and development
I tried to make my own transfer guide using OpenTripPlanner and GTFS
I tried to make full use of the CPU core in Ruby
I tried to visualize the access of Lambda → Athena with AWS X-Ray
I tried to measure and compare the speed of GraalVM with JMH