Structural Patterns

Structural patterns are concerned with how classes and objects are composed to form larger structures.Structural class patterns use inheritance to compose interfaces or implementations. As a simple example, consider how multiple inheritance mixes two or more classes into one. structural object patterns describe ways to compose objects to realize new functionality.

There are 7 type of Structural Patterns

  • Adapter pattern
  • Bridge pattern
  • Composition pattern
  • Decorator pattern
  • Facade pattern
  • Flyweight pattern
  • Proxy pattern

Adapter Pattern

Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces. Adapter pattern use the Adapter class to implement all the methods of an interface, because if a class want to implement only some methods of interface rather all the methods, the adapter pattern best suit here in this situation.

Example

View Source Code

Bridge Pattern

In this pattern, we separate an abstraction and its implementation and develop separate inheritance structures for both the abstraction and the implementor. Doing this gives the flexibility so that both can vary independently. The abstraction is an interface or abstract class.

Example lets take Animal as a interface which has two methods, Animal interface is implements by Dog and Cat class.d

View Source Code


Here, we can see, that the interface Animal can be implemented in different ways. Here, we can easily use Animal as an interface as it has only two functions, dailyMeal() and makeSound(). But, there may arise a case where some other function be added to it. In this case, the interface will change and so, the implementations will also changed, for such cases, you should use the Animal as abstract class. This decision should be made earlier to implementation whether the interface should be interface or abstract class.

Composite Pattern

In this pattern object are compose into tree structure to represent part-whole hierarchies. Composition pattern is used when you want clients to be able to ignore the difference between compositions of objects and individual objects. Clients will treat all objects in the composite structure uniformly.

Example

View Source Code

Decorator Pattern

Decorator pattern or wrapper is use to add responsibility or behavior to an object. As inheritance adds functionality to classes, the decorator pattern adds functionality to objects by wrapping objects in other objects.

Example suppose we have shape as interface and Oval,Square as class which implements Shape interface and RedColorDecorator class add new funtionality to shapes.

View Source Code

Facade Pattern

Facade pattern defines a higher-level interface that makes the subsystem easier to use. A facade classes is used to provide a single interface to set of classes. Facade class can be define as friendly face which hides all the complexities of the system.

Example classes Add, Multiply, Divide and Subtract implements Arthmetic interface and AllCalculation class hide all the complex work and work as Facade class.

View Source Code

Flyweight Pattern

In this pattern, instead of creating large numbers of similar objects, objects are reused. A flyweight is a shared object that can be used in multiple contexts simultaneously. It reduce related cost. A Concrete Flyweight class implements the Flyweight interface which is used to perform operations based on external state and it also stores common state. A Flyweight Factory is used create and return Flyweight objects.

Example

View Source Code

Proxy Pattern

If creation of object is expensive, its creation can be postponed till the very need arises and till then, a simple object can represent it. Like instead of sending image we send a URL link, as it save time and relative cost of transferring image.

Example

View Source Code