0% found this document useful (0 votes)
4 views86 pages

Design Patterns v4 Amal-Structural-Patterns

The document discusses various software design patterns, focusing on structural patterns that enhance flexibility and maintainability in code. It details the Adaptor, Bridge, Flyweight, and Decorator patterns, explaining their purposes and implementations. Additionally, it highlights the use of annotations in Java to reduce boilerplate code, particularly through the Lombok library.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views86 pages

Design Patterns v4 Amal-Structural-Patterns

The document discusses various software design patterns, focusing on structural patterns that enhance flexibility and maintainability in code. It details the Adaptor, Bridge, Flyweight, and Decorator patterns, explaining their purposes and implementations. Additionally, it highlights the use of annotations in Java to reduce boilerplate code, particularly through the Lombok library.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 86

Tempus

Module Topics (Cont’d)

 Introduction to Design Patterns


 Creational Patterns
 Structural Patterns
 Behavioral Patterns
Software Design Patterns:
Structural Patterns
Structural Patterns Discussion

 Structural patterns deals with how inheritance and composition


can be used to provide extra functionality.
 Structural design patterns explain how to assemble objects and
classes into larger structures, while keeping these structures
flexible and efficient and easy to maintain.
 By using structural patterns, you can better manage complex
class hierarchies, reuse existing code, and create scalable
architectures.
 Structural Patterns in addition target flexible object composition
at runtime which is impossible with static class composition
Adaptor Pattern

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?

• Step 1: The client initiates a request by calling a method on the


adapter via the target interface.
• Step 2: The adapter maps or transforms the client’s request into a
format that the adaptee can understand using the adaptee’s interface.
• Step 3: The adaptee does the actual job based on the translated
request from the adapter.
• Step 4: The client receives the results of the call, remaining unaware
of the adapter’s presence or the specific details of the adaptee.
Bridge Patten!

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 (@)

• These annotations come from a popular library called Lombok, which is


used to reduce boilerplate code like getters, setters, constructors,
toString(), etc.
• @Data: is a shortcut annotation that combines several useful
annotations into one. It generates:
• Getters for all fields
• Setters for all non-final fields
• toString()
• equals() and hashCode()
• A constructor for all final fields
• @RequiredArgsConstructor
Annotations in Java (@Data)

This is equivalent to writing getters, setters, toString(), etc. manually.


Annotations in Java (@Getter)

This generates getter methods for all fields, or you can apply it
to individual fields.
Annotations in Java (@AllArgsConstructor)

Generates a constructor with one parameter for each field in


your class.

Generates:
Annotations in Java (@RequiredArgsConstructor)

This annotation generates a constructor with parameters for


all final fields and fields annotated with @NonNull

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

• The hierarchy will


grow largely, when
adding a new
notification method
• The number of
sub-classes we
have are hard to
keep track of
Inheritance has several series drawbacks:

• Inheritance is static. You can’t alter the behavior of an existing object at


runtime. You can only replace the whole object with another one that’s
created from a different subclass.
• Subclasses can have just one parent class. In most languages, inheritance
doesn’t let a class inherit behaviors of multiple classes at the same time.
• Solution: Use Aggregation or Composition instead of inheritance.
• With this new approach you can easily substitute the linked “helper” object with another,
changing the behavior of the container at runtime.
• An object can use the behavior of various classes, having references to multiple objects
and delegating them all kinds of work
Decorator Design Pattern: How it works?

• “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

• The rest of our code won’t care whether it


works with the initial notifier object or the
decorated one; Why?
• since all decorators implement the same
interface, as does the base notifier class
(INotifier Interface)
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

You might also like