Repository navigation
Structural Design Patterns
Short Description:
Decouple an Abstraction from its implementation so the two can vary independently
When is used:
- Shapes
- Drawing API
- Object Persistence
Short Description:
Adapts the one interface to feature interfaces or classes and are also knower as Wrappers
Related Patterns
- Repository very common to the Adapter pattern
- Strategy-if Adapter is passed by constructor to a class than that needs Strategy pattern
- Facade- Adapter and Facade are both wrappers however the Facade is attempting to simplify interfaces and often wraps more than one classes while Adapter is typically wraps a single Adaptee and is not concerned with interface simplifications;
Short Description
Compose objects to tree structures to represent part-whole hierarchies. in other words it's tree-like structure that is storing in the same time leaf and other branches. Also with this pattern you can easely go trough each tree leaf without a problem.
Where to use
- Groups or collections - because with this pattern oyu can have group and single items in the same time
- Trees
- Distirbution
Short Description
use to extend or alter the functionality in real time in other words it's primary goal is to wrap objects and extend their functionalities without harming their structure
It allows to: 1) add functionality dynamically 2) Alternative to Sub classing - with Decorator you can add the changes in single object and other inherit it 3) Flexible Design -> will provide flexible design that we allow us to extend it support Open Close Principle;
When to use: 1) Legacy Systems - to add new functionality without worrying of using the existing spaghetti code; 2) Add functionality to Control - it is better to use decorator instead of inheritance of base class; 3) Sealed Classes - decorate DP is helping to add functionallity to Sealed classes
main part of Decorator DP
- Component - it is base class for all component and base for the decorator, if we are speaking to animal classes then this should be the animal base;
- Concrete component - this is the object of current component implementation ;
- Decorator - is base class for all concrete decorators it's inheriting from base component so he can extend our concrete components, it'll store instance of component;
- Concrete Decorator - it's current implementation of decorator
Consequences 1) Original Object is unaware of any decorations- 2) There is no one big feature-land class with all the option in it 3) Decorations can be composed together in a mix and match fashion 4) Can increase complexity of code
##Facade Short Description
A single class that represents an entire subsystem Façade is Dp that can provide single Interface from which we can access entire subsystem or api,
Structure
façade is separated between
- Caller - façade that call the needed api methods;
- API methods - methods that needs to be called;
Know Uses
- Data Access - wrap the whole process instead of calling each methods;
- File and IO - there are many options that are available when we are streaming however the major classes are wrappers which provide simplicity
##Flyweight Short Description
this is pattern provides single object to multiple contexts A flyweight is an object that minimizes memory use by sharing as much data as possible with other similar objects; it is a way to use objects in large numbers when a simple repeated representation would use an unacceptable amount of memory. Often some parts of the object state can be shared, and it is common practice to hold them in external data structures and pass them to the flyweight objects temporarily when they are used.
Problems that solves:
- Reduce Storage costs for large number of objects
- Share object to be used in multiple contexts simultaneuusly
- Retain object oriented granualarity and flexability
##Proxy Short Description
You need to support resource-hungry objects, and you do not want to instantiate such objects unless and until they are actually requested by the client.
Problems that solves:
- Provide a surrogate or placeholder for another object to control access to it.
- Use an extra level of indirection to support distributed, controlled, or intelligent access.
- Add a wrapper and delegation to protect the real component from undue complexity.
Design a surrogate, or proxy, object that: instantiates the real object the first time the client makes a request of the proxy, remembers the identity of this real object, and forwards the instigating request to this real object. Then all subsequent requests are simply forwarded directly to the encapsulated real object.
There are four common situations in which the Proxy pattern is applicable.
A virtual proxy is a placeholder for "expensive to create" objects. The real object is only created when a client first requests/accesses the object.
A remote proxy provides a local representative for an object that resides in a different address space. This is what the "stub" code in RPC and CORBA provides.
A protective proxy controls access to a sensitive master object. The "surrogate" object checks that the caller has the access permissions required prior to forwarding the request.
A smart proxy interposes additional actions when an object is accessed. Typical uses include:
Counting the number of references to the real object so that it can be freed automatically when there are no more references (aka smart pointer),
Structure
By defining a Subject interface, the presence of the Proxy object standing in place of the RealSubject is transparent to the client.

- Adapter provides a different interface to its subject. Proxy provides the same interface. Decorator provides an enhanced interface.
- Decorator and Proxy have different purposes but similar structures. Both describe how to provide a level of indirection to another object, and the implementations keep a reference to the object to which they forward requests.