Search Sreeni's Insights

Filter by content type
technical June 28, 2011 9 min read

How to model the real world with objects? - Part 1

This article introduces object-oriented thinking by modeling a grocery shop as customers, items and bills, then explains objects through identity, state, behaviour and classes for beginners.

Share:

How to model the real world with objects? - Part 1

Most of the business software we use every day, banking, billing, ticket booking, hospital management, are built with object-oriented languages like Java, C# and C++. Many beginners learn the keywords, class, new, private, extends, and think they know object-oriented programming. But when they are asked to design a small system, they write one big class with all the code inside, which is just procedural code with a class around it. Object-oriented programming is not a set of keywords, it is a way of looking at the problem. In the first series, we discussed how a computer solves a real-world problem with a program. In the previous series, we discussed how to think like a programmer, computational thinking, algorithms, data and the different programming paradigms, and object-oriented paradigm is one of them. In this series, I share the object-oriented way of thinking, the principles behind it and not only the syntax. For each principle, we will start from how people think about it in the real world and in philosophy, and only then move to software. In this article, we are going to see what is real-world modeling and what is an object, its identity, state and behaviour.

Object-Oriented Thinking series:

  1. How to model the real world with objects? (this article)
  2. How do we organize what we know? - Part 2
  3. What is abstraction? - Part 3
  4. What is encapsulation? - Part 4
  5. What is inheritance? - Part 5
  6. What are subtype and subclass polymorphism? - Part 6
  7. How objects relate to each other? - Part 7
  8. How all these concepts connect? - Part 8

TL; DR Summary

  • Object-oriented thinking is looking at a problem as a set of things (objects) which work together, instead of a list of steps.
  • Real-world modeling is choosing the important things of the problem domain and representing them as objects in software.
  • An object has three characteristics, identity, state and behaviour.
  • Identity - What makes one object different from another, even when their values are same.
  • State - The data the object knows about itself at a point in time.
  • Behaviour - What the object can do, and how its state changes.
  • A class is the blueprint, an object is a real thing created from the blueprint.
  • Objects collaborate by asking each other to do things, not by changing each other’s data.

Common Challenges

Thinking in steps only

When we learn programming with procedural style, we naturally think “first read the items, then calculate, then print”. It works for a small program. But when the shop is growing, with discounts, loyalty points, returns and multiple counters, the steps are spreading everywhere and every new rule touches many places. As a result of this, a small change in the discount rule breaks the printing of the bill, and nobody knows why.

Data and logic live separately

In procedural code, the data of an item (price, stock) is in one place and the logic which changes it is in many other places. Anyone can change the stock from anywhere. When the stock suddenly becomes negative, we have to search the whole program to find who changed it. This is one of the main reasons large procedural programs become hard to maintain.

What is object-oriented thinking?

Object-oriented thinking is the way of understanding a problem as a collection of objects, each one responsible for its own data and behaviour, and solving the problem by making these objects work together.

In simple terms, instead of asking “what are the steps?”, we ask “who are the actors and what is each one responsible for?”. The idea is not new, Simula 67, created in Norway in 1967, was the first object-oriented language, and Smalltalk in the 1970s made the term “object-oriented” popular.

Let us take an analogy. A restaurant doesn’t work as one long list of steps. There is a waiter who takes the order, a chef who cooks, a cashier who takes the payment. Each one has his own responsibility and his own information, the waiter knows the table number, the chef knows the recipe. They work together by asking each other, the waiter doesn’t go into the kitchen and cook. This is exactly how an object-oriented program is organized.

From computational thinking to objects

Object-oriented thinking is not a new start, it is the same computational thinking we discussed in Thinking Like a Programmer - Part 1, applied with a different question. Find below how each of the four pillars continues in this series,

  • Decomposition - In procedural thinking, we break the problem into steps. In object-oriented thinking, we break it into things, the customer, the item, the bill, each with its own responsibility.
  • Pattern recognition - Rice, sugar and oil follow the same pattern, so they are not three different things, they are three things of the same kind, an item. Finding kinds is the topic of Part 2.
  • Abstraction - The same pillar becomes the first principle of object-oriented thinking, keeping only what matters about each kind. We discuss it in Part 3.
  • Algorithm design - The algorithms don’t disappear, they move inside the objects as their behaviour. “Calculate total” becomes the job of the bill.

It means, every idea in the previous series is still here. Object-oriented thinking only changes where we put them.

What is real-world modeling?

Real-world modeling is the process of identifying the important things, their information and their behaviour in the problem domain, and representing them as objects in software.

A model is always a simplified version of the reality, like a map or a toy car. It doesn’t have everything, it has only what is needed for the purpose. The following diagram depicts how the things in our grocery shop become objects in the software model.

From real world to objects
From real world to objects

