Nothing Special   »   [go: up one dir, main page]

Structural and Behavioral Design Patterns

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

CEN 308 - SOFTWARE ENGINEERING

Lab 5: Structural and Behavioral Design Patterns


April 7th, 2022

05. Structural and Behavioral Design Patterns

Structural Design Patterns


Structural patterns explain how to assemble objects and classes into larger structures while
keeping these structures flexible and efficient. The most commonly used structural patterns are:
● adapter
● bridge
● composite
● decorator
● facade
● flyweight
● proxy
● …

Adapter
Adapter is a structural design pattern that allows objects with incompatible interfaces to
collaborate. It converts the interface of a class into another interface that the clients expect. The
adapter pattern lets classes, which initially could not work together, cooperate. It works as a bridge
between two incompatible interfaces.

The adapter pattern Involves a single class which is responsible for joining the functionalities of
independent or incompatible interfaces.

Example implementation:
https://github.com/Aldin-SXR/creational-design-patterns/tree/master/structural/adapter

1
CEN 308 - SOFTWARE ENGINEERING
Lab 5: Structural and Behavioral Design Patterns
April 7th, 2022

Composite
Composite is a structural design pattern that lets you compose objects into tree structures and
then work with these structures as if they were individual objects. This pattern composes objects
into tree structures to represent part-whole hierarchies. Composite pattern is used when we need
to treat a group of objects in a similar way to a single object.

This pattern creates a class that contains a group of its own objects. This class then provides ways
to modify its group of same objects.

Example implementation:
https://github.com/Aldin-SXR/creational-design-patterns/tree/master/structural/composite

2
CEN 308 - SOFTWARE ENGINEERING
Lab 5: Structural and Behavioral Design Patterns
April 7th, 2022

Decorator
Decorator is a structural design pattern that lets you attach new behaviors to objects by placing
these objects inside special wrapper objects that contain the behaviors. The decorator attaches
additional responsibilities to an object dynamically. It allows dynamic and transparent addition and
removal of responsibilities without affecting the object.

The decorator pattern allows a user to add new functionality to an existing object without altering
its structure. This pattern creates a decorator class which wraps the original class and provides
additional functionality keeping class methods signature intact.

Example implementation:
https://github.com/Aldin-SXR/creational-design-patterns/tree/master/structural/decorator

3
CEN 308 - SOFTWARE ENGINEERING
Lab 5: Structural and Behavioral Design Patterns
April 7th, 2022

Facade
Facade is a structural design pattern that provides a simplified interface to a library, a framework,
or any other complex set of classes. This pattern provides a unified interface to a set of interfaces
in a subsystem. Like one side of the exterior of a building, it hides the complexities of a system
(hence the name “facade”).

This pattern involves a single class which provides simplified methods required by the client and
delegates calls to methods of existing system classes.

Example implementation:
https://github.com/Aldin-SXR/creational-design-patterns/tree/master/structural/facade

4
CEN 308 - SOFTWARE ENGINEERING
Lab 5: Structural and Behavioral Design Patterns
April 7th, 2022

Behavioral Design Patterns


Behavioral design patterns are concerned with algorithms and the assignment of responsibilities
between objects. The most commonly used behavioral patterns are:
● chain of responsibility
● command
● iterator
● mediator
● memento
● observer
● state
● strategy
● template method
● visitor
● ...

Command
Command is a behavioral design pattern that turns a request into a stand-alone object that
contains all information about the request. This transformation lets you pass requests as a method
argument, delay or queue the request's execution, and support undoable operations. The
command pattern encapsulates a request as an object, letting you parameterize clients with
different requests, queue or long requests

5
CEN 308 - SOFTWARE ENGINEERING
Lab 5: Structural and Behavioral Design Patterns
April 7th, 2022

A request is wrapped under an object as command and passed to the invoker object. The invoker
object then looks for the appropriate object which can handle this command and passes the
command to the corresponding object which executes the command. If all commands can service
a specific request, they can be called by the invoker.

Example implementation:
https://github.com/Aldin-SXR/creational-design-patterns/tree/master/behavioral/command

Strategy
Strategy is a behavioral design pattern that lets you define a family of algorithms, put each of them
into a separate class, and make their objects interchangeable. This pattern defines strategies for
performing a behavior and changing them dynamically as per requirement.

With the strategy pattern, a class behavior or its algorithm can be changed at run time. We create
objects which represent various strategies and a context object whose behavior varies per its
strategy object. The strategy object changes the executing algorithm of the context object.

Example implementation:
https://github.com/Aldin-SXR/creational-design-patterns/tree/master/behavioral/strategy

6
CEN 308 - SOFTWARE ENGINEERING
Lab 5: Structural and Behavioral Design Patterns
April 7th, 2022

Template method
Template method is a behavioral design pattern that defines the skeleton of an algorithm in the
superclass but lets subclasses override specific steps of the algorithm without changing its
structure.

The main algorithm skeleton is defined in the base class and subclasses can then plug in their own
details without affecting the algorithm itself. Template method uses inheritance (abstract class) to
define the template, and concrete classes to manage the implementation. Template uses the
implementation on demand.

Example implementation:
https://github.com/Aldin-SXR/creational-design-patterns/tree/master/behavioral/template

7
CEN 308 - SOFTWARE ENGINEERING
Lab 5: Structural and Behavioral Design Patterns
April 7th, 2022

You might also like