Object Oriented Thinking — Part I

Naveen Gaddi
3 min readJul 4, 2022

In this tutorial I’ll show you how to solve/start thinking problem solving using Object Oriented way.

If you are already solid in OOP then this might be refresher, this post is for people who want’s to learn designing solutions in Object Oriented way.

Prerequisites :

  • Java 15 or above ( Good language to show OO ; download from here sdkman)
  • IDE ( Download from here IntelliJ CE ) | any other Java supported IDE
  • Basic Understanding of programming

Terminology :

  • Object Oriented = Treating domain as real object and actions that can do is called behaviour. ( sounds complex ? it becomes clear by reading entire blog)

Before we begin, let’s try to understand these concepts by implementing a solution for our client.

A bit of background about client!

Client is a fan of mathematics and they want to build an application that can compare two probabilities.

Pretty simple requirement right!

Implementation :

As a dev what will be our thinking process when we get above requirement … ?

  • Read two inputs
  • Write “if” condition that compare both inputs
  • Display result based the condition above.

Well if we think like above then our code will be somewhat similar to below.

import java.util.Scanner;public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double p1 = scanner.nextDouble();
double p2 = scanner.nextDouble();
if (p1 == p2) {
System.out.println("P1 and P2 are equal");
} else {
System.out.println("P1 and P2 are not equal");
}
}
}

When we run above code and supplying below values then results in

java Main.java
0
1
P1 and P2 are not equal

Finally it works …!!!

Retrospect :

Let’s look back and retrospect by posing below questions!

  • What is object here and what is behaviour?
  • What will happen if client comes with new requirement for and/or of probabilities?
  • is the above tightly coupled with business logic(if condition) and reading inputs ?
  • can probability more than 1 or less than 0 ?
  • is the client requirement comparing means equality ?

Make it better ?

Think of what object to create ?

Client’s requirement is probability how about creating class Probability and that holds a value inside

Think of what action we want to perform ?

At the moment client interested in comparing two probabilities so let’s add behaviour equals (to our Probability class) which accepts another probability as an argument to compare return result.

Think of what validations we need to perform ?

According Maths probability values only ranges from [0,…,1] so I can validate value coming in.

When we think of above three questions and our code will be something like below

//1. creating a class probability
public class Probability {

//2. creating data member to hold probability value
private final double value;

//3. constructor to create probability object
public Probability(double value)
throws InvalidProbabilityException {
validate(value);
this.value = value;
}
//4. Equality check by accepting another probability
public boolean equals(Probability that) {
if (this == that) return true;
return this.value == that.value;
}
//5. validate probability range [0,...,1]
private void validate(double value)
throws InvalidProbabilityException {
if (value > 1 || value < 0) {
throw new InvalidProbabilityException();
}
}
}

Playing with above code

//Create some objects using above class
Probability probabilityOne = new Probability(1);
Probability probabilityZero = new Probability(0);
//Equate objects
probabilityOne.equals(probabilityZero) => this will return false
//Create some objects using above class
Probability probabilityOne = new Probability(1);
Probability anotherProbabilityOne = new Probability(1);
//Equate objects
probabilityOne.equals(anotherProbabilityOne)
=> this will return true

Now we can ship the above class anywhere and use it…!!!

Wanna try out?

Clone this repo : https://github.com/naveengaddi/probability and start experimenting.

To be continued … in next post.

--

--