Must Know Design Patterns
In Continuation of our Design patterns Series on LinkedIn , here we are with another design pattern which I prefer to use in notification features of web applications. this is Observer Design Pattern.
Let’s say you follow a social media page. Whenever this page adds a new post, you would like to be notified of the same.
So, in the event that one object (page) does an action (adding a post), another object (follower) is notified. This scenario can be implemented through Observer Pattern.
Let’s have a class Page
and an interface Follower
. The page can have different types of followers, a normal user, a recruiter and an official. We’ll have a class for each follower type and all the classes will implement the Follower
interface.
Here, Page
class is the Subject and the followers are the Observer classes. The observer pattern defines a one to many relationship with the Subject and the Observers. If the subject changes its state (page adds a new post), all the observers i.e followers are notified.
The class Page will have the following methods:
registerFollower()
: This method registers new followers.notifyFollowers()
: This method notifies all the followers that the page has a new post.getLatestPost()
andaddNewPost()
: Getter and setters for the latest post on the page.
On the other hand, the Follower
interface only has one method update()
that will be overriden by the follower types that implement this interface, also called the Concrete Observers.
interface Follower {
public void update();
}
The update()
method is called when the subject needs to notify the observer about the state change i.e. a new post.
Let’s implement the Page class i.e. the subject.
class Page {
private ArrayList<Follower> followers;
String latestPost;
public Page() {
followers = new ArrayList();
}
public void registerFollower(Follower f) {
followers.add(f);
}
public void notifyFollowers() {
for(int i = 0; i < followers.size(); i++) {
Follower follower = followers.get(i);
follower.update();
}
}
public String getLatestPost() {
return latestPost;
}
public void addNewPost(String post) {
this.latestPost = post;
notifyFollowers();
}
}
In this class, we have a list of all followers. When a new follower wants to follow the page, it calls the registerFollower()
method. latestPost
holds the new post added by the page.
When a new post is added, the notifyFollowers()
method is called where it iterates over each follower and notifies them by calling the update()
method.
Now, let’s implement our first kind of follower, User
.
class User implements Follower {
Page page;
public User(Page page) {
this.page = page;
page.registerFollower(this);
}
public void update() {
System.out.println("Latest post seen by a normal user: " + page.getLatestPost());
}
}
When a new User
object is created, it takes the page that it wants to follow and registers for the same. When the page adds a new post, User
is notified through the update()
method that the User
can implement in its own way.
Let’s have two more classes that want to follow the page.
class Recruiter implements Follower {
String company;
// Rest is the same as User class
}
class Official implements Follower {
String designation;
// Rest is the same as User class
}
Let’s test the pattern in the main class. First, create a page and add a new post.
Page page = new Page();
page.addNewPost("I am feeling lucky!");
No one has followed the page yet, so this won’t notify anyone. Now, a normal user follows the page which adds another post.
User user = new User(page);
page.addNewPost("It's a beautiful day!");
Now, the user will be notified and give the following output.
Latest post seen by a normal user: It's a beautiful day!
Next, the recruiter and the official also follow the post.
Recruiter recruiter = new Recruiter(page);
Official official = new Official(page);
page.addNewPost("Ready to go for a run!!");
All three of them will be notified of this acitivity.
Latest post seen by a normal user: Ready to go for a run!!
Latest post seen by a recruiter: Ready to go for a run!!
Latest post seen by an official: Ready to go for a run!!
This is all about Observer pattern, Thanks for Reading and stay tuned for next must know design patterns.!
See you later Alligators..!