Object Oriented

Observer design pattern


Suppose we’ve a system like thermometer that measures the room temperature

public class Thermometer {
    private int temp = 0;
    public void setTemp(int temp) {
        this.temp = temp;
	System.out.println("Subject | temp = " + temp);

    public int getTemp() {
        return temp;

We want to get notified when every change in temperature happens.
Suppose that many other systems wants to be notified and these systems may varies and we want to make a good solution to handle this without the need to modify our code multiple times to handle new added systems

Encapsulation & Data Hiding


a great concept in OOP is Encapsulation which represented in a concept called Data hiding

Data hiding
The concept of data hiding is to hide data!
the reason beyond hiding data is to protect it from user while accessing it

suppose we have the following code

class Person
	char* name;	// C++
	String name;	// Java
	string name;	// C#
	int age;

the two ways to access the data :

  • Setting the value
  • Getting the value


Go to Top