[Tutor] Mapping to an equivalent / similar object?
Alan Gauld
alan.gauld at btinternet.com
Fri May 29 03:27:02 CEST 2009
"Allen Fowler" <allen.fowler at yahoo.com> wrote
> Imagine the relationship between an Ice Cream retail
> store and the several manufactures that supply it's flavors.
> The store's ordering system takes as input a CanonicalFlavor('Vanilla')
> object that needs to be purchased, and must route this to a
> manufacturer's ordering system.
> The choice of which manufacture to use is based on external factors.
>
> However, each manufacturer's ordering system is different,
> and each expects a radically different variation of a Flavor object.
> (Different ingredients, parameters, etc. )
The difference between objects is measeured by their interfaces
not their data so provided the supplier objects all use the same
operations of a Flavor then its not too bad. You can either create
subclasses of Flavor for each manufacturer that retuirns the requisite
list of attributes (but requires ongoing changes for every new
manufacturer - eek!) or you can write a method of Flavor that takes
a list (or dictionary?) of required attributes and returns the values.
It can also raise exceptions if asked for unsupported attributes,
or return None, in which case the Supplier object provides a
suitable default.
This way you can limit the changes for a new supplier. You might
also be able to data drive the Supplier object to make the required
list of attributes configurable(via a database or file?). Then you
only need subclasses of Supplier for the actual interface types
(EDI, v FTP, v http v email etc).
> How should the mapping between the CanonicalFlavor('Vanilla')
> object and ManufAFlavor('Vanilla') / ManufBFlavor('Vanilla')
> objects be handled.
If the mapping is one to many I don;t think you need to maintain
it - at least not in the objects. Just pass the Flavor object to the
supplier object. Let the supplier query the Flavor for the data it
needs.
If the choice of supplier was driven by Flavor characteristics
then the selection method would be in the Flavour and then
you'd need to keep a list of suppliers in the Flavor. But since
the supplier is chosen from external factors the Flavor doesn't
need to know about its supplier. And the supplier only needs
to know about how to query a flavour.
HTH,
--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list