Java – When is an object of real world a (computational) object in OOP world

class-designdesignjavaobject-orientedobject-oriented-design

In an OOP design phase strategy,

Any physical/conceptual object of a system can be modeled(considered) as computational object in your OOP designed program based on below two conditions:

First case: That physical/conceptual object of a system must have it's local state and that state changes over time.

or

Second case: that physical/conceptual object of a system may or may not have it's local state but that same object must influence the state of other physical/conceptual object of a system through interactions.

Above two cases are also supported here, which says: Viewing objects as finite state machines

To illustrate above two cases, below is the object model diagram and class diagram of coin flipping game.

class Coin and class Player type objects, have local state coinOption, as mentioned in first case. class CoinGame type object has no local state but influence the state of other objects(of type Player and Coin), as mentioned in second case.

enter image description here

As per second case, class CoinGame type object influences the state of other objects of type Player and Coin through below interactions, but class CoinGame type object itself does not have local state on it's own.

enter image description here

So, class CoinGame does not maintain any local state and has composite relation with Player and Coin, as per below java code.

public class CoinGame {

        Player[] players = new Player[2];

        Coin theCoin = new Coin();

        CoinGame(String player1Name, String player2Name){

            players[0] = new Player(player1Name);
            players[1] = new Player(player2Name);

        }
       .....
}

Here is the complete code in java.
Above two cases are valid, when you select objects from real world.
Is my understanding correct?

Best Answer

In SICP ยง 1.1.2, it says:

A critical aspect of a programming language is the means it provides for using names to refer to computational objects. We say that the name identifies a variable whose value is the object.

So a computational object is simply that which is pointed to by a variable. It can be an object, a first-class function, or even a number. That's all you need to have a computational object.

As to your first two assertions, that an object can model changes through time by changing its state and/or passing messages around to other objects, those two assertions are both true.

Related Topic