Presentation is loading. Please wait.

Presentation is loading. Please wait.

DECORATOR by Ramani Natarajan Also known as ‘Wrapper.’ Also known as ‘Wrapper.’ According to ‘gang of four’(sounds like an Akira Kurosawa movie): A Decorator.

Similar presentations


Presentation on theme: "DECORATOR by Ramani Natarajan Also known as ‘Wrapper.’ Also known as ‘Wrapper.’ According to ‘gang of four’(sounds like an Akira Kurosawa movie): A Decorator."— Presentation transcript:

1 DECORATOR by Ramani Natarajan Also known as ‘Wrapper.’ Also known as ‘Wrapper.’ According to ‘gang of four’(sounds like an Akira Kurosawa movie): A Decorator is used “to attach additional responsibilities to an object dynamically. A Decorator provides a flexible alternative to subclassing for extending functionality.” According to ‘gang of four’(sounds like an Akira Kurosawa movie): A Decorator is used “to attach additional responsibilities to an object dynamically. A Decorator provides a flexible alternative to subclassing for extending functionality.” It is a ‘Structural Object Pattern.’ It is a ‘Structural Object Pattern.’ Structural Class patterns like ‘Adapter’ are concerned with how classes and objects are composed using inheritance. They are static Structural Class patterns like ‘Adapter’ are concerned with how classes and objects are composed using inheritance. They are static

2 Decorator (contd.) compared to structural object patterns like Decorator that describe ways to compose objects to realize new functionality. Such a structural object pattern gives the ability to change compositions at run-time. Such a structural object pattern gives the ability to change compositions at run-time. It allows open-ended number of additional responsibilities, like adding borders, shadow, instill scrolling, zooming…….. It allows open-ended number of additional responsibilities, like adding borders, shadow, instill scrolling, zooming……..

3 What am I talking about? Consider the class interface for the universal set { x | x is a frog }: DEFINE CLASS Frog AS CUSTOM FUNCTION Jump(nValue) FUNCTION Eat( ) ENDDEFINE Suppose we want to make Kermit dance; we subclass as: DEFINE CLASS DancingFrog AS Frog FUNCTION Dance( ) ENDDEFINE

4 What am I talking about?(contd) Now suppose Kermit wants to sing and dance, we have several options: Now suppose Kermit wants to sing and dance, we have several options: We could augment DancingFrog with the Sing( ) method, perhaps renaming the class to EntertainingFrog, or We could augment DancingFrog with the Sing( ) method, perhaps renaming the class to EntertainingFrog, or We could subclass DancingFrog and add a Sing( ) method to the subclass DancingSingingFrog, or We could subclass DancingFrog and add a Sing( ) method to the subclass DancingSingingFrog, or We could make all Frogs sing by augmenting the original Frog class, or We could make all Frogs sing by augmenting the original Frog class, or (This is getting out of hand!!) we could simplify the exploding hierarchies by giving every Frog the Sing( ) and Dance( ) methods in a single, static, stiff, monolithic, gargantuan Frog class. (This is getting out of hand!!) we could simplify the exploding hierarchies by giving every Frog the Sing( ) and Dance( ) methods in a single, static, stiff, monolithic, gargantuan Frog class.

5 What am I talking about?(contd) Now what if Kermit wants to teach “Object Oriented Architecture, Design and Methodology?” Now what if Kermit wants to teach “Object Oriented Architecture, Design and Methodology?” ENTER DECORATOR ENTER DECORATOR Consider the abstract Decorator class: Consider the abstract Decorator class: DEFINE CLASS DecoFrog AS CUSTOM RealFrog = NULL FUNCTION INIT(Frog) THIS.RealFrog = Frog FUNCTION Jump(n) THIS.RealFrog.Jump(n) FUNCTION Eat( ) THIS.RealFrog.Eat( ) ENDDEFINE

6 What am I talking about?(contd) If we need a specialized one-off Frog, we could If we need a specialized one-off Frog, we could DEFINE CLASS DecoSingingFrog AS DecoFrog FUNCTION Sing( ) ? WAIT WINDOW “Que Sera Sera Whatever will be will have been Future’s not mine to be seen It’s not easy being Green”

7 What I am Saying Similarly we could substitute a singing and dancing Frog at run-time as: Similarly we could substitute a singing and dancing Frog at run-time as: Kermit = CREATE(Frog) Kermit = CREATE(“DecoSingingFrog”, Kermit) Kermit = CREATE(“DecoDancingFrog”, Kermit) Note: Decorators can be chained with functionality added or modified as needed; as done above. Voila! We now have Frog Kermit that is instantiated to sing and dance and we didn’t need to pollute the Frog class; in fact, we didn’t need the source to class Frog.

8 Advantages More Flexible than static class inheritance. More Flexible than static class inheritance. Responsibilities can be added and subtracted at run-time simply by attaching and detaching them Responsibilities can be added and subtracted at run-time simply by attaching and detaching them Easy to add functionality n number of times by simply attaching responsibilities n number of times. Easy to add functionality n number of times by simply attaching responsibilities n number of times. Avoids feature heavy classes high up in the hierarchy. We can define a simple class and add functionality as we go along. A microcosmic example of the philosophy of eXtreme Programming. Avoids feature heavy classes high up in the hierarchy. We can define a simple class and add functionality as we go along. A microcosmic example of the philosophy of eXtreme Programming. A class can be extended by simply knowing its interface, source is usually not required. A class can be extended by simply knowing its interface, source is usually not required. Depending upon the situation, we can use Decorators stoked on Wheaties, or Decorators munching on Rice Krispies, or any Decorator in-between to accomplish that particular task Depending upon the situation, we can use Decorators stoked on Wheaties, or Decorators munching on Rice Krispies, or any Decorator in-between to accomplish that particular task

9 Disadvantages A Decorator and its component aren’t identical. Relying on object identity will make the programmer plead Mea Culpa. A Decorator and its component aren’t identical. Relying on object identity will make the programmer plead Mea Culpa. Lots of little objects like the flesh-eating scarabs in the movie Mummy. Too many Decorator objects will make the program confusing and hard to debug. Lots of little objects like the flesh-eating scarabs in the movie Mummy. Too many Decorator objects will make the program confusing and hard to debug. It takes more time to instantiate an object if one also instantiates one or more Decorators, and once created, it takes slightly more time for messages to filter through layers of Decorators to reach the object being decorated. It takes more time to instantiate an object if one also instantiates one or more Decorators, and once created, it takes slightly more time for messages to filter through layers of Decorators to reach the object being decorated.

10 Conclusion Decorators change the class appearance unlike subclassing that changes the class internals. Decorators change the class appearance unlike subclassing that changes the class internals. Decorators have their interfaces identical to the object that it decorates. Decorators have their interfaces identical to the object that it decorates. A Decorator adds functionality either before or after the call to the object that the Decorator contains. A Decorator adds functionality either before or after the call to the object that the Decorator contains. Decorators can be nested to allow for an infinite amount of customization. Decorators can be nested to allow for an infinite amount of customization. Decorators are not solutions for every situation, but occasionally, just occasionally, they are the ticket. Decorators are not solutions for every situation, but occasionally, just occasionally, they are the ticket.


Download ppt "DECORATOR by Ramani Natarajan Also known as ‘Wrapper.’ Also known as ‘Wrapper.’ According to ‘gang of four’(sounds like an Akira Kurosawa movie): A Decorator."

Similar presentations


Ads by Google