Friday, August 22, 2014

Programming Design Pattern - Observer Pattern Applied - Best Practise

Hi there!

Today i'm gonna show the Observer design pattern in action. The Observer design pattern is a very useful programming design pattern whenever you need to notify classes or objects depending on changes or on user interactions. This is one of the most used pattern i think.

I'm assuming here you already know the concepts and i'll be focusing on practise. The example i will provide is a nice way to show it how it could looks like. You can always come back here, take it, adapt it and use it in your applictions as you may need. So be sure you bookmark it or join the group here on the right side of this post subscribing it.

First of all, let's take a look at the UML diagram of it. After that we will take the analogy for our example.

The UML Diagram of the Observer Pattern


Pay close attention, because once you understand that, everything will become clear and simple to understand. That's the reason I'm putting always the UML first. That way you'll get an eye for it with the time.





The example

In our example we will see, how we could notify all postmans every time new posts arrives in the post office central station.

Observer and Observable

Those interfaces are part of the default java library. For that reason we will not invent the wheel again. We will just use it.

The PostOffice Observable

This will be our observable. In other words, the object which holds a register method for observers interested in being notified, as soon as changes in the post office occurs.

import java.util.Observable;
public class PostOffice extends Observable {
    public void receberCartas(){
        System.out.println("New post arrived!");
        setChanged();
    }
    public void distributePost(){
        notifyObservers();
    }
}

The Postman Observer

This is the observer itself interested in being notified, as soon as changes in the post office occurs.

import java.util.Observable;
import java.util.Observer;
public class Postman implements Observer {
    @Override
    public void update(Observable o, Object arg) {
        System.out.println("Postman: distributing posts...");
    }
}

The Test

Let's see how those classes interacts together. Pay close attention, because this pattern will surely be one of the most used in your programming life! ;)

public class Client {
    public static void main(String[] args) {
        PostOffice postoffice =  new PostOffice();
        // adding postmans to the post office (observers)
        postoffice.addObserver(new Postman());
        // Simulating arrival of letters in the central post office
        postoffice.receberCartas();
        // simulating postmans distributing letters
        postoffice.distributePost();
    }
}

That's all! I hope you like it!


😱👇 PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO ðŸ˜±ðŸ‘‡

Be sure to read, it will change your life!
Show your work by Austin Kleonhttps://amzn.to/34NVmwx

This book is a must read - it will put you in another level! (Expert)
Agile Software Development, Principles, Patterns, and Practiceshttps://amzn.to/30WQSm2

Write cleaner code and stand out!
Clean Code - A Handbook of Agile Software Craftsmanship: https://amzn.to/33RvaSv

This book is very practical, straightforward and to the point! Worth every penny!
Kotlin for Android App Development (Developer's Library): https://amzn.to/33VZ6gp

Needless to say, these are top right?
Apple AirPods Pro: https://amzn.to/2GOICxy

😱👆 PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO ðŸ˜±ðŸ‘†