Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OOP: Which class should own a method? [closed]

I’m having trouble understanding how classes relate to their methods. Is a method something that the object does, or something that’s done to it? Or is this a different concept entirely?

Specifically, in a library’s software system, should the borrow() method belong to the class representing the library patron, or the class representing the item that the patron is borrowing? My intuition is that it should read like patron.borrow(copy), like English sentence structure, subject.verb(object); but my instructor says that’s Wrong, and I don’t understand why he would have borrow() belong to the Copy class (and he doesn’t really explain things too well). I’m not looking for justification, but can someone just explain the proper relationship?

Edit: This question was closed as “off topic”. I don’t understand. Are software design questions not appropriate for this site?

like image 823
Frungi Avatar asked Apr 26 '12 05:04

Frungi


People also ask

When should a method be private?

Private methods are typically used when several methods need to do the exact same work as part of their responsibility (like notifying external observers that the object has changed), or when a method is split in smaller steps for readability.

Can a method be defined outside a class?

Yes you can definitely have functions outside of a class.

Can object call private method?

Object users can't use private methods directly. The main reason to do this is to have internal methods that make a job easier.

Are class methods private by default?

Yes, it is private.


2 Answers

subjective :) but honestly, I'd go with the Information Expert Pattern and say something like

library.lend(item, patron)

The library contains the information about the items it has (perhaps in its catalog).
The library lends the item to the patron (which it knows because it registers them)

Not sure how your instructor sees this, but this is the level of 'abstraction' (software objects mimicking real world entities) that would make sense for your scenario.

like image 60
Ryan Fernandes Avatar answered Oct 29 '22 06:10

Ryan Fernandes


You should not confuse the idea of OOP with one specific incarnation like Java or C++.

This limit "methods are a property of the object" is not part of the OOP idea, but just of some implementations and as you discovered it doesn't scale well.

How many methods sould an "integer number" object have? What is more logical... myfile.write(myint) or myint.write(myfile)? There is really no good general answer to this. The idea of a method being part of a single object is a special case and sometimes the bending needed to fit the problem to this solution can become noticeable or even close to a showstopper. The answer is really totally acceptable only when a method has no parameters except the object being processed: single dispatch is a perfect answer only when there is a single type involved.

In other languages you have a separation between objects and methods, so for example you have the file object, the integer object and a method write(myfile, myint) that describes what to do when the operation is needed... and this method is neither part of the file nor of the integer.

like image 10
6502 Avatar answered Oct 29 '22 07:10

6502