Sunday 25 March 2007

Lecture 6: Decorator, Composite and Facade patterns

This week lecture was on decorator, composite and facade patterns. This was the last lecture before the Easter break as therewould be no lecture on the 26th March. We are met to use the time for the lecture to work on our portfolio.
Decorator
Head First Design Patterns book uses the example of a system used in a coffee shop. The coffee shop expands with time and additional functionalities are needed to be added to system such adding the price for additional condiments (soy, mocha). The decorator is a design pattern that allows additional behaviour/functionalities to be added to this coffee shop system. I think this a very important pattern because one thing that we can definately be sure of regarding software development is that user requirements always change with time. The head first book described the decorator pattern as a pattern that "attaches additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending fuctionality".
During the lecture we also discussed how decorators are open for extension of responsibilities but closed for modificiation (of existing code) to meet changing requirements. I think it is also worth it to note that inheritance, usually considered as a form of extension might not probably be the best way to achieve flexibility in designs. Decorators act as wrappers, wrapping a component with any numbers of decorators, increaing functionality.
Composite
The composite pattern is used a collection of objects with whole-part relationships(for instance, a text editor has several parts like scroll bars, buttons but it is generally considered as whole) exists and there is a need to treat these objects uniformly. The difference between the composition of objects and individual objects are deliberately ignored. Data structures came up as a good example of composite pattern being implemented. Appyling a structure such any of the various types binary search tree, we would have a structure of an object that contains both compositions of objects and individual objects as nodes.
I learnt that the difference between composite and decorator is not in the structure but it is in the implmentation as one can put one composite object into another. Visitor patterns could be applied to composite patterns to add new functionalities to objects as well as the iterator pattern search through composite objects.
Facade
Head First describe the Facade pattern as a pattern that "provides a unified interface to a set of interfaces in a subsystem.. Facade defines a higher level interface that makes the subsystem easier to use". This pattern is useful if you want to run parts of your system as a subsystem. The example used in the Head first book is a collection of classes for a home entertainment system. Facade passes request on to other classes such as the dvd player class without making changes to the other classes. A client could interact directly with the server classes, while an adapter is needed and should be present for two server classes to interact. I believe changes could be made to one of the server classes without the other classes being affected as the whole system is treated as a subsystem.
Facade pattern provides an interface to interact with the whole system. However, an advantage that Facade has over adapter pattern is that the interface of the facade pattern does not always have to be used.


LINKS

2 comments:

Anonymous said...

Hey, I am checking this blog using the phone and this appears to be kind of odd. Thought you'd wish to know. This is a great write-up nevertheless, did not mess that up.

- David

Anonymous said...

hi, good site very much appreciatted