Re: Help with class design



"JSheble" <jsheble-NOSPAM@xxxxxxxxxxx> a écrit dans le message de news:
#Kyv#uWWFHA.580@xxxxxxxxxxxxxxxxxxxxxxx

> I come from a Delphi background, and am currently trying to port or
convert
> some of our Delphi classes to C#. I've got a good handle on basic class
> design, but am a bit lost with some of the more advanced things I need to
> do, and was hoping somebody could point me to an online resource that can
> help me better understand what I need to do.

There really is no difference in how you would design classes in C# as
opposed to Delphi. If you got the design right in Delphi, then it should be
fine in C# :-)

> For example, one of the classes is a Shipment class. A Shipment can have
> any number of Packages, so I have designed a Shipment Class and a Package
> class. I should be able to retrieve any of the packages by it's index or
I
> should be able to use a single package as a property. For example (these
> examples are not working code, merely pseudo-code to demonstrate what I
> need):

You need to start off by asking yourself whether a Package can exist outside
of a Shipment e.g. can a Package be stored in a Warehouse or transported in
a DeliveryVehicle.

If a Package can exist outside of a Shipment then the relationship between
them is one of Aggregation and can be expressed thus :

class Package
{
...
}

class PackageList
{
void Add(Package item)...
void Remove(Package item)...
Package this[int idx]...
...
}

class Shipment
{
PackageList Packages
{
get {...}
set {...}
}
}

But if the Packages cannot exist outside of a Shipment then the relationship
is one of Composition and should be expressed thus :

class Package
{
...
}

class Shipment
{
Package AddPackage()...
void RemovePackage(Package item)...
int PackageCount
{
get {...}
}...
PackageEnumerator GetPackageEnumerator()...
...
}

The essential difference is that the Composition relationship should not
allow adding or otherwise manipulating Packages without going through
methods of the Shipment.

> oShip = new Shipment();
> oShip.AddPackage(sPackageID, dPackageWeight);
> oShip.AddPackage(sDifferentPackageID, dPackageWeight);

PAckageWeight should be a property of the Package class and you should not
need to pass such details to the Add method of the Shipment class. Assuming
you are using Composition, you should retrieve a new instance of the Package
class from the Shipment and set properties like Weight, etc on that
instance. I suspect though that you should be using Aggregation and
therefore you should create an instance of Package, set the properties
either in the constructor or otherwise, and then add the instance to the
Shipment.

> // loop though all packages
> foreach(Package oPack in oShipment)
> {
> MessageBox.Show(oPack.PackageID);
> }

I am not sure whether you really should make the Shipment 'enumerable', that
implies it not really much more than a list ??

Joanna

--
Joanna Carter (TeamB)
Consultant Software Engineer


.



Relevant Pages

  • Re: VisionLab 3.1 is available!
    ... VisionLab is a video capture, motion detection, and basic computer vision ... The package includes both VFW and DirectShow capture components, ... Delphi 5 ... Added number of new design editors. ...
    (borland.public.delphi.thirdpartytools.general)
  • ANN: VisionLab 3.1 is available!
    ... VisionLab is a video capture, motion detection, and basic computer vision package designed for security, and robotics type of applications. ... Delphi 5 ... Added number of new design editors. ...
    (borland.public.delphi.thirdpartytools.general)
  • Re: International Shipping
    ... >one letter cost you? ... US Shipment: ... click a few times, print lable, stick on box, go to post office, get ... We must deliver the package to teh postoffice ...
    (alt.marketing.online.ebay)
  • Almost done with the Poo
    ... attached an UPC sticker specifying the shipping zipcode to the 5/27 ... package. ... my shipping address in a database and indicated any shipment is good to ... Xiaguan special grade tuocha and a bag of green Tibetan mushrooms. ...
    (rec.food.drink.tea)
  • Re: Help with class design
    ... Ok, so assuming a package cannot exist outside of a shipment, I would be ... > you are using Composition, you should retrieve a new instance of the ... Actually, a package cannot exist without a weight, which is why I'm adding ...
    (microsoft.public.dotnet.languages.csharp)