Is this just container pattern?

Hallo pattern-lovers,

I have troubles finding design pattern that covers relation betweenTelevion andDoodle in following example.

Example:

Purpose:Offer only read only methods of given object.

// Commonality between Doodle and Television

// Not neccessary for example, but good practice IMHO

interface TelevisionState{

abstractpublicint getSelectedChannel();

abstractpublicboolean isRunning();

}

// Some class

class Televisionimplements TelevisionState{

publicvoid setSelectedChannel(int number){/* ... */}

publicint getSelectedChannel(){/* ... */}

publicboolean isRunning(){/* ... */}

}

// Read-only methods of television

class Doodleimplements TelevisionState{

private Television tv;

Doodle(Television tv){

this.tv = tv;

}

publicint getSelectedChannel(){

return tv.getSelectedChannel();

}

publicboolean isRunning(){

return tv.isRunning();

}

}

Is it justcontainer pattern?

[2633 byte] By [a3cchana] at [2007-11-26 13:17:00]
# 1

never heard of container patterns.

I don't know what a Doodle is, so I can't comment on its relationship with Television.

Your example looks like what I'd call mixin behavior. Doodle implements an interface and also has a constructor that takes a reference of the interface type and defers to the ctor parameter object when implementing the interface behavior. It allows users to change the behavior of an object like Doodle on the fly, simply by passing a different instance of the interface (e.g.., your TelevisionState).

I'd say you compromised the usefulness of the pattern by passing a Television to your Doodle constructor instead of a TelevisionState. Better to change the private data member and ctor argument to TelevisionState.

%

duffymoa at 2007-7-7 17:40:14 > top of Java-index,Other Topics,Patterns & OO Design...
# 2

> never heard of container patterns.

http://en.wikipedia.org/wiki/Container_pattern ... but that pattern's not what i ment ... as i wrote in my original question, i simply can not name it

> I don't know what a Doodle is, so I can't comment on

> its relationship with Television.

It's all about passing 'read-only object' to world. This example continues from original question, do not comment bad coupling between Bar-Barman, Bar-Drunk, please^_^:final class Bar {

static final Television tv = new Television();

static final Doodle doodledTv = new Doodle(tv);

public static void open(Barman emploee){

emploee.tv = tv;

}

public static void sitAndDrink(Drunk host) {

host.doodledTv = doodledTv;

}

}

class Barman {

// Control tv

Television tv;

}

class Drunk {

// View tv

Doodle doodledTv;

}

Doodle is jus arbitrary identifier, like FooBar. Name might affect your oppinion on that pattern imho.

> I'd say you compromised the usefulness of the pattern

> by passing a Television ... instead of a TelevisionState.

Ok thank you, that's correct. TelevisionState would be much better.

a3cchana at 2007-7-7 17:40:14 > top of Java-index,Other Topics,Patterns & OO Design...
# 3

> > never heard of container patterns.

> http://en.wikipedia.org/wiki/Container_pattern ...

> but that pattern's not what i ment ... as i wrote in

> my original question, i simply can not name it

Sorry, this is one situation where I don't agree with Wiki. I'll need another source.

> It's all about passing 'read-only object' to world.

That's just about immutable objects. It's not necessarily a "pattern".

> > I'd say you compromised the usefulness of the

> pattern

> > by passing a Television ... instead of a

> TelevisionState.

> Ok thank you, that's correct. TelevisionState would

> be much better.

I find your examples confusing. If you're sticking with nonsense names use them consistently (e.g., "Foo" and "Bar"). Mixing with a real object like Television just muddies the waters for me.

%

duffymoa at 2007-7-7 17:40:14 > top of Java-index,Other Topics,Patterns & OO Design...
# 4
I've never heard of the container pattern, either. For your example, I would say its a Facade.
stefan.schulza at 2007-7-7 17:40:14 > top of Java-index,Other Topics,Patterns & OO Design...
# 5
data structure != design pattern> > never heard of container patterns.> http://en.wikipedia.org/wiki/Container_pattern ...> but that pattern's not what i ment ... as i wrote in> my original question, i simply can not name it
mchan0a at 2007-7-7 17:40:14 > top of Java-index,Other Topics,Patterns & OO Design...
# 6

This sounds/looks/smells more like a Proxy of some sort, maybe a Shield Proxy? It probably has many names.

You should be troubled more by why you need it in your implementation. Usually exposing state (even through methods) is a no-no, and you have a whole interface just for that.

Do you have any real code that uses these objects?

gewittera at 2007-7-7 17:40:14 > top of Java-index,Other Topics,Patterns & OO Design...
# 7

> This sounds/looks/smells more like a Proxy of some sort,

Maybe.

