• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Adapter or Bridge

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
/*
My existing system has an interface Property
with a makeClean() method. I have two implementations,
Office and Workshop. I have a factory as PropertyFactory
to chose the instance based on client input
*/


/*
Now lets say, I bought a Home for which I have defined a service takeCare().
This house will now require the makeClean() service.
Also my client knows only the Property Interface
*/

/* I think this can be an example of Adapter Pattern.
Here is how I implement it
*/

// Property remains the same

/* I think that it is not a good practice to use instanceof in the Client code */


/* So I thought of an alternative. I decided to use Bridge */

/* Modify the Property Interface */

/*
The problem here that I feel is takeCare() method can be
invoked on Office and Workshop
*/

/*
Please advice if my understanding of Adapter and Bridge is correct
I will also appreciate the pros and cons mentioned of both the approaches.
Is there a better way to implement.
*/

[ August 18, 2004: Message edited by: Aspiring Architect ]
[ August 19, 2004: Message edited by: Nikhil Vasaikar ]
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello "Aspiring Architect" and welcome to the JavaRanch!
We have a couple of rules -- one of which is the naming policy -- so I'd like to ask you to go to My Profile and change your public display name into something compliant with the naming policy.

Thanks.
 
Nikhil Vasaikar
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I apologize for the non-compliant display name. I have changed the same.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much! Now the next step would be to use the UBB code tags to format your code example (you can find a button for inserting it below the editing textarea). You can edit your post using the second icon from the right above your post.
 
Nikhil Vasaikar
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ilja, for providing information on code Indentation. I have indented the code.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice, now I can actually read it...

Now, could you please explain where in the both examples you used the Adapter and Bridge design patterns? I just don't see it...

Also, would it be possible that the makeClean method in House just calls takeCare itself, so that the client doesn't need to know about it at all?
 
Nikhil Vasaikar
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You for replying.
The first implementation uses Adapter. The interface Extra adapts class Home to interface Clean.
The second implementation uses Bridge. The classes Office, Workshop, Home vary independently of the abstract class Extra.
Yes we could make the takeCare() method private in Home and call it from makeClean() in Home. However, then, both the methods will be invoked. Lets say, I have already cleaned the House. Now I just want to take care. This will not be possible then. Please correct me if my understanding of Adapter or/and Bridge is wrong. I have just started reading Design Patterns a week ago.
[ August 20, 2004: Message edited by: Nikhil Vasaikar ]
 
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should avoid giving a class a method that doesn't make sense to it. In your case takeCare() doesn't make sense in WorksShop and Office. I think you said that you are forcing that method even though they don't make sense for the 2 classes.

I would suggest having Home, WorkShop and Office all implement the property interface. If you are trying to say that a Home contains a WorkShop and Office then it may be a fit for the Composite interface. This would work out well to if you had more than 1 home/property as you could do things like the following.

Even if the Composite pattern doesn't fit it makes sense to have all 3 classes implement Property.



 
Nikhil Vasaikar
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with you. However I have a different scenario. Workshop and Ofiice are Property. There is only one service makeClean() for them. Now this is an existing system. Lets say I bought a Home [which is also a Property now, i.e it needs makeClean()], for which I require a service takeCare() which is only applicable to Home. How should I plug in this Home into my existing system by making minimum code changes, minimum recompilations and not exposing takeCare() to Office and Workshop.
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should have the Property interface that only has the makeClean() method, and Home should implement Property and add the takeCare() method. In your code when you want to be able to makeClean() assign WorkShop, Office and Home to the Property interface, and when you want to takeCare() you must assign it only to the Home class as this is the only class that has this method.
 
Nikhil Vasaikar
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Steve. I understand what you are saying. My Example cannot be Adapter pattern because Home is also a Property i.e a compatible interface. Also my client is aware of Extra..Right!
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For all the reasons you state it is not the adapter pattern.
 
If you settle for what they are giving you, you deserve what you get. Fight for this tiny ad!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic