OOP responsibility assignment -
lets have 2 classes user & orders. should functionality of getuserorders (userid) implemented ? in user class or orders class ? should in user class orders being retrieved related user or should order class retrieving orders
it depends on oop design. key feature consider loose coupling.
it's bit difficult make determinant decision based on information have provided. however, let's take deeper @ 2 opposite scenarios.
scenario no.1: have user
class , order
class. using composition link between two. that's means user class has list (array, list, linked list, dictionary, associative array, or whatever) consists of order objects @ disposal. in case, prefer make function inside user class called: getorders
. retrieve of orders associated current user.
order not know user entity, , easier refactor things in future when user gets change. user knows order not vice versa. way kept on loosely coupled objects (at degree).
scenario no.2: have user
class , orders
class. orders class consists of multi-dimensional array in there user order matching. in case user class not know order. here better have getordersbyuser
in orders class. if refactor our order logic, there no need of touching user class, decoupled our orders.
furthermore, better make methods dependant on instantiation instead of being global hospitable (static). talking object oriented programming after all.
Comments
Post a Comment