• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Decorator Vs Java Mixin

 
Himalay Majumdar
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Himalay Majumdar
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got a clear picture. Thanks Ernest
 
Himalay Majumdar
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it..Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic