You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Stephen Colebourne <sc...@btopenworld.com> on 2002/08/09 00:35:48 UTC

[pattern] Command or Closure [was ThrowableHandler]

What do people read into these two classnames?
Are they the same, or different?
Is there a strict computer science meaning?

I'll confess that I hadn't heard of a Closure until recently, hence my
choice of Command. However [collections] project used Closure. I'm willing
to change the classname to Closure if there is a preference for it.

Stephen

----- Original Message -----
From: "Ola Berg" <ol...@arkitema.se>
To: <co...@jakarta.apache.org>
Sent: Thursday, August 08, 2002 9:29 PM
Subject: RE: [pattern] ThrowableHandler


> >> There is this other pattern called Command, where a Command
> >> object is an encapsulation of a command call of some sort
> >> (with parameters and everything), that you use fx when you do
> >> undo/redo, or implement command queues. Like the State
> >> pattern for state changes.
>
> >That is a closure.
>
> Isn\'t is strange then that Closure is recieving objects to handle/act
upon, instead of encapsulating the Action (that you send to some Machine,
like in the undo/redo case)? Or am I just plain stupid here?
>
> >> BTW, I find the term Closure a bit vague.
>
> >Why vague? Closures are a well known construct that have been around for
a
> >long, long time. Certainly decades before people started talking about
> >design patterns.
>
> Ah, well I said that the term appeared vague to me, not that it was vague
in itself ;-) Since I started to program OO professionally first in the late
90\'s, I am too young in the business (albeit not on earth) to remember. The
Command pattern is what I remember (and I never bothered to learn the
synonyms).
>
> /O
>
> --
> To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
> For additional commands, e-mail:
<ma...@jakarta.apache.org>
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: [pattern] Command or Closure [was ThrowableHandler]

Posted by Steve Downey <st...@netfolio.com>.
Commands can be implemented using closures, but that isn't a requirement.

A closure is a CompSci construct. It encapulates the state of the machine
together with code that can then be executed later in the context of the
state at the time that it was created.

Anonymous classes in Java have many of the features of closures. The fact
that local variables, method parameters and exception handler parameters
must be final and assigned before the body of the inner class keeps them
from being true closures. IOW, if Java had try closures, the following code
would be legal:

int a;

Closure c = new Closure() {
	int fiddleA(int i) {
		a = i;
		return a;
	}
}

return c;

// .... <elsewhere>

int j = c.fiddleA(6);


and the value of the stack based variable a would be changed.


Now Command is a pattern that passes pieces of execution around, usually
with invocations like do() and undo(). Closures are quite useful in
implementing Command, and in fact, the pattern comes from languages that
support block closures, like Smalltalk.

Some Closures are Commands. Not all Commands are Closures. For example, I've
used the Command pattern in C++, which doesn't support closures as a
language feature. Commands have to carry references to their significant
state around with them as members.

All clear?

As mud?

Here's a web reference which might clarify things further:
http://mindprod.com/jglossclosure.html




> -----Original Message-----
> From: Henri Yandell [mailto:bayard@generationjava.com]
> Sent: Thursday, August 08, 2002 7:01 PM
> To: Jakarta Commons Developers List
> Subject: Re: [pattern] Command or Closure [was ThrowableHandler]
>
>
>
> Command makes me think Design patterns. Closure makes me think of
> anonymous commands a la perl/lisp.
>
> Just going on the names, I would expect to extend a Command, but I would
> expect a Closure to take a piece of Java code, or more realisitically to
> be intended for use as an anonymous inner class.
>
> In reality they're probably the same concept [? any fanatic Lispers out
> there?], so is amazing the difference a name can make to people :)
>
> Hen
>
> On Thu, 8 Aug 2002, Stephen Colebourne wrote:
>
> > What do people read into these two classnames?
> > Are they the same, or different?
> > Is there a strict computer science meaning?
> >
> > I'll confess that I hadn't heard of a Closure until recently, hence my
> > choice of Command. However [collections] project used Closure.
> I'm willing
> > to change the classname to Closure if there is a preference for it.
> >
> > Stephen
> >
>
>
> --
> To unsubscribe, e-mail:
> <ma...@jakarta.apache.org>
> For additional commands, e-mail:
> <ma...@jakarta.apache.org>
>
>
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: [pattern] Command or Closure [was ThrowableHandler]

Posted by Henri Yandell <ba...@generationjava.com>.
Command makes me think Design patterns. Closure makes me think of
anonymous commands a la perl/lisp.

Just going on the names, I would expect to extend a Command, but I would
expect a Closure to take a piece of Java code, or more realisitically to
be intended for use as an anonymous inner class.

In reality they're probably the same concept [? any fanatic Lispers out
there?], so is amazing the difference a name can make to people :)

Hen

On Thu, 8 Aug 2002, Stephen Colebourne wrote:

> What do people read into these two classnames?
> Are they the same, or different?
> Is there a strict computer science meaning?
>
> I'll confess that I hadn't heard of a Closure until recently, hence my
> choice of Command. However [collections] project used Closure. I'm willing
> to change the classname to Closure if there is a preference for it.
>
> Stephen
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>