OOP Question
-
Hi i had a discussion with my friend about OOP and now i want to ask some of you. The question is: Knows a file how it can be printed or knows a printer how it can print a file ? or Knows a car how it can be repaired or knows a car repair shop how it can repair a car? or Knows a clothes how it can be washed or knows a washmaschine how it can wash a clothes or generally when have a class a specific ability on its own and when have only another class a specific ability and need another class to do it according to a example above: File.Print( ) or Printer.Print( File ) ? Car.Repair( ) or CarRepairShop.Repair( Car ) ? Clothes.Wash( ) or WashMaschine.Wash( Clothes ) ?
-
Hi i had a discussion with my friend about OOP and now i want to ask some of you. The question is: Knows a file how it can be printed or knows a printer how it can print a file ? or Knows a car how it can be repaired or knows a car repair shop how it can repair a car? or Knows a clothes how it can be washed or knows a washmaschine how it can wash a clothes or generally when have a class a specific ability on its own and when have only another class a specific ability and need another class to do it according to a example above: File.Print( ) or Printer.Print( File ) ? Car.Repair( ) or CarRepairShop.Repair( Car ) ? Clothes.Wash( ) or WashMaschine.Wash( Clothes ) ?
MarkPhB wrote:
File.Print( ) or Printer.Print( File ) ? Car.Repair( ) or CarRepairShop.Repair( Car ) ? Clothes.Wash( ) or WashMaschine.Wash( Clothes ) ?
My opinion: - neither, usually a printer doesn't care about the document being sent to him, you'd have to convert any object (file, bitmap, text) to a format the printer can actually print (raw bitmap e.g.) - CarRepairShop.Repair( Car ) - WashMaschine.Wash( Clothes ) Objects like files, cars, clothes etc. have some properties on their own, but they usually don't have any abilities. A car probably doesn't know how to repair itself, but due to some given properties it may know how it should be accelerated, so Car.Accelerate() might be okay in some circumstances. regards
-
MarkPhB wrote:
File.Print( ) or Printer.Print( File ) ? Car.Repair( ) or CarRepairShop.Repair( Car ) ? Clothes.Wash( ) or WashMaschine.Wash( Clothes ) ?
My opinion: - neither, usually a printer doesn't care about the document being sent to him, you'd have to convert any object (file, bitmap, text) to a format the printer can actually print (raw bitmap e.g.) - CarRepairShop.Repair( Car ) - WashMaschine.Wash( Clothes ) Objects like files, cars, clothes etc. have some properties on their own, but they usually don't have any abilities. A car probably doesn't know how to repair itself, but due to some given properties it may know how it should be accelerated, so Car.Accelerate() might be okay in some circumstances. regards
-
yea thats my opinion too but my friend say: "Only the class knows what it can be done with it" for his example: "An object knows how it can be saved in a database". So he means: Object.Save() and not Database.Save( Object )
Then you, me and the .NET framework disagree with your friend ;) Object.Save() makes no sense because where should the object save itself to? A database connection, a file system, over network? The object doesn't know about either of the available streams. Also, saving an object depends on the place being stored to. For a database it would be a SQLConnection, for the hard disk it would be a FileStream, for network a NetworkStream. So only the streams themselves know how to execute the proper Save() operation.
-
Then you, me and the .NET framework disagree with your friend ;) Object.Save() makes no sense because where should the object save itself to? A database connection, a file system, over network? The object doesn't know about either of the available streams. Also, saving an object depends on the place being stored to. For a database it would be a SQLConnection, for the hard disk it would be a FileStream, for network a NetworkStream. So only the streams themselves know how to execute the proper Save() operation.
-
Hi i had a discussion with my friend about OOP and now i want to ask some of you. The question is: Knows a file how it can be printed or knows a printer how it can print a file ? or Knows a car how it can be repaired or knows a car repair shop how it can repair a car? or Knows a clothes how it can be washed or knows a washmaschine how it can wash a clothes or generally when have a class a specific ability on its own and when have only another class a specific ability and need another class to do it according to a example above: File.Print( ) or Printer.Print( File ) ? Car.Repair( ) or CarRepairShop.Repair( Car ) ? Clothes.Wash( ) or WashMaschine.Wash( Clothes ) ?
Your friend's ideas are not really OOP, because he wants to create monolithic classes that know how to do every aspect of any operation that involves them.
Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )