| View previous topic :: View next topic |
| Author |
Message |
alepuzio Guest
|
Posted: Wed Feb 06, 2008 12:12 pm Post subject: Questions of a beginner about difference between pattern "c |
|
|
Hi!
I'm insteresting by a few of time about design pattern. In the book
"Design Pattern" of Gangs Of Four I read that some pattern is in
compile time (i.e. Abstract Factory) and others are in run-time (es.
Decorator).
I don't understand this point: the decorator pattern in Java language
is used for I/O package.
BufferedReader stdin = new BufferedReader(new
InputStreamReader(System.in)
This is an example of pattern decorator but I know at compile-time the
structure of variable stdin because in compile time ( when I read the
source code) I see the constructor.
What's means the run-time (when the code runs) in this case?
Excuse-me for the level of questions and for my English :)
Bye |
|
| Back to top |
|
 |
| |
Ads |
Advertising
Sponsor
|
|
H. S. Lahman Guest
|
Posted: Wed Feb 06, 2008 9:55 pm Post subject: Re: Questions of a beginner about difference between pattern |
|
|
Responding to Alepuzio...
| Quote: |
I'm insteresting by a few of time about design pattern. In the book
"Design Pattern" of Gangs Of Four I read that some pattern is in
compile time (i.e. Abstract Factory) and others are in run-time (es.
Decorator).
|
The difference lies in the instantiation of the relationships. In
Abstract Factory the relationships between ConcreteFactoryN and
ProductXN are invariant in the problem space. So A ConcreteFactory2 will
always construct a PorductA2 or ProductB2 but never a ProductA1. Since
that is always true, the relationships can be defined statically for the
compiler.
Contrast that with Decorator where the relationship between Decorator
and VisualComponent can be modified at run-time. That is, one can add
different functionality by dynamically assigning different Decorators to
suit a particular solution context.
| Quote: |
I don't understand this point: the decorator pattern in Java language
is used for I/O package.
BufferedReader stdin = new BufferedReader(new
InputStreamReader(System.in)
|
That would be true if the application only ever needed a single
combination of buffered stream. In that case Decorator would be overly
complex because one could just define a BufferedReader object directly.
The point of Decorator is to dynamically add functionality _as required
by the solution context_.
Remember that Java, being a member of the C family of OOPLs, is
inherently statically typed. So the syntax requires that the compiler
must be able to resolve types. To provide the dynamic substitution of
Decorator one needs additional infrastructure. The key is the argument
to the constructor that does the instantiation. That is used to
instantiate the relationship.
By using the constructor the implication is that a Buffered Reader will
/always/ read a stream. If you wanted the dynamic capability of
BufferedReader to, say, read an ISAM reader for some other context in
the solution, you would have to also provide a setter in BufferedReader
to supply the ISAM context to replace the stream context. At that point
you would have a true Decorator despite the Java static typing.
--
There is nothing wrong with me that could
not be cured by a capful of Drano.
H. S. Lahman
hsl@pathfindermda.com
Pathfinder Solutions
http://www.pathfindermda.com
blog: http://pathfinderpeople.blogs.com/hslahman
"Model-Based Translation: The Next Step in Agile Development". Email
info@pathfindermda.com for your copy.
Pathfinder is hiring:
http://www.pathfindermda.com/about_us/careers_pos3.php.
(888)OOA-PATH |
|
| Back to top |
|
 |
| |
Ads |
Advertising
Sponsor
|
|
alepuzio Guest
|
Posted: Thu Feb 07, 2008 9:01 am Post subject: Re: Questions of a beginner about difference between pattern |
|
|
On 6 Feb, 16:55, "H. S. Lahman" <h...@pathfindermda.com> wrote:
| Quote: |
CUT
Decorator one needs additional infrastructure. The key is the argument
to the constructor that does the instantiation. That is used to
instantiate the relationship.
CUT
H. S. Lahman
|
Right: if I have understood before I can write (in Java)
(1)BufferedReader stdin = new BufferedReader(new
InputStreamReader(System.in)
and after i.e.
(2)stdin=new BufferedReader(new SocketStream(net_parameter))
Pratically the call of the constructor of a class is considered as a
simple parameter what add some qualities to built object.
The qualities of stdin are different between (1) and (2) phases too if
stdin is the same object.
Right?
Thanks a lot |
|
| Back to top |
|
 |
| |
Ads |
Advertising
Sponsor
|
|
H. S. Lahman Guest
|
Posted: Thu Feb 07, 2008 9:33 pm Post subject: Re: Questions of a beginner about difference between pattern |
|
|
Responding to Alepuzio...
| Quote: |
On 6 Feb, 16:55, "H. S. Lahman" <h...@pathfindermda.com> wrote:
CUT
Decorator one needs additional infrastructure. The key is the argument
to the constructor that does the instantiation. That is used to
instantiate the relationship.
CUT
H. S. Lahman
Right: if I have understood before I can write (in Java)
(1)BufferedReader stdin = new BufferedReader(new
InputStreamReader(System.in)
and after i.e.
(2)stdin=new BufferedReader(new SocketStream(net_parameter))
Pratically the call of the constructor of a class is considered as a
simple parameter what add some qualities to built object.
The qualities of stdin are different between (1) and (2) phases too if
stdin is the same object.
Right?
|
You could do it that way but in a GC language like Java that invites
foot-shooting because there may be references to the old stdin hanging
around. That may be fine if the existing clients always want the
buffered stream version, but it opens the door for maintenance problems
if some but not all of the existing clients need to use the socket
version later. It also defeats the purpose of Decorator because the
clients are "hard-wired" to particular stdin objects throughout the
solution.
The reason one uses Decorator is because the /same/ clients may need a
different implementation of BufferedReader based on some dynamic context
in the solution (i.e., at different times in the solution). A more
robust way of dealing with that is dynamically changing BufferedReader's
relationship to the source. That has two advantages:
(1) The change in context is completely transparent to the clients; they
still collaborate with the same BufferedReader. IOW, somebody else can
keep track of context and ensure the relationships are right. That
allows the solution context driving the choice of source to span
multiple clients and multiple collaborations.
(2) The only new object when the source context changes is the source,
which is exactly as it should be. Among other things, that separation of
concerns usually means that one can instantiate a set of source objects
at startup and reuse them on an as-needed basis as the solution context
changes.
--
There is nothing wrong with me that could
not be cured by a capful of Drano.
H. S. Lahman
hsl@pathfindermda.com
Pathfinder Solutions
http://www.pathfindermda.com
blog: http://pathfinderpeople.blogs.com/hslahman
"Model-Based Translation: The Next Step in Agile Development". Email
info@pathfindermda.com for your copy.
Pathfinder is hiring:
http://www.pathfindermda.com/about_us/careers_pos3.php.
(888)OOA-PATH |
|
| Back to top |
|
 |
| |
Ads |
Advertising
Sponsor
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|

112 Attacks blocked
Powered by phpBB © 2001, 2005 phpBB Group
|