Structural Pattern
Bachelor of Technical-Vocational Teacher Education
Major in Computer Programming
Object Oriented Programming
Object-Oriented Programming (OOP) in Python
• Structural Design Patterns are a category of design patterns that focus on how
classes and objects are organized to form larger structures. They help in
simplifying the design by identifying a way to realize relationships between
entities and making software more flexible and maintainable. Essentially, they
address how objects and classes are composed to create more complex
systems.
Bachelor of Technical-Vocational Teacher Education
Major in Computer Programming
Object Oriented Programming
Structural Patterns
• Adapter
• Decorator
• Façade
Bachelor of Technical-Vocational Teacher Education
Major in Computer Programming
Adapter
• One structural design pattern
that enables the usage of an
existing class’s interface as an
additional interface is the adapter
design pattern. To make two
incompatible interfaces function
together, it serves as a bridge.
This pattern involves a single
class, the adapter, responsible for
joining functionalities of
independent or incompatible
interfaces.
Object Oriented Programming
Structural Patterns
Real-life example:
• You have a laptop charger plug for the U.S. (flat pins).
• But you are traveling to Europe (they use round pins).
• You need an adapter to connect!
Bachelor of Technical-Vocational Teacher Education
Major in Computer Programming
Object Oriented Programming
Structural Patterns
Step 2: When to Use Adapter Pattern?
Use an Adapter when:
• You already have a working class.
• You want to reuse it without modifying its code.
• But it doesn't match the expected method names.
3: Know the Parts of Adapter Pattern
• Target Interface What the client (new code) expects.
• Adaptee Existing class that needs adapting0
• Adapter Middleman that converts one to the other.
Bachelor of Technical-Vocational Teacher Education
Major in Computer Programming
Object Oriented Programming
Example
We have an OldPrinter [Link] systems expect a method called
print_text(), but OldPrinter only has old_print().
Bachelor of Technical-Vocational Teacher Education
Major in Computer Programming
Object Oriented Programming
Example
Bachelor of Technical-Vocational Teacher Education
Major in Computer Programming
Singleton Pattern
Without Adapter With Adapter
Must directly call old_print(). Can call print_text() as expected.
Client must know about old method Client doesn’t care about old method
names. names.
Hard to integrate old classes into new
Easy and clean integration.
code.
Risky: You might have to modify the
Safe: No need to touch old class.
old class.
Less flexible for future changes. More flexible and maintainable.
Object Oriented Programming
Example
Bachelor of Technical-Vocational Teacher Education
Major in Computer Programming
Object Oriented Programming
Example
Bachelor of Technical-Vocational Teacher Education
Major in Computer Programming
Decorator
• In Python, a decorator is a design pattern that enhances or
modifies the functionality of functions or methods without
altering their core implementation. It is implemented using the
@ symbol followed by the decorator function name, placed
above the function or method definition to be decorated.
Decorator
Real-life example:
• You have a plain donut.
• You want to add chocolate or sprinkles on top without baking a
whole new donut!
• You just decorate the donut!
When to Use the Decorator Pattern?
Use Decorator Pattern when:
• You want to add new behavior to objects.
• You don't want to modify the original class.
• You want flexibility to add features dynamically.
Decorator
Know the Parts of the Decorator Pattern
Component Original object.
Decorator Adds new behavior around the component.
Basic Example Without Decorator
Basic Example With Decorator
Decorator
Coffee --> MilkDecorator --> WhipDecorator
Each decorator wraps the previous one.
Each adds something new without changing the original.
Decorator
Important Points
• Decorators are objects too!
• You can stack many decorators on top of each other.
• You don't modify the original class.
• You add features dynamically as needed.
•Decorators are objects too!
•You can stack many decorators on top of each other.
•You don't modify the original class.
•You add features dynamically as needed.
Facade
• In object-oriented programming, the Facade pattern provides a
simplified interface to a complex subsystem. It acts as a single
entry point, hiding the underlying complexity and making the
subsystem easier to use. This structural design pattern is
useful when dealing with large and intricate systems, offering a
higher-level interface that simplifies interactions for the client
Facade
• Sometimes a system is very complicated —There are many
classes, many methods, and it's hard for the user to know
what to call.
• Instead of making the user deal with all the details,
• You create a Facade — a simple class that hides the
complexity.
Facade
Real-life example:
• You want to watch a movie at home.
• You don't want to manually:
• Turn on TV
• Turn on sound system
• Set HDMI
• Press Play
• You just want to press one button: "Watch Movie" — that's the
Facade!
Facade
When to Use the Facade Pattern?
Use Facade Pattern when:
• You want to simplify a complicated system.
• You want to provide a clean and easy interface.
• You want to hide internal details from the user.
Part Role
Complicated classes/methods behind
Subsystems
the scenes.
Simplified interface that talks to
Facade
subsystems.
Client User who calls only the Facade.
Facade
The client has to know everything
about how CPU, Memory, HardDrive
[Link]'s complicated and error-prone.
Facade
Facade
Client --> Facade --> (CPU + Memory + HardDrive)
The client only talks to Facade.
The Facade talks to the complicated classes behind the scenes.
Facade Pattern Summary
Purpose Simplify complex subsystems.
Key Idea Provide a simple interface.
Benefit Easier for users, hides complexity.
Facade
Without Facade With Facade
Client needs to know everything. Client needs to know almost nothing.
High chance of errors. Lower chance of errors.
Hard to maintain. Easy to maintain.
Object Oriented Programming
Conclusion
Structural design patterns focus on how classes and objects are
organized to form larger structures, making systems easier to
manage, extend, and maintain. By carefully arranging
relationships between components, structural patterns promote
flexibility, efficiency, and clean architecture. They help developers
build systems that are not only powerful but also easy to
understand and adapt to future changes.
Bachelor of Technical-Vocational Teacher Education
Major in Computer Programming
1. This pattern allows two incompatible interfaces to work together by converting one interface
into another.
2. This pattern adds new behaviors or responsibilities to an object dynamically without altering
its structure.
3. This pattern provides a simplified interface to a larger body of code, like a library or a complex
subsystem.
4. A system has many subsystems, and you introduce one class to coordinate them and make
them easier to use.
5. You want to make an old class work with a new system, so you create a wrapper class that
translates method calls.
6. You have an object but you want to enhance its features (like adding scrolling to a window)
without changing its base code.
7. This pattern is often compared to a "translator" because it helps two classes with different
languages communicate.
8. In this pattern, you can stack multiple objects to add more and more features dynamically at
runtime.
9. You create a high-level interface that hides the complexity of the system and makes it easy for
clients to interact with.
10. When you use this pattern, you wrap an object inside another object that adds or overrides
behavior without modifying the original object.