Notice that the “Shopkeeper” is in the real world but not in our model. For a billing system, we may not need him as an object. For a staff attendance system, he will be the most important object. It means, what to model always depend on the problem we are solving.

Steps to model:

Follow the following steps to find the objects in a problem,

  1. Write the problem in simple sentences. “A customer buys items. The shop creates a bill for the customer. The bill shows the amount of each item and the total.”
  2. Underline the nouns, these are the candidates for objects, customer, item, shop, bill, amount, total.
  3. Remove the nouns which are just a value of another object. “Amount” and “total” are information of the bill, not separate objects.
  4. Underline the verbs, these are the candidates for behaviour, buys, creates, shows.
  5. Assign each behaviour to the object which has the information to do it. The bill has the items, so the bill should calculate the total.

Keep in mind that the first model is never perfect. We must refine it as we understand the problem better.

What is an object?

An object is a self-contained unit which represents a thing from the problem domain, with its own identity, state and behaviour.

This way of seeing the world is very old. The Vaisheshika school of Indian philosophy describes reality with categories like dravya (substance, the thing itself), guṇa (quality, what the thing has) and karma (action, what the thing does). Substance, quality and action is almost exactly the identity, state and behaviour we use for an object.

Simply put, an object is something which knows some things and can do some things. The rice packet in our software knows its name and price, and it can tell its amount for a given quantity. The following diagram depicts the three characteristics of an object. The rice01 object has its own identity, its state (name, price and stockQty) is kept inside and only its behaviour can read or change it. The Bill doesn’t touch the price directly, it asks rice01 with getAmount(2). The rice02 object on the right has exactly the same values, but it is a different object, with a different identity.

Identity, state and behaviour
Identity, state and behaviour

Identity

Identity is the property which makes an object unique and different from all other objects, even if all their values are the same.

Think about twins. They may have the same height, the same dress and even the same name tag, but they are two different people. If one twin gets a haircut, the other doesn’t. Similarly, two rice packets of the same brand, same weight and same price are still two different packets. When one is sold, the other is still on the shelf.

In a program, the identity of an object is normally its reference, the location of the object in memory. In a database, we use an ID or a barcode for the same purpose. Be mindful that “equal” and “same” are not the same thing, two objects can have equal values, but they are the same object only when they have the same identity.

State

State is the set of values an object is holding at a particular point in time.

The state of our rice item is name = Rice, price = 50 and stockQty = 120. After the customer buys 2 packets, the stockQty becomes 118, it is still the same object (same identity), only its state is changed. The state answers the question “what does this object know about itself right now?”.

Behaviour

Behaviour is what an object can do, the operations it can perform and how it responds when other objects ask it to do something. Behaviour normally reads or changes the state.

The rice item can calculate its amount for a quantity, reduce its stock when it is sold and change its price when the shopkeeper updates it. Most importantly, the behaviour protects the state. The reduceStock behaviour can check that the stock never goes below zero, otherwise anybody could set it to -5. We will see this in detail in Part 4.

What is a class?

A class is a blueprint or template which defines what state and behaviour its objects will have. An object is an instance created from a class.

The architect’s plan of a house is a class, the actual houses built from the plan are the objects. All houses have the same rooms, but each house has its own address (identity) and its own colour and furniture (state). The following diagram depicts one Item class and three item objects created from it. The class only says every item has a name, price and stockQty and can do getAmount, reduceStock and changePrice, it has no values. Each object fills its own values, rice01 has price 50 and sugar01 has price 40, but all three use the same behaviour defined in the class.

One class, many objects
One class, many objects

Again, a class is something we write once in the program, objects are created many times when the program is running. For a beginner, it is very common to confuse these two. When we say “the item has price 50”, we are talking about an object, when we say “every item has a price”, we are talking about the class. Philosophers have a name for this pair, the particular (this one) and the universal (this kind), and we will see it in the next article.

How objects work together?

A real program is not one object, it is many objects talking to each other. In object-oriented thinking, an object doesn’t reach inside another object and change its data, it asks the other object to do it. This asking is called as sending a message, and in most languages it is simply calling a method.

When the shopkeeper scans the rice packet, the bill asks the rice item “what is your amount for 2 packets?”, the item calculates and replies 100. The bill doesn’t know how the item calculates it, maybe there is an offer price, maybe it is weight based. The bill just trusts the item to give the correct answer. It means, each object has a clear responsibility, and when a rule is changing, we know exactly which object to change.

Wrap up

In this article, we have looked at what is object-oriented thinking, how to model the real world, what is an object with its identity, state and behaviour, and the difference between a class and an object. I really appreciate that you took the time to read this article and I hope this article provided you some insights into the object-oriented way of looking at a problem. Before we go to the principles of object-oriented thinking, we need the thinking tools they are built on. In the next article, I will walk you through how we organize what we know, classification, generalization, specialization and hierarchy, from the real-world and philosophical point of view.

Related Posts