Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB OOD question

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I want to know what the ‘best of practices’ as to what and how objects attributes are initiation and population.

Let use the example of a CAR as we ALL know what one is.

Option #1
Instantiate an instance of the car object….

Dim oCar as New Car

Fetch the one I want..

oCar.Fetch (83726)

Should car ID number 83726 not exist I still have an EMPTY car object.

Option #2.

Instantiate the application server.

Dim oAppServer as New AppServer

Instantiate an instance of the car object….

Dim oCar as Car

Fetch the one I want..

oCar = oAppServer.FetchCar (83726)

Should car ID number 83726 not exist the car object is = Nothing.

And how about a collection of cars

oCar = oAppServer.FetchCars (RED)

Your comments?
 
I think it depends on what you want to do next. If you just want to see if the car exists, you can program an isCarAvailable method on your collection class or appserver. You may also have the collection class or appserver raise an error and trap it (is it an error when the car is not available?). Or test for 'Nothingness' as you described.

It may well be that there are more steps to be taken when the car is not available. In that case, it may be wise to generate a defaultCar as a result of the retreival. For example in this situation:

A customer want to buy a car. He points at the red one that he is standing at and says to the salesman: "I want that car". The salesman (your appserver or collection class) then says: This one is already sold, but I can order a new one. You can decide what color it has, etc.

The nice thing in this case is that the testing for nothinness is inside the collection class. Instead of failing or passing the wrong objects ('Nothing' is a wrong object here), it corrects the failure and passes what you really want: a car.

Hope this helps or amuses,

Willem Bogaerts
 
You need both options.

Option #1: Create method allows manipulation of the properties to specialize the unit creating a new unit.

Option #2: Copy method allowing the unit to duplicate an existing similar object.

The difference between the two is subtile. The main difference is the source. Copy requires an object of the same type or base type, and produces a new object. Create produces the same results but is less critical of the source.

As Willem pointed out, how you control these two operations in your model is up to you. If consumers are only able to access a pre-created unit, you need to block the create method from them.

In your car example, a specialize copy method actualy serves better. That specialization adds a delete original to the process, AKA Move.


Wil Mead
wmead@optonline.net

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top