The Design of Software (CLOSED)A public forum for discussing the design of software, from the user interface to the code architecture. Now closed. |
||
The "Design of Software" discussion group has been merged with the main
Joel on Software discussion group.
The archives will remain online indefinitely. |
Hi,
I want to know how an object of a clas must be constructed which has some invariants?For example,itinerary specification is a class which when constructed..number of days must be > 0,client name should not be empty..now i normally hav done this was.. ItinearaSpec it=new ItinearaSpec it.noofdays and it.clientname(both are set methods of properties,where i check if data is valid)eg like ItineraryDays { set{check if data is valid..if not then a boolean variable is set to true which indicates an invalid state and an event is raised to client} } But, Now i have been reading about immutable objects..and design by contract..i need some practical suggestions,and options? thks vishy
well if you are using C++ this is how you do it:
-decide what invariants you have -make those variables private -write a constructor for your class -make sure that by the end of your constructor all invariants are satisfied -create "set" methods that maintain the invariant The key to success is establishing the invariant in the constructors
Tom Tuesday, May 09, 2006
but if someone passes a value which is not satisfactory..what should i do in the constructor..raise an exception..or use a property to say invalid state..basically object should not be created..so shouldnt it be checked before calling a contructor..maybe a factory object..which will check if invariant criteria is met..and then create object??
|
Powered by FogBugz