5.1.1 Design Pattern and Observer Pattern - FZN
5.1.1 Design Pattern and Observer Pattern - FZN
In 1994, four authors Erich Gamma, Richard Helm, Ralph Johnson and John
Vlissides published a book titled Design Patterns - Elements of Reusable
Object-Oriented Software which initiated the concept of Design Pattern in
Software development.
These authors are collectively known as Gang of Four (GOF). According to these
authors design patterns are primarily based on the following principles of object
orientated design.
● Program to an interface not an implementation
● Favor object composition over inheritance
Usage of Design Pattern
Best Practices
Design patterns have been evolved over a long period of time and they provide best solutions to
certain problems faced during software development. Learning these patterns helps inexperienced
developers to learn software design in an easy and faster way.
Types of Design Patterns
Creational Patterns
These design patterns provide a way to create objects while hiding the creation logic, rather than instantiating objects
directly using new operator. This gives program more flexibility in deciding which objects need to be created for a given
use case.
Structural Patterns
These design patterns concern class and object composition. Concept of inheritance is used to compose interfaces and
define ways to compose objects to obtain new functionalities.
Behavioral Patterns
These design patterns are specifically concerned with communication between objects.
J2EE Patterns
These design patterns are specifically concerned with the presentation tier. These patterns are identified by Sun Java
Center.
Observer Pattern
Intent: Define a one-to-many dependency between objects so that when one object
change state, all its dependents are notified and updated automatically.
Student-Teacher Example
Class Teacher:
def __init__(self,name): teacher1 = Teacher(“aaa”)
student1= Student(“bbb”)
self.students=[]
self.name=name
student1.add_teacher(teacher1 )
def attach_student(self, student):
self.students.append(student)
print(f"Student {student} added to teacher's list.")
Class Student:
def__init__(self,name): Output:
self.teachers=[] Teacher aaa added to student's list.
def add_teachers(self,teacher): Student bbb added to teacher's list.
self.teachers.append(teacher)
print(f"Teacher {teacher} added to student's list.")
teacher.attach_student(self)
Fan-Celebrity Example
# Subject (Celebrity) # Observer (Fan)
celebrity = Celebrity()
class Celebrity: class Fan:
fan = Fan()
def __init__(self): def __init__(self):
#fan starts following…
self._fans = [] self._celebrities = []
fan.add_celebrity(celebrity)
def update(self, celebrity):
self._state = None #celeb changes status and
state = celebrity.get_state()
def attach(self, fan): #notification is received by fan.
def add_celebrity(self, celebrity):
self._fans.append(fan) celebrity.set_state('New state')
self._celebrities.append(celebrity)
def detach(self, fan): #fan can stop following…
celebrity.attach(self)
fan.remove_celebrity(celebrity)
self._fans.remove(fan) def remove_celebrity(self, celebrity):
def _notify(self): self._celebrities.remove(celebrity)
for fan in self._fans: celebrity.detach(self)
fan.update(self)
def set_state(self, state):
self._state = state
self._notify()
def get_state(self):
return self._state
Observer Pattern 13
}
Observer Pattern 14
}
Observer Pattern 15
f1.addCelebrity(c1);
f1.addCelebrity(c2);
f2.addCelebrity (c1);