java - Object-oriented design: Giving too much responsibility to a value object -
i came across design question in book cracking coding interview:
imagine have call center 3 levels of employees: fresher, technical lead (tl), product manager (pm). there can multiple employees, 1 tl or pm. incoming telephone call must allocated fresher free. if fresher can’t handle call, or must escalate call technical lead. if tl not free or not able handle it, call should escalated pm. design classes , data structures problem. implement method getcallhandler().
solution in book
public class callhandler { static final int levels = 3; // have 3 levels of employees static final int num_freshers = 5; // have 5 freshers arraylist<employee>[] employeelevels = new arraylist[levels]; // queues each call’s rank queue<call>[] callqueues = new linkedlist[levels]; public callhandler() { ... } employee getcallhandler(call call) { (int level = call.rank; level < levels - 1; level++) { arraylist<employee> employeelevel = employeelevels[level]; (employee emp : employeelevel) { if (emp.free) { return emp; } } } return null; } // routes call available employee, or adds queue void dispatchcall(call call) { // try route call employee minimal rank employee emp = getcallhandler(call); if (emp != null) { emp.receivecall(call); } else { // place call queue according rank callqueues[call.rank].add(call); } } void getnextcall(employee e) {...} // call e’s rank } class call { int rank = 0; // minimal rank of employee can handle call public void reply(string message) { ... } public void disconnect() { ... } } class employee { callhandler callhandler; int rank; // 0- fresher, 1 - technical lead, 2 - product manager boolean free; employee(int rank) { this.rank = rank; } void receivecall(call call) { ... } void callhandled(call call) { ... } // call complete void cannothandle(call call) { // escalate call call.rank = rank + 1; callhandler.dispatchcall(call); free = true; callhandler.getnextcall(this); // waiting call } } class fresher extends employee { public fresher() { super(0); } } class techlead extends employee { public techlead() { super(1); } } class productmanager extends employee { public productmanager() { super(2); } } this solution not satisfying because involves passing callhandler object employee. think employee should treated value object meaning job should holding data without being aware of entities contain real business logic (like callhandler). interested in finding out better ways there design this. come actionscript background , in use actionscript's event model send messages employee , listen them in callhandler.
there infinite number of ways design system (that's makes developing software fun) ways better others. provided answer not best works.
you have have way of employee escalate call doing somekind of callback callhandler. whether done passing around callhandler or having employees , callhandlers listening events both ideas. given solution simpler , therefore more easy understand target audience. event based solution more complicated, harder write more scalable , easier modify.
for example, if had add new feature kind of "overseer" monitor how employees resolve calls versus how many times escalate, easy write new event listener try shoe-horn new object between employee , callhandler.
basically, yes ideas better solution, both answer question.
Comments
Post a Comment