| Author |
Regarding Facade Pattern implementation
|
Kiran Baratam
Greenhorn
Joined: Sep 03, 2003
Posts: 27
|
|
Hi All I have a doubt regarding pattern imlplementation.I am reading facade pattern and they had written JDBC is best example for facade pattern. Connection is an interface and when i call a method say createStatement() its creating the statement and returning a Statement Object.Statement itself is an interface. I know here facade pattern is implemented but i am not able to understand how is it working. Who implemented these interfaces.how will i see them. i wanted to know how i can implement this. Can somebody help me. Regards Kiran [ September 03, 2003: Message edited by: Kiran Baratam ]
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
I usually think of facade as a public object that provides a public face for a component or function, and a mess of private objects that do the real work. Facade hides all the complexity from the client, giving the client a simple set of public methods. This makes the client simpler, because it does not have knowledge of a lot of little implementation objects, and gives the component author freedom to change the implementation without telling the client, so long as all the contracts implied by the public methods are maintained. JDBC does not jump out in my mind as doing this. And that might be because they've done it so well ... maybe there are zillions of detailed little implementation classes inside JDBC that I've never heard of. That might make Connection a successful facade after all!
|
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
|
 |
Pradeep bhatt
Ranch Hand
Joined: Feb 27, 2002
Posts: 8876
|
|
... maybe there are zillions of detailed little implementation classes inside JDBC that I've never heard of. That might make Connection a successful facade after all!
In that case we have lot of facades in real life. How is facade defined in that case?
|
Groovy
|
 |
Jayadev Pulaparty
Ranch Hand
Joined: Mar 25, 2002
Posts: 645
|
|
Originally posted by Pradeep Bhat: In that case we have lot of facades in real life. How is facade defined in that case?
I'm a little confused with the above statement. All the little implementation classes are the ones masked by the facades. Am i missing something here
|
 |
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
I think what Pradeep meant was that almost any object you call a message on will delegate much of its work to many other little objects. I think a good hint that an object is a facade is when - it delegates to objects inside the same subsystem and gets used from outside the subsystem, and - it doesn't contain much logic. But I have to admit that it's something I am wondering about from time to time, too...
|
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
|
 |
Kiran Baratam
Greenhorn
Joined: Sep 03, 2003
Posts: 27
|
|
Hello Everybody Thansk for the replies.i am trying to implement patterns and currently i am reading the facade pattern.I understand the use of the facade and try to understand some implementation.I tried to figure out from DriverManagerClass which is using Driver interface, but did not find any way.I am not able to follow exactly what is heppening when i say Statement stmt = connection.createStatement(); Can you please help me. Regards Kiran [ September 03, 2003: Message edited by: Kiran Baratam ]
|
 |
Pradeep bhatt
Ranch Hand
Joined: Feb 27, 2002
Posts: 8876
|
|
Statement stmt = connection.createStatement();
The Statement gets created. Are you asking which pattern it is?
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
I suppose what distinguishes a facade from anything else that delegates work is that its main purpose is to simplify the public interface and deliberately hide the details. (Note that almost ALL the GoF patterns seek to simplify interfaces and reduce coupling, so that's a pretty generic objective.) connection.createStatement() doesn't strike me as a facade because connection reveals to you that a statement exists rather than hiding details. And Connection has a some real functionality of its own so it does not exist primarily to simplify the API. We have "create" in the name here, so look in the GoF category of creational patterns. This might be a FactoryMethod or a Builder or it might just be a simple convenience method that saves you writing a couple lines of code. Waddya think?
|
 |
Pradeep bhatt
Ranch Hand
Joined: Feb 27, 2002
Posts: 8876
|
|
Stan, I dont think it is a Builder pattern but a factory pattern. Create statement is a creational pattern so I dont think it can be a Facade which is structural. [ September 04, 2003: Message edited by: Pradeep Bhat ] [ September 05, 2003: Message edited by: Pradeep Bhat ]
|
 |
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
|
I would actually say that it's AbstractFactory.
|
 |
Pradeep bhatt
Ranch Hand
Joined: Feb 27, 2002
Posts: 8876
|
|
Can you please explain?
Originally posted by Ilja Preuss: I would actually say that it's AbstractFactory.
|
 |
Jayadev Pulaparty
Ranch Hand
Joined: Mar 25, 2002
Posts: 645
|
|
|
AbstractFactory deals with creation of families of related objects supporting the same interface. I think factory pattern is the best fit here.
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
And of course since there are no pattern police enforcing rules (and in fact no rules!) an object can participate in several patterns at once. Connection might be a factory for statements, a facade hiding some database details, and a first-order domain object all at once. Which might lead you to some opinions about certain qualities of the design. [ September 05, 2003: Message edited by: Stan James ]
|
 |
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
Originally posted by Jayadev Pulaparty: AbstractFactory deals with creation of families of related objects supporting the same interface. I think factory pattern is the best fit here.
There is no Factory pattern - at least not in the GoF book. There is a Factory Method pattern, where a factory method is declared and used in a class, but implemented by subclasses. And there is the Abstract Factory pattern, where there is an interface defined for the factory method and actual implementations handed around (and used, of course) by other objects. At least, that's my current understanding - and from that, the latter seems to be a better fit for connection.createStatement().
|
 |
Jayadev Pulaparty
Ranch Hand
Joined: Mar 25, 2002
Posts: 645
|
|
|
I'm really not sure if Factory is a GoF pattern. I don't think so. I came across this in Larman's book - Applying UML and patterns. What it says is that if there is some creational logic involved for an object, the class that is supposed to create the object should not be concerned about it. It makes it less cohesive w.r.t its responsibilities. It can delegate the job to a factory class that takes care of it.
|
 |
Fintan Conway
Ranch Hand
Joined: Apr 03, 2002
Posts: 141
|
|
Originally posted by Kiran Baratam: I tried to figure out from DriverManagerClass which is using Driver interface, but did not find any way.I am not able to follow exactly what is heppening when i say Statement stmt = connection.createStatement(); [ September 03, 2003: Message edited by: Kiran Baratam ]
Hi Kiran, I think you may be approaching this from the wrong side. The idea of the facade pattern is that you have an obect (the "facade" = 'connection'), and within the facade you have a multitude of classes which have their own methods. The facade will hide these classes and only expose the appropriate methods to you. When you look at this code you do not see the inner workings of the facade pattern - only the end implementation. For a better understanding of how to use the facade pattern visit this link javaworld facade pattern Regards, Fintan
|
 |
 |
|
|
subject: Regarding Facade Pattern implementation
|
|
|