Design Patterns v4 Amal-Structural-Patterns
Design Patterns v4 Amal-Structural-Patterns
Watch Video!
Adaptor Pattern
Adaptor Pattern
Adaptor Pattern
Adaptor Pattern
Adaptor Pattern
Adaptor Pattern
In Java, the final keyword is
used to indicate the variable,
method or class cannot be
modified or extended
Adaptor Pattern
Adaptor Pattern
MultiRestoApp& IMultiRestoApp
FancyUIService
FancyUIServiceAdaptor
Adaptor Pattern
Adaptor Pattern
Adaptor Pattern
How Adaptor Pattern works?
Watch Video!
Bridge Patten!
• Extend the
pizza offering
by Italian and
American:
• VeggieItalian,
ViggieAmerican,
PepperoniItalian
, Pepperoni
American?
Bridge Patten!
Bridge Patten: Problem!
Bridge Patten!
Bridge Patten!
Bridge Patten!
Bridge Patten!
Bridge Patten!
Bridge Patten!
Bridge Patten!
Bridge Patten!
Bridge Patten!
Bridge Patten!
Bridge Patten!
Bridge Patten!
Flyweight Design Pattern
Watch Video!
Flyweight Design Pattern
Flyweight Design Pattern
Flyweight Design Pattern
Flyweight Design Pattern
Flyweight Design Pattern
Flyweight Design Pattern
Flyweight Design Pattern
Flyweight Design Pattern
Flyweight Design Pattern
Flyweight Design Pattern
Flyweight Design Pattern
Flyweight Design Pattern
Flyweight Design Pattern
Flyweight Design Pattern
Flyweight Design Pattern
Flyweight Design Pattern
Flyweight Design Pattern
Flyweight Design Pattern
Flyweight Design Pattern
Flyweight Design Pattern
Flyweight Design Pattern
Annotations in Java (@)
This generates getter methods for all fields, or you can apply it
to individual fields.
Annotations in Java (@AllArgsConstructor)
Generates:
Annotations in Java (@RequiredArgsConstructor)
Generates:
Decorator Design Pattern
Watch Video!
Decorator Design Pattern
• Decorator is a
structural design
pattern that lets you
attach new behaviors
to an object by placing
this object inside
special wrapper object
that contains
the behaviors.
https://refactoring.guru/design-patterns/decorator
Decorator Design Pattern
Decorator Design Pattern
• What is the
problem?
• Other ways of
notifications?
Decorator Design Pattern
Decorator Design Pattern
• What is the
problem with this
design
(Inheritance)?
Decorator Design Pattern
Decorator Design Pattern
• “Wrapper” is the alternative nickname for the Decorator pattern that clearly
expresses the main idea of the pattern.
• A wrapper is an object that can be linked with some target object.
• The wrapper contains the same set of methods as the target and delegates to
it all requests it receives.
• the "target" is generally referred to as the component interface or abstract
class that both the concrete component and decorators implement or
extend.
• Makes the wrapper’s reference field accept any object that follows that
interface.
• This will let you cover an object in multiple wrappers, adding the combined
behavior of all the wrappers to it.
Decorator Design Pattern
Decorator Design Pattern
Decorator Design Pattern
• INotifier interface
will be used by
our classes across
all applications
and by the
wrapper class
(BaseNotifierDecor
ator)
Decorator Design Pattern
• Decorators are wrappers
for our core object
• Wrapping enforces us to
replace Inheritance with
composition
• Its advantage is that you
can substitute an object
with one another (all of
them belong to the same
interface)
• Allows you to change the
behavior of the container
at runtime
Decorator Design Pattern
• On top of that, an object
can use the behaviour of
various classes at
runtime, not only one, as
it can reference multiple
and can delegate them
Decorator Design Pattern
• FacebookDecorator
super.send(msg) call, will
invoke the
WhatsAppDecorator
super.send(msg), which will
invoke the base Notifier
send(msg) call
Decorator Design Pattern
Decorator Design Pattern
Decorator Design Pattern
Decorator Design Pattern
Decorator Design Pattern
Decorator Design Pattern
Decorator Design Pattern: Participants and Collaborations
1. The Component declares the common interface for both wrappers and wrapped objects.
2. Concrete Component is a class of objects being wrapped. It defines the basic behavior, which
can be altered by decorators.
3. The Base Decorator class has a field for referencing a wrapped object. The field’s type should
be declared as the component interface so it can contain both concrete components and
decorators. The base decorator delegates all operations to the wrapped object.
4. Concrete Decorators define extra behaviors that can be added to components dynamically.
Concrete decorators override methods of the base decorator and execute their behavior either
before or after calling the parent method.
5. The Client can wrap components in multiple layers of decorators, as long as it works with all
objects via the component interface.
Decorator Design Pattern