Re: Help with class design
- From: "JSheble" <jsheble-NOSPAM@xxxxxxxxxxx>
- Date: Sun, 15 May 2005 15:49:12 -0700
Ok, so assuming a package cannot exist outside of a shipment, I would be
using the Composition type. In reality, a package could exist outside of a
shipment in the real world, but for this object model, it makes no sense to
have a package without a shipment.
But this line of pseudo-code you added:
PackageEnumerator GetPackageEnumerator()...
makes no sense to me. I'm assuming I will have to create a
PackageEnumerator type? Which should have or do what exactly?
I don't suppose you'd have a small example class showing or demonstrating
this, would you? Once I see and digest an example, hopefully I'd be able to
simulate it for my specific needs.
> 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.
Actually, a package cannot exist without a weight, which is why I'm adding
it to the Addpackage method. A package must have a package ID and a weight.
If not, there's no sense in adding a package. I think leaving the weight as
a parameter is a feasible solution.
"Joanna Carter (TeamB)" <joannac@xxxxxxxxxxxxxxx> wrote in message
news:%23HBXuKYWFHA.2540@xxxxxxxxxxxxxxxxxxxxxxx
> "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
>
>
.
- Follow-Ups:
- Re: Help with class design
- From: Joanna Carter \(TeamB\)
- Re: Help with class design
- References:
- Help with class design
- From: JSheble
- Re: Help with class design
- From: Joanna Carter \(TeamB\)
- Help with class design
- Prev by Date: Re: Why can't overloads take into account the return type.
- Next by Date: Re: Help with class design
- Previous by thread: Re: Help with class design
- Next by thread: Re: Help with class design
- Index(es):
Relevant Pages
|