• Post Reply Bookmark Topic Watch Topic
  • New Topic

Container & Contained Interface Question  RSS feed

 
Thomas Hubschman
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers,

Is it bad, how bad is it, and is there another way to get around a contained class implementing the same interface as the container class, with the container class having pass through method implementations to the contained class.

In my particular case the container class is Data which contains a reference to the Storage class but provides the additional functionality of locking and finding (finding through a cache). The Storage class presents only data altering methods.

The issue is that I want to add a 'DBSchema' interface that provides the following methods:



It seems to me that what makes the most sense is to have the Data class AND the Storage class both implement this interface. Although in the case of the Data class it is just passing through calls to the contained Storage class. The Data class needs to implement the interface so that external callers can continue to use an interface to access the Data class rather than an instance reference.

That seems icky to me.... but I am not sure. I searched around on Google for a bit, but didn't find an answer there.

Any help as always would be appreciated.

Regards,

Tom
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the question?
 
Thomas Hubschman
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The question is how bad is it to have a containing (threading wrapper) class AND the singleton implementation class it contains implement the same interface listed above.

Tom
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do the clients care about the Storage class? I suspect not, so why have it implement the interface.
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think it's bad to implement the interface in both the container and the contained classes.

It would not be uncommon to do this to implement a "new" collection, using another collection internally.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may be describing the "Decorator" pattern, generally recognized to be a good, rather than bad thing. Google it!
 
Thomas Hubschman
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Guys!

After reading the Wikipedia entry I have realized that the Decorator pattern is exactly what I am doing. While in fact the current implementation does not NEED to have the 'contained' functionality public, there are plenty of administrative use cases where the concurrency guarding behavior of the contained class would not be necessary. As such, I think I will continue to have both container and contained classes implement the interface and let the implementor of the API make the decision as to which class to use.

Tom
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!