0% found this document useful (0 votes)
5 views30 pages

Composite, Decorator, Facade

The document discusses three design patterns: Composite, Decorator, and Facade. The Composite Design Pattern allows clients to treat individual objects and compositions uniformly, while the Decorator Pattern enables dynamic addition of functionality to objects without altering their structure. The Facade Pattern simplifies interactions with complex subsystems by providing a unified interface, minimizing dependencies and communication between components.

Uploaded by

Jay Parmar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views30 pages

Composite, Decorator, Facade

The document discusses three design patterns: Composite, Decorator, and Facade. The Composite Design Pattern allows clients to treat individual objects and compositions uniformly, while the Decorator Pattern enables dynamic addition of functionality to objects without altering their structure. The Facade Pattern simplifies interactions with complex subsystems by providing a unified interface, minimizing dependencies and communication between components.

Uploaded by

Jay Parmar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Composite Design Pattern

• The Composite Design Pattern is a


structural design pattern that lets you
compose objects into tree-like structures
to represent part-whole hierarchies. It
allows clients to treat individual objects
and compositions of objects uniformly. In
other words, whether dealing with a
single object or a group of objects
(composite), clients can use them
interchangeably.
Composite Design Pattern
example
1. Task (Component)
• Represents the common interface for both simple tasks
and task lists.
• Defines methods such as getTitle(), setTitle(), and
display().
2. SimpleTask (Leaf)
• Represents an individual task with a title.
• Implements the Task interface.
3. TaskList (Composite)
• Represents a collection of tasks, which can include both
simple tasks and other task lists.
• Implements the Task interface but also has a list of tasks
(List<Task>).
• Defines methods to add, remove, and display tasks.
4. TaskManagementApp (Client)
• Represents the application that uses the Composite
Design Pattern to manage tasks.
• It creates a mix of simple tasks and task lists, showcasing
how the Composite pattern allows treating both individual
tasks and task collections uniformly.
• The created tasks are displayed in a hierarchical structure
to illustrate the pattern’s flexibility and uniform handling
of different task types.
Need of Composite Design
Pattern
• Uniform Interface
• Hierarchical Structures
• Flexibility and Scalability
• Common Operations
• Client Simplification
When to use Composite Design
Pattern?
• Composite Pattern should be used when clients need to
ignore the difference between compositions of objects and
individual objects.
• If programmers find that they are using multiple objects in
the same way, and often have nearly identical code to
handle each of them, then composite is a good choice, it is
less complex in this situation to treat primitives and
composites as homogeneous.
Decorator Method Design
• Decorator design pattern allows us to dynamically add
functionality and behavior to an object without affecting
the behavior of other existing objects within the same
class. We use inheritance to extend the behavior of the
class. This takes place at compile-time, and all the
instances of that class get the extended behavior.
• Decorator patterns allow a user to add new functionality to an existing object without
altering its structure. So, there is no change to the original class.
• The decorator design pattern is a structural pattern, which provides a wrapper to the
existing class.
• The decorator design pattern uses abstract classes or interfaces with the composition
to implement the wrapper.
• Decorator design patterns create decorator classes, which wrap the original class and
supply additional functionality by keeping the class methods’ signature unchanged.
• Decorator design patterns are most frequently used for applying single responsibility
principles since we divide the functionality into classes with unique areas of concern.
• The decorator design pattern is structurally almost like the chain of responsibility
pattern.
Procedure
• Create an interface.
• Create concrete classes implementing the same interface.
• Create an abstract decorator class implementing the above same
interface.
• Create a concrete decorator class extending the above abstract
decorator class.
• Now use the concrete decorator class created above to decorate
interface objects.
• Lastly, verify the output
• Step 1: Creating an interface named ‘Shape’
• Step 2: Create concrete classes implementing the same
interface. Rectangle.java and Circle.java are as follows.
• Step 3: Create an
abstract decorator
class implementing
the Shape interface.
• Step 4: Create a concrete
decorator class extending the
ShapeDecorator class.
• Step 5: Using the
RedShapeDecorator to
decorate Shape objects.
Facade Method Design Pattern
• It hides the complexity of the underlying system and
provides a simple interface that clients can use to interact
with the system.
• Facade Method Design Pattern provides a unified
interface to a set of interfaces in a subsystem. Facade
defines a high-level interface that makes the subsystem
easier to use.
• Structuring a system into subsystems helps reduce
complexity.
• A common design goal is to minimize the communication
and dependencies between subsystems.
• One way to achieve this goal is to introduce a Facade
object that provides a single simplified interface to the
more general facilities of a subsystem.
When to use Facade Method
Design Pattern
• When you want to provide simple interface to a complex
sub-system.
• When several dependencies exist between clients and the
implementation classes of an abstraction.
Example of Facade Pattern
• Step 5
• Create a ShopKeeper concrete
class that will use MobileShop
interface.
• Step 6
• Now, Creating a client that
can purchase the mobiles from
MobileShop through
ShopKeeper.

You might also like