1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Using Design Patterns in Java Projects

Observer

Contents

keyboard_tab
Design Patterns
1
Design Patterns
PREVIEW2m 26s
2
Singleton Pattern
PREVIEW3m 13s
9
Decorator
7m 48s
10
Flyweight
9m 20s
11
Proxy
7m 36s
12
Facade
6m 5s
14
Command
11m 44s
15
Iterator
8m 24s
16
Visitor
9m 4s
17
Mediator
11m 38s
18
Memento
9m 48s
19
Observer
10m 40s
20
Strategy
8m 27s
21
State
10m 27s
22
Template
7m 56s
23
MVC
10m 27s
26
DAO
10m 27s
27

The course is part of this learning path

Start course
Overview
Difficulty
Intermediate
Duration
4h 56m
Students
20
Ratings
5/5
starstarstarstarstar
Description

This course takes an in-depth look at how to use design patterns in your Java projects. We will then run through some example questions of what you could expect from the Oracle Certified Java EE exam.

Learning Objectives

  • Understand what design patterns are
  • Learn about the myriad of design patterns that you can use in your projects

Intended Audience

This course is intended for anyone who already has basic knowledge of Java and now wants to learn about Java EE 6.

Prerequisites

Basic knowledge of Java programming.

 

Transcript

Hello, dear friends. In this video, we will examine the observer design pattern. 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 is also referred to as the publish-subscribe pattern. In observer pattern, there are many observers that are observing a particular subject. Observers register themselves to a subject and get a notification when there is a change made inside that subject. An observer object can register or unregister from 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 dependence as observer in such a way that whenever state of subject changes, its dependence 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 detaching 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 dependence 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 system. In such systems, the subject is usually called a source of events while the observers are called sync of events. 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 pattern.

These have been available since Java 1; but in Java 9, these were called deprecated and obsolete because observer and observable only support a limited event model. Observable doesn't specify the order in which notifications are sent, and state changes don't always match up with notifications. But in this course, we are working on Java 6, so this will not be a problem for us. For reliable and ordered messaging among threads, you can use one of the concurrent data structures in the java.util.concurrent package. Observable classes are used to create subclasses. The other parts of the program can observe. The observer pattern is 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 decimal 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. Done. Let's add our subject class. Define an observer list. We have to import List libraries. Also, we need state. Generate getter and setter of state. Now, we need two methods. First one is attach. Now to communicate, we need notify method. In set state, we have to call 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 Update method, we can use toBinaryString method of integer class. Done. Let's add Octal Observer. If you want, you can copy from binary. Only we will change toBinaryString with toOctalString. Let's add Hexadecimal toHexString.

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. Done. Let's run and look at the result. We can see the binary, octal, and hexadecimal equivalents of 12 and 14. With observer, we can update all values of numbers. We have now finished the observer design pattern. In the next video, we will examine the strategy design pattern. See you in the next video.

 

About the Author
Students
413
Courses
35
Learning Paths
2

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.

Covered Topics