aspose file tools*
The moose likes Java in General and the fly likes Decorator Vs Java Mixin Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Decorator Vs Java Mixin" Watch "Decorator Vs Java Mixin" New topic
Author

Decorator Vs Java Mixin

Himalay Majumdar
Ranch Hand

Joined: Sep 28, 2008
Posts: 324
I studied about Java Mixins from the following

http://www.diku.dk/forskning/performance-engineering/Generative-software-development/Glossary/mixins.html

I dont think java supports the concepts of mixin, but cant we achieve the same using decorator pattern like

BufferedInputStream bin = new BufferedInputStream(in);

DataInputStream dbin = new DataInputStream(bin);

A clear explanation of mixin will be really helpful

Thank you,
Himalay Majumdar


SCJP 1.6, SCWCD 5.0, SCBCD 5.0 [loading..]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

A mixin is basically a class you inherit from to absorb its code -- as opposed to a class you inherit so that your objects have a certain type. In languages with multiple inheritance, mixins can be a useful way to reuse code. In Java, the concept really doesn't apply.

Decorators are certainly useful, but they're not the same as a mixin. Decorators only apply to a specific type hierarchy -- i.e., a BufferedReader can only decorate a Reader, not a JButton or a Thread. In contrast, a mixin can be extended by pretty much any class.


[Jess in Action][AskingGoodQuestions]
Himalay Majumdar
Ranch Hand

Joined: Sep 28, 2008
Posts: 324
Got a clear picture. Thanks Ernest
Himalay Majumdar
Ranch Hand

Joined: Sep 28, 2008
Posts: 324
Hi Ernest,

Can you go thru this link below. I understand it says a way to implement mixin in java.

http://csis.pace.edu/~bergin/patterns/multipleinheritance.html

Thanks again.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

The link shows you how to use delegation instead of inheritance. Delegation is where you implement a method by calling a method of some other object. Delegation is very useful in Java. Some IDEs will actually automatically delegate implementation of an interface this way, which can be a big help.

It's not really the same as mixins, though, because it's not automatic, and it's fairly brittle (i.e., if the mixin adds or removes a method, the client class has to be changed, too, which isn't necessary with true mixins.) But it's about as close as you can get in Java!
Himalay Majumdar
Ranch Hand

Joined: Sep 28, 2008
Posts: 324
In that case, can I say decorators are basically delegation applied in specific type hierarchy.

Its really nice to know about delegations, decorators and mixins.

Thank you so much.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by Himalay Majumdar:
In that case, can I say decorators are basically delegation applied in specific type hierarchy.


Well, no, not really. The whole idea of a decorator is that it has the same type as the object it's decorating -- a BufferedWriter is a Writer that can decorate other Writers. Any class, regardless of type, can delegate to any other class.
Himalay Majumdar
Ranch Hand

Joined: Sep 28, 2008
Posts: 324
I got it..Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Decorator Vs Java Mixin