0
0
Software Engineeringknowledge~30 mins

Structural patterns (Adapter, Decorator, Facade) in Software Engineering - Mini Project: Build & Apply

Choose your learning style9 modes available
Understanding Structural Patterns: Adapter, Decorator, Facade
📖 Scenario: Imagine you are building a simple software system that manages different types of devices and their features. You want to organize your code so it is easy to extend and maintain. To do this, you will learn three common structural design patterns: Adapter, Decorator, and Facade.
🎯 Goal: Build a small example that shows how Adapter, Decorator, and Facade patterns work. You will create basic classes and then apply each pattern step-by-step to see how they help organize and improve the code.
📋 What You'll Learn
Create a basic device interface and two device classes
Add an adapter class to make an incompatible device work with the interface
Create a decorator class to add extra features to a device
Build a facade class to simplify interaction with multiple devices
💡 Why This Matters
🌍 Real World
Structural patterns help organize code in software projects where different components need to work together smoothly, especially when integrating legacy systems or adding new features.
💼 Career
Understanding these patterns is valuable for software developers and architects to write flexible, maintainable, and scalable code in real-world applications.
Progress0 / 4 steps
1
Create basic device classes
Create a class called Device with a method get_status() that returns the string 'Device status: OK'. Then create two classes Printer and Scanner that inherit from Device and override get_status() to return 'Printer ready' and 'Scanner ready' respectively.
Software Engineering
Hint

Define the base class Device with a method that returns a status string. Then create two subclasses that change this status message.

2
Add an Adapter for an incompatible device
Create a class called OldCamera with a method status() that returns 'Old camera active'. Then create an adapter class called CameraAdapter that has a constructor accepting an OldCamera instance and a method get_status() that calls the status() method of OldCamera and returns its result.
Software Engineering
Hint

The adapter wraps the old device and translates its method to the expected interface method.

3
Create a Decorator to add features
Create a class called DeviceDecorator with a constructor that takes a Device instance and stores it. Add a method get_status() that calls the wrapped device's get_status() and appends the string ' with extra features' to the result.
Software Engineering
Hint

The decorator holds a device and extends its behavior by adding text to the status.

4
Build a Facade to simplify device control
Create a class called DeviceFacade with a constructor that creates instances of Printer, Scanner, and CameraAdapter (using an OldCamera instance). Add a method all_statuses() that returns a list of status strings from all three devices by calling their get_status() methods.
Software Engineering
Hint

The facade creates and holds all devices and provides a simple method to get their statuses together.