> maybe a Shield Proxy? It probably has many names.

Never heard of a "Shield Proxy".

> You should be troubled more by why you need it in

> your implementation.

This is pretty standard practice for mixin behavior. Why is this so troubling?

> Usually exposing state (even

> through methods) is a no-no, and you have a whole

> interface just for that.

The interface is only about methods, not state. The user has no idea whether this class implements the interface itself or defers to a child instance to do the work.The interface is hiding the implementation, as all interfaces do. The user has no idea which implementation of that interface is being used to accomplish the task.

Gotta expose something, or the object isn't very useful.

%

duffymoa at 2007-7-7 17:40:14 > top of Java-index,Other Topics,Patterns & OO Design...
# 8

The whole example is useless. It just describes some real world part, like those beginner's text-books examples of the animal kingdom or vehicles.

I can't tell what's the significant part of the thread-starter question. If it was the wrapping, yes, you can do that. If it's exposing a read-only interface, you can do that too, without wrapping.

To discuss design, we need to know why on earth would anyone need an object that can change channels, and can be asked for its current channel. Right now these objects are useless just like an empty interface.

gewittera at 2007-7-7 17:40:14 > top of Java-index,Other Topics,Patterns & OO Design...
# 9

> The whole example is useless. It just describes some

> real world part, like those beginner's text-books

> examples of the animal kingdom or vehicles.

I agree with this.

> I can't tell what's the significant part of the

> thread-starter question. If it was the wrapping, yes,

> you can do that. If it's exposing a read-only

> interface, you can do that too, without wrapping.

I think it's meaningful to demonstrate how to change behavior by passing a new implementation class to the constructor, but the example is hardly "real world".

> To discuss design, we need to know why on earth would

> anyone need an object that can change channels, and

> can be asked for its current channel. Right now these

> objects are useless just like an empty interface.

Agreed.

%

duffymoa at 2007-7-7 17:40:14 > top of Java-index,Other Topics,Patterns & OO Design...
# 10
Some implementations of Memento pattern, for example, uses this narrow/wideinterface split.The memento creator have access to mutators (setX) and external users only holds the memento for a while, so they have access to getX at most.
gewittera at 2007-7-7 17:40:14 > top of Java-index,Other Topics,Patterns & OO Design...
# 11
Gotta admit that all this fascination with the GoF patterns leaves me cold. I've seen too many people abuse them to be terribly interested anymore. A great book, and a great idea, but patterns knowledge doth not a great programmer make.%
duffymoa at 2007-7-7 17:40:14 > top of Java-index,Other Topics,Patterns & OO Design...
# 12

of course, not everything should be implemented as one of the patterns right away or at all.

having a read-only interface, or an empty interface, actually tells clients "someone else is taking care of business", and i neglected the fact earlier.

not sure how this applies to televisions though :)

gewittera at 2007-7-7 17:40:14 > top of Java-index,Other Topics,Patterns & OO Design...
# 13

> of course, not everything should be implemented as

> one of the patterns right away or at all.

We agree then. 8)

> having a read-only interface, or an empty interface,

> actually tells clients "someone else is taking care

> of business", and i neglected the fact earlier.

Indeed.

> not sure how this applies to televisions though :)

LOL! Hope your holidays were good.

%

duffymoa at 2007-7-7 17:40:14 > top of Java-index,Other Topics,Patterns & OO Design...
# 14
>Is it just container pattern?I must have been away the weekend that "pattern" (?) was invented.Either that, or I'm getting too old to keep up with the pace of "pattern innovation".Sigh.
karma-9a at 2007-7-7 17:40:14 > top of Java-index,Other Topics,Patterns & OO Design...
# 15

> The whole example is useless. It just describes some

> real world part, like those beginner's text-books

> examples of the animal kingdom or vehicles.

>

> I can't tell what's the significant part of the

> thread-starter question. If it was the wrapping, yes,

> you can do that. If it's exposing a read-only

> interface, you can do that too, without wrapping.

Yes the OP said soemthing about It's all about "passing 'read-only object' to world".

Maybe with IP-over-cable-TV-and-return-channel we can make use of this example to provide an interface for AudienceRateDetectors, using it like this:

class AudienceRateDetector

{

...

public float calculateRate(int channel)

{

long total = 0;

long viewers =0;

for(TelevisionState tv : allTelevisonsInTheNation() )

{

total = tv.isRunning() ? total + 1 : total;

viewers = (channel == tv.getSelectedChannel()) ? viewers +1 : viewers;

}

return viewers / total;

}

}

:-)

This hardly could be called a pattern; it's just an Interface.

Horst_Ma at 2007-7-21 15:47:43 > top of Java-index,Other Topics,Patterns & OO Design...