develooper Front page | perl.moose | Postings from November 2011

Re: hash of objects inside an instance

Thread Previous | Thread Next
From:
Lars Balker Rasmussen
Date:
November 28, 2011 08:47
Subject:
Re: hash of objects inside an instance
Message ID:
CAB-_BaRggtJAPn0paM90v6O-EbpwtT6SgKvjd30-=g+XCUyp5Q@mail.gmail.com
On Mon, Nov 28, 2011 at 5:08 PM, Erik Colson <eco@ecocode.net> wrote:

> I have two classes : Employee and Department
> every Employee works for at least one department. Sometimes the cost is
> distributed over 2 or more departments. So I'd want a hashref inside the
> Employee in which the keys are Department instances and the value a
> percentage to be attributed to that Department.
>

No, what you want is an abstraction.

Basically you're trying to hack in something that's actually
business-information connecting several objects.

In relational databases, you know it'd be wrong to do something like this
in the employee table; the principle is much the same here.

Maybe add an extra class WorksFor[1] containing the cost and department,
and have a list of WorksFor in employee. (Warning, very little thought went
into this and all my brain-blood is occupied around full stomach, but it
solves the basic problem :) )

PS: When you're exposing hash/array-references to users of your class,
you're begging for problems down the road - always[2] encapsulate
implementation.

[1] I suck at naming
[2] Always doesn't always mean always.
-- 
Lars Balker Rasmussen                                        Consult::Perl


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About