KEMBAR78
GL TP3 - Structural Patterns | PDF | Proxy Server | Computer Programming
0% found this document useful (0 votes)
11 views8 pages

GL TP3 - Structural Patterns

The document provides an overview of structural design patterns in software engineering, specifically focusing on Adapter, Facade, Decorator, and Proxy patterns. Each pattern is explained with its components, functionality, and practical exercises to implement them. Additionally, it briefly mentions other patterns like Composite, Bridge, and Flyweight, along with a section on relationship types in class diagrams.

Uploaded by

benonihel8704
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)
11 views8 pages

GL TP3 - Structural Patterns

The document provides an overview of structural design patterns in software engineering, specifically focusing on Adapter, Facade, Decorator, and Proxy patterns. Each pattern is explained with its components, functionality, and practical exercises to implement them. Additionally, it briefly mentions other patterns like Composite, Bridge, and Flyweight, along with a section on relationship types in class diagrams.

Uploaded by

benonihel8704
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/ 8

USTHB ​ ​ ​ ​ ​ ​ ​ ​ ​ 2024/2025

Faculté d’Informatique​ ​ ​ ​ ​ ​ ​ Génie Logiciel


Département Informatique​ ​ ​ ​ ​ ​ ​ L2/ISIL

TP 3. Structural Patterns
explain how to assemble objects and classes into larger structures, while keeping these
structures flexible and efficient.

Adapter
Adapter is a structural design pattern that allows objects with incompatible interfaces to
collaborate.

1.​ The Client contains the existing business logic of the program.
2.​ The Client Interface defines a protocol that other classes must follow to collaborate
with the client code.
3.​ The Service is a useful class (usually third-party or legacy) that the client can’t use
directly due to an incompatible interface.
4.​ The Adapter works with both the client and the service: it implements the Client
Interface while wrapping the Service. It translates client calls into a format the
service understands.
5.​ The client code remains decoupled from the concrete adapter as long as it interacts
through the Client Interface. This allows new adapters to be introduced without
breaking existing client code, making it easier to handle service changes or
replacements.

Exercise
Follow the diagram to add a new feature that lets the app switch from miles to kilometers
whenever needed!

1
USTHB ​ ​ ​ ​ ​ ​ ​ ​ ​ 2024/2025
Faculté d’Informatique​ ​ ​ ​ ​ ​ ​ Génie Logiciel
Département Informatique​ ​ ​ ​ ​ ​ ​ L2/ISIL

Facade
Facade is a structural design pattern that provides a simplified interface to a library, a
framework, or any other complex set of classes.

1.​ The Facade simplifies access to specific parts of a complex subsystem, directing
client requests efficiently.
2.​ Additional Facades can be created to keep features organized and avoid
complexity.
3.​ The Complex Subsystem consists of many interdependent objects that require
careful initialization and data handling, but it doesn't know about the facade.
4.​ The Client interacts with the facade instead of dealing with subsystem details
directly.

Exercise
With a single press of the "Watch Movie" button, your home theater comes to life—the TV
turns on, the sound system adjusts the volume, the lights dim, and the DVD player
starts playing your chosen movie.

2
USTHB ​ ​ ​ ​ ​ ​ ​ ​ ​ 2024/2025
Faculté d’Informatique​ ​ ​ ​ ​ ​ ​ Génie Logiciel
Département Informatique​ ​ ​ ​ ​ ​ ​ L2/ISIL

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.

1.​ The Component defines a common interface for both wrapped objects and
wrappers.
2.​ The Concrete Component is the original object with basic behavior that decorators
can modify.
3.​ The Base Decorator holds a reference to a wrapped object and delegates
operations to it.
4.​ Concrete Decorators add new behaviors by overriding methods and executing
additional logic before or after calling the parent method.
5.​ The Client can apply multiple decorators as long as it interacts with objects through
the component interface.

Exercise
You will implement a Coffee Ordering System where customers can customize their coffee
with different add-ons (like milk and sugar) dynamically, without modifying the core coffee
class.

3
USTHB ​ ​ ​ ​ ​ ​ ​ ​ ​ 2024/2025
Faculté d’Informatique​ ​ ​ ​ ​ ​ ​ Génie Logiciel
Département Informatique​ ​ ​ ​ ​ ​ ​ L2/ISIL

1.​ Define the Coffee Interface


Create an interface Coffee that declares two methods:
●​ getDescription(): Returns the coffee's description.
●​ getCost(): Returns the total cost of the coffee.
2.​ Create the SimpleCoffee Class (Concrete Component)
Implement Coffee in a class called SimpleCoffee, which represents a basic coffee.
●​ getDescription() should return "Plain Coffee".
●​ getCost() should return the base price (5.0).
3.​ Create the CoffeeDecorator Class (Base Decorator)
●​ This class should implement the Coffee interface.
●​ It should have a protected field to store a Coffee object (the wrapped
coffee).
●​ The constructor should accept a Coffee object and store it.
●​ It should delegate getDescription() and getCost() to the wrapped coffee
(override the methods and return the according fields of the wrapped coffee).
4.​ Create Concrete Decorators (Milk and Sugar)
Each decorator class should extend CoffeeDecorator and override the methods:
Milk Class:

