From: Franz GEIGER Date: 2001-01-03T04:40:02+09:00 Subject: [ruby-talk:8534] Re: design question What about private inheritance in this situation? Wouldn't it save a lot of work AND enable me to hide the methods I don't like to expose to the public? Regards Franz GEIGER "Kevin Smith" wrote in message news:0G6H0012WVQMJY@mta5.snfc21.pbi.net... > chad fowler wrote: > >In the Java world, I tend to encapsulate Java's > >supplied libraries/types by composing things like > >collections inside my own domain objects. So, for > >example, to create a collection ("People") or "Person" > >objects, I would create a "People" class with a > >limited interface that delegated any requests for > >members of the list to an internal representation > >which I could vary at a later time (like an ArrayList > >or something like that). > > > >The problems I'm trying to solve by doing that in Java > >are: 1) I don't want to expose an explicit type which > >is not under my control as part of an externally > >visible interface, 2) I want to be able to vary the > >internal representation at any time, 3) I want to take > >care of type casting--mine is a colleciton of > >"Person"s, not "Object"s 4) I want my intent to be as > >clear as possible (users of my classes won't have to > >think "What is this array for?"). > > > >So, my question is: What are the recommended > >practices in Ruby? Do folks generally inherit the > >supplied Ruby classes or do they try to use delegation > >in the same way I've outlined above? It seems that a > >lot of my reasons for using composition/delegation are > >somewhat irrelevant in Ruby, but it still feels really > >weird to extend an Array to create a domain-specific > >list. > > I've only been working in Ruby for a couple > months, and my background is in C++ rather than > Java. But I think I understand your question. > Basically, you're asking whether in Ruby you > would tend to have People inherit from Array or > contain an Array member. > > In my mind, the issue is whether you want to > expose *all* of the Array members as the > interface, or to create your own limited or > different interface. Do you want client code to > be able to sort your people at any time, for > example. Are you concerned that client code might > stuff a car into your list of people? > > If you want control over the interface, then you > would want to contain (aka delegate)*, not > inherit. That's as true in Ruby as it is in Java > or C++. Thus, I would tend to delegate when > creating a specific class like People, but would > inherit when creating a generic improvement on a > built-in class, like "StreamableHash". > > * = Note that my use of the word "delegate" here > is not related to the 'delegate' module that > ships with Ruby. > > Kevin