Skip to Content.
Sympa Menu

patterns-discussion - [patterns-discussion] A question about Coin and Wallet

patterns-discussion AT lists.cs.illinois.edu

Subject: General talk about software patterns

List archive

[patterns-discussion] A question about Coin and Wallet


Chronological Thread 
  • From: "Yan, Hong [IT]" <hong.yan AT citigroup.com>
  • To: "Patterns Discussion" <patterns-discussion AT cs.uiuc.edu>
  • Cc: "Yan, Hong [IT]" <hong.yan AT citigroup.com>
  • Subject: [patterns-discussion] A question about Coin and Wallet
  • Date: Thu, 11 Sep 2003 17:11:25 -0400
  • List-archive: <http://mail.cs.uiuc.edu/pipermail/patterns-discussion/>
  • List-id: General talk about software patterns <patterns-discussion.cs.uiuc.edu>

Hi everyone:

I have a question that has been on my mind for a couple of days, and I
would like to hear your suggestions/comments. It is on Coin and Wallet,
which describes my wallet and the coins in it. And I am thinking in Java
language.

Please be a little patient -:)

(1) Just Ordinary Coins
Wallet is an object that contains a hashtable with instances of Coin in
it. And Coin class should have an interface like the following:

interface Circulateable{
int getFaceValue();
String getYearProduced();
boolean equals();
String hashCode();
}

and of course the class Coin implements Circulateable, and Comparable.
The latter requires that the Coin class implement method compareTo().

The equals method should be based on the face value of the coin, because
that's all I care for a generic coin. All quarters are the same to me,
so are all the dimes or pennies. This is ValueObject pattern.

So far, everything is fine.

(2) Collectible Coins
But then, I find two of my coins more valuable than their face values -
they are collectible coins because of mint defects.

I have never been so lucky, and I want to describe this, so I invented
another interface Collectiable:

interface Collectable{
int getFaceValue();
String getYearProduced();
int marketValue(); //note this is new
String describeDefect(); //note this is new
boolean equals();
String hashCode();
}

And I invent a CollectableCoin class to implement three interfaces:
Circulateable, Comparable and Collectable. This is good, because
collectible coins can be spent at face value just like any other coins.
They are special only when they are considered to be collectible, or in
OO terms, only when a CollectableCoin object is casted to Collectable
type.

Because Wallet is a wrapper of a hashtable object, I do not want to
rename equals/hashCode methods, because I do not know how to make
hashtable know about this renaming.

(3)
Because each defect coin is unique, the CollectableCoin class should
really implement equals/hashCode method in the way Object class does.
This is ReferenceObject pattern.

How do I implement ValueObject and ReferenceObject pattern in
CollectableCoin class at the same time?

(4)
Or maybe I should use different method names anyway, so that Collectable
interface becomes

interface Collectable{
int getFaceValue();
String getYearProduced();
int marketValue();
String describeDefect();
boolean equalsInMarketValue(); //changed
String hashCodeForMarketValue(); //changed
}

and invent another interface called

interface ComparableInMarket{
int compareToInMarkte(Collectable o); //changed
}

But then, because Wallet knows only hashCode/equals methods, it would
just keep the coins the same way as generic coins.

(5)
Maybe I am worrying too much - I have only one wallet anyway, and how
can I store coins differently when I have only one wallet? But how about
my having two wallets, one for orginary coins and the other one for
precious ones? I still have no way of changing the behavior of hashtable
in Java, so both wallets would be storing coins in the same way - as
defined by equals/hashCode methods.


(6)
I am too picky. Then disregard this posting -:)


Thanks and regards


Jeff





Archive powered by MHonArc 2.6.16.

Top of Page