●​ Modify getDescription() to add ", Milk" to the coffee description.


●​ Modify getCost() to add an extra 1.5.
Sugar Class:

●​ Modify getDescription() to add ", Sugar" to the coffee description.


●​ Modify getCost() to add an extra 0.5.
5.​ Implement the Main Class for Testing
In the main method:
1.​ Create a SimpleCoffee object and print its description and cost.
2.​ Wrap the coffee in a Milk decorator and print the updated details.
3.​ Wrap the coffee in both Milk and Sugar decorators, then print the final order.

4
USTHB ​ ​ ​ ​ ​ ​ ​ ​ ​ 2024/2025
Faculté d’Informatique​ ​ ​ ​ ​ ​ ​ Génie Logiciel
Département Informatique​ ​ ​ ​ ​ ​ ​ L2/ISIL

Proxy
Proxy is a structural design pattern that lets you provide a substitute or placeholder for
another object. A proxy controls access to the original object, allowing you to perform
something either before or after the request gets through to the original object.

1.​ The Service Interface defines the contract that the service and proxy must follow.
2.​ The Service is a class that provides core business functionality.
3.​ The Proxy holds a reference to the service and adds extra processing (like logging,
caching, or access control) before forwarding requests. It often manages the
service’s lifecycle.
4.​ The Client interacts with both the service and proxy through the same interface,
allowing proxies to be used interchangeably with service objects.

Exercise
You will implement an Internet Access Control System using the Proxy Pattern. The goal
is to restrict access to certain websites while allowing unrestricted browsing for others.

1.​ Define the Internet Interface (Service Interface)


Create an interface Internet that declares a method:

●​ connectTo(String website): This method will allow connection to a website.


●​ Both the real service and proxy must implement this interface.

5
USTHB ​ ​ ​ ​ ​ ​ ​ ​ ​ 2024/2025
Faculté d’Informatique​ ​ ​ ​ ​ ​ ​ Génie Logiciel
Département Informatique​ ​ ​ ​ ​ ​ ​ L2/ISIL

2.​ Create the RealInternet Class (Service)


Implement the Internet interface in a class called RealInternet.
●​ The connectTo(String website) method should print a message confirming a
successful connection.
●​ This class represents the actual internet service without any restrictions.
3.​ Create the ProxyInternet Class (Proxy)
●​ Implement the Internet interface in ProxyInternet.
●​ Store a list of blocked websites (e.g., "blocked.com").
●​ Hold a reference to a RealInternet object.
●​ In the connectTo(String website) method:
○​ Check if the requested website is in the blocked list.
○​ If blocked, deny access.
○​ Otherwise, delegate the request to RealInternet.
4.​ Create the Main Class (Client Code)
●​ Create an instance of ProxyInternet.
●​ Try connecting to both allowed and blocked websites.
●​ Observe how the proxy filters requests before allowing access to the real
service.

Note: In this exercise, for classes that extend a superclass or implement an interface, be
sure to use the @Override annotation.

6
USTHB ​ ​ ​ ​ ​ ​ ​ ​ ​ 2024/2025
Faculté d’Informatique​ ​ ​ ​ ​ ​ ​ Génie Logiciel
Département Informatique​ ​ ​ ​ ​ ​ ​ L2/ISIL

Other Structural Patterns


●​ 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.
●​ Bridge is a structural design pattern that lets you split a large class or a set of closely
related classes into two separate hierarchies—abstraction and
implementation—which can be developed independently of each other.
●​ Flyweight is a structural design pattern that lets you fit more objects into the
available amount of RAM by sharing common parts of state between multiple objects
instead of keeping all of the data in each object.

You can explore all the patterns in detail through this link.

7
USTHB ​ ​ ​ ​ ​ ​ ​ ​ ​ 2024/2025
Faculté d’Informatique​ ​ ​ ​ ​ ​ ​ Génie Logiciel
Département Informatique​ ​ ​ ​ ​ ​ ​ L2/ISIL

Annexe

Relationship Type Class Diagram Equivalent Use Case Diagram Equivalent

Use (Dependency) One class temporarily depends on Similar to <<extend>>, where


another (e.g A Printer class uses one use case may call another
a Driver class temporarily.) under conditions

Aggregation (Weak Whole-part relationship where parts Somewhat like <<extend>>,


Has-a) can exist independently (e.g A where an extended use case is
Library has a Book, but a book optional
can exist outside the library.)

Composition Whole-part relationship where parts Similar to <<include>>, where


(Strong Has-a) cannot exist separately (e.g A Car an included use case must
has an Engine, and an engine always execute
cannot exist without a car.)

Classes diagram arrows and their meaning :

You might also like