The course is part of this learning path
In this course, we will learn the concepts of microservice and spring framework with a focus on design patterns.
- Learn about various design patterns
- Beginner Java developers
- Java developers interested in learning how to Build and Deploy RESTful Web Services
- Java Developers who want to develop web applications using the Spring framework
- Java Developers who want to develop web applications with microservices
- Java Developers who wish to develop Spring Boot Microservices with Spring Cloud
- Basic Java knowledge
Hello, my dear friends. In this video, we will examine the Observer Design Pattern. So, let's begin. According to GoF definition, observer pattern defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. It's also referred to as the publish subscribe pattern. In the observer pattern, there are many observers that are observing a particular subject.
Observers register themselves to a subject to get a notification when there is a change made inside that subject. An observer object can register or unregister from a subject at any point of time. It helps in making the objects loosely coupled. Observer pattern is used when there is one to many relationship between objects, such as, if one object is modified, its dependent objects are to be notified automatically. Observer pattern falls under the behavioral pattern category. The observer design pattern maintains one to many dependency between subject as observable and its dependents as observer in such a way that whenever the state of the subject changes, its dependents get notified. As you see in this UML diagram, the observer pattern has four participants.
Subject is an interface or abstract class defining the operations for attaching and de-attaching observers to the subject. ConcreteSubject is the concrete subject class. It maintains the state of the object and when a change in the state occurs, it notifies the attached observers. Observer is an interface or abstract class defining the operations to be used to notify this object. ConcreteObserver is concrete observer implementations. The observer design pattern is a design pattern in which an object called the subject maintains a list of its dependents called observers and notifies them automatically of any state changes, usually by calling one of their methods.
The observer design pattern is one of 23 well-known Gang of Four design patterns. The observer design pattern is mainly used to implement distributed event handling in event-driven systems. In such systems, the subject is usually called a source of events while the observers are called sync of events. So, let us take a real life implementation of the observer design pattern. A newsletter with sales and new products of the shop is the subject. All the subscribers in this newsletter are observers. Every time a new product is added to the shop, the marketing sends a newsletter. It might include media such as video and photos of the product.
The subscribers are notified about the new product. In this example, the subscribers are observers and the store is the subject. Most modern languages have built in event constructs which implement the observer pattern components. Java also offers observer and observable to support built in event constructs for implementing observer patterns. These have been available since Java 1, but in Java 9 these are declared deprecated/obsolete because the event model supported by observer and observable is quite limited. The order of notifications delivered by observable is unspecified, and state changes are not in one-for-one correspondence with notifications. For reliable and ordered messaging among threads, you can use one of the concurrent data structures in the java.util.concurrentpackage. Observable classes are used to create sub-classes that other parts of the program can observe. The observer pattern is also known as dependence or publish subscribe. Observer pattern describes the coupling between the objects and the observer. Now, let's create a project with the observer pattern. Create a new project and name it Observer. I'd like to create a project for number conversions. We'll enter a decibel number and get its binary, octal, and hexadecimal equivalents. Let's get started. First, we need an abstract class, let's call it Observer.
Now, in this class we have to define our subject, but we haven't created it yet so I will add it first and then create the class. Also, we need an update method. Okay, done. Let's add our subject class. We have to import list libraries, define an observer list. Also, we need state. Generate getter and setter of state. Now, we need two methods. First one is attached. Now to communicate, we need notify method. In setState, we have to call the notify method. Now, we will define our number classes.
First, we will start with binary. Extends from observer. Constructor will take a subject as a parameter. Let's override the update method. In the update method, we can use the binary string method of integer class. Done. Let's add Octal observer. If you want you can copy from binary. We will only change to BinaryString toOctalString. Let's add hexadecimal. toHexString.
Okay, done. Let's use in main. First, we need a subject. Now, we will define hexa, octal, and binary instances. First, let's set 12. After that, let's set 14. Okay, done. Let's run and look at the result. We can see the binary, octal, and hexadecimal equivalents of 12 and 14.
With an observer, we can update all the values of the numbers. We have now finished the observer design pattern. In the next video, we will examine the strategy design pattern. So, I'll see you in the next video my friends.
OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.