Computer Software Talk
Computer Software Talk
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

feedback on Java componentized Decorator Pattern

 
Post new topic   Reply to topic    Computer Software Talk Forum Index -> Computer Software Talk
View previous topic :: View next topic  
Author Message
Giovanni Azua
Guest





PostPosted: Sun Apr 13, 2008 5:08 pm    Post subject: feedback on Java componentized Decorator Pattern Reply with quote

hi,

I was working on providing a componentized reusable implementation of the
Decorator pattern and came out with something that works as illustrated in
the example below, for easier reference I used the same example as GoF:

http://perfectjpattern.svn.sourceforge.net/viewvc/perfectjpattern/trunk/perfectjpattern-examples/src/main/java/perfectjpattern/core/structural/decorator/Example.java?view=markup

My question is => how inconvenient is to have a decorator that attaches
additional methods and that does not have the decorated type directly
implemented/accessible but under one level of indirection e.g.

IVisualComponent
(+focus)
(+hide)
^ ^
| |
| |
TextView |
|
-----------------------------
| |
ScrollDecorator |
(+scrollTo) |
BorderDecorator

The common usage for ScrollDecorator would be:
*******************************************************
ScrollDecorator myDecorator = new ScrollDecorator(new TextView());

// calls decorator's specific additional method
myDecorator.scrollTo();

// calls decorated component's interface
myDecorator.focus();
myDecorator.hide();
******************************************************

However in perfectjpattern's current componentized implementation the way
would be:

******************************************************
ScrollDecorator myDecorator = new ScrollDecorator(new TextView());

// calls decorator's specific additional method
myDecorator.scrollTo();

//
// IVisualComponent is not directly implemented by ScrollDecorator.
// But getting a Component type view of it. Note that even though
// you call here "getComponent()" what you actually get is the (decorated)
// Component interface type view implemented by ScrollDecorator
// and not the TextView instance
//
myDecorator.getComponent().focus();
myDecorator.getComponent().hide();
******************************************************

In any case I would asume that it is more commonly used the variation of the
Decorator that only redefines functionality rather than adding i.e.
reusability, therefore continuing the example above would be:

// note again getComponent() means get me the Component type view of the
// Decorator (it is decorated)
//
IVisualComponent myComponent = new ScrollDecorator(new
TextView()).getComponent();

If you are wondering what the main advantage of the partially componentized
perfectjpattern's Decorator is, here is the answer:

- perfectjpattern offers a base class AbstractDecorator<C> from which you
extend and specify which type you would like to decorate and implementation
for exactly and only the methods you want to decorate e.g.
http://perfectjpattern.svn.sourceforge.net/viewvc/perfectjpattern/trunk/perfectjpattern-examples/src/main/java/perfectjpattern/core/structural/decorator/ScrollDecorator.java?view=markup

- you either define your additional methods e.g. scrollTo() or redefine some
of the methods of the underlying component without having to provide
implementation for the full Component interface e.g. IVisualComponent but
only implement those relevant methods you want to decorate.

Advantage: Having many decorator implementations is more maintainable and
reusable because adding more functionality/methods in the Component
interface will not require existing Decorators to continuously be
modified/adapted to provide empty implementations.

Disadvantage: Since the Decorator does not directly implement the Component
interface, changing the component interface might lead to bugs i.e. leaks
where intended redefining methods might end up being bypassed. However a
valid solution would be fine graining the Component interface into many
smaller interfaces and the decorator hard (type) implementing only those
relevant while still decorating the full Component interface.

Thanks in advance,
regards,
Giovanni
Back to top
  Ads
Advertising
Sponsor


Display posts from previous:   
Post new topic   Reply to topic    Computer Software Talk Forum Index -> Computer Software Talk All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Australian Debt Consolidation Experts
medical insurance
Wedding Ring
ESCORTFORUM: recensioni di escort e girl
Swinging in Spain
Point of Sale
mortgage quotations
Make Your Own Website
Cheap phone calls to Poland
Cleaning Service
black mold
UK Swingers Genuine Contacts Site
Janitorial Supplies
bissell Vacuum parts


Board Security

112 Attacks blocked

Powered by phpBB © 2001, 2005 phpBB Group