Week 6 Structural Design Patterns PDF

Summary

This document provides a comprehensive overview of structural design patterns in software development. It covers various patterns like Adapter, Bridge, Composite, Decorator, Facade, and Flyweight, explaining their purpose and use cases.

Full Transcript

Structural Design Patterns XBSE2034 Software Design Recap What is the Behavioral Patter? Recap What are some examples of the Behavioral Pattern? Structural Design Patterns Structural patterns explain how to assemble objects and classes into larger structures while keeping...

Structural Design Patterns XBSE2034 Software Design Recap What is the Behavioral Patter? Recap What are some examples of the Behavioral Pattern? Structural Design Patterns Structural patterns explain how to assemble objects and classes into larger structures while keeping these structures flexible and efficient. Adapter Adapter Adapter Adapter Adapter So, when should you use the Adapter pattern?  class when you want to use some existing class, but its interface isn’t compatible with the rest of your code.  pattern when you want to reuse several existing subclasses that lack some common functionality that can’t be added to the superclass. Adapter Example Adapter- similar to how an electrical adapter lets your laptop work both in the US or UK even though voltages are different The class diagram consists of the following entities Target Client Adaptee Adapter Adapter Adapter Adapter Bridge Bridge Bridge Bridge Bridge Bridge Bridge So, when should you use the Bridge pattern?  when you want to divide and organize a monolithic class that has several variants of some functionality (for example, if the class can work with various database servers).  when you need to extend a class in several orthogonal (independent) dimensions.  if you need to be able to switch implementations at runtime. Bridge Examples Bridge- how parallel class hierarchies or layers can be decoupled from one another using the bridge pattern. The class diagram consists of the following entities Abstraction Refined Abstraction Implementor Concrete Implementor Bridge Bridge Bridge Bridge Bridge Bridge Bridge Composite Composite Composite Composite Composite So, when should you use the Composite pattern?  when your collection has a complex data structure under the hood, but you want to hide its complexity from clients (either for convenience or security reasons).to reduce duplication of the traversal code across your app.  when your collection has a complex data structure under the hood, but you want to hide its complexity from clients (either for convenience or security reasons).  when you want your code to be able to traverse different data structures or when types of these structures are unknown beforehand. Composite Composite - lets us treat individual elements and group of elements as one. The class diagram consists of the following entities Component Leaf Composite Client Composite Composite Composite Composite Composite Decorator Decorator Decorator Decorator Decorator Decorator Decorator Decorator Decorator So, when should you use the Decorator pattern?  when you need to be able to assign extra behaviors to objects at runtime without breaking the code that uses these objects.  when it’s awkward or not possible to extend an object’s behavior using inheritance.  Example 1 – Christmas Tree decoration  Example 2 – Assault gun Decorator Decorator- how objects can be enhanced with new behavior using the decorator pattern. The class diagram consists of the following entities Component Concrete Component Decorator Concerete Decorator Decorator Decorator call our decorator BoeingDecorator and have it implement the IAircraft interface Decorator two concrete decorators, one for the luxury fittings and the other for bullet proofing the plane Decorator The client can use the decorators like so Decorator Other Examples Facade Facade Imagine that you must make your code work with a broad set of objects that belong to a sophisticated library or framework. Facade A facade is a class that provides a simple interface to a complex subsystem which contains lots of moving parts. A facade might provide limited functionality in comparison to working with the subsystem directly. However, it includes only those features that clients really care about. Facade So, when should you use the Facade pattern?  when you need to have a limited but straightforward interface to a complex subsystem  when you want to structure a subsystem into layers. Facade Façade - how the interface to a complex system of interacting entities can be simplified by providing a front that hides the subsystem intricacies from the client. The class diagram consists of the following entities Facade Subsystem Classes Facade Flyweight Flyweight Flyweight Flyweight Flyweight Flyweight So, when should you use the Flyweight pattern? when your program must support a huge number of objects which barely fit into available RAM. Flyweight Example Flyweight- how the flyweight pattern can be applied to reduce memory requirements The class diagram consists of the following entities Flyweight Concrete Flyweight Unshared Concrete Flyweight Flyweight Factory Client Flyweight Flyweight The client code can take advantage of the flyweight like so Proxy Proxy Proxy Proxy Proxy So, when should you use the Proxy pattern?  when you have a heavyweight service object that wastes system resources by being always up, even though you only need it from time to time.  when you want only specific clients to be able to use the service object; for instance, when your objects are crucial parts of an operating system and clients are various launched applications (including malicious ones)  when the service object is located on a remote server.  when you want to keep a history of requests to the service object.  when you need to cache results of client requests and manage the life cycle of this cache, especially if results are quite large.  when you need to be able to dismiss a heavyweight object once there are no clients that use it. Proxy Example Proxy-how objects can act on behalf of other objects without the clients knowing they aren't talking to the intended object. The class diagram consists of the following entities Proxy Subject Real Subject Proxy The simplistic drone interface can be: Proxy The code for the DroneProxy Proxy The object of class Drone which will be part of the code that runs on the hardware of the flying drone and controls its flight may look like below The client code will be like so:

Use Quizgecko on...
Browser
Browser