java:how to define capabilities of a subclass instance that can be derived from the superclass

StackOverflow https://stackoverflow.com/questions/5822358

  •  26-10-2019
  •  | 
  •  

سؤال

To make my question more concrete, let me pose it as a problem:

situation description:

We have 3 abstract concepts:

Boss:  which has a number of employees
Worker:  that can execute some types of tasks
Task:  contains the semantics needed for a worker to execute it

In the implementation/subtypes we have a number of different types of workers and different types of tasks. A specific type of worker can execute certain types of tasks (subset of all types)

problem to solve

Now the boss has a task of known type he wishes to see executed, he does not know the types of workers he has (only abstract type/interface). What kind of interface(s) could the worker implement/what could the boss do to find out?

ways to solve it that I can think of

I found this two ways, but there probably are other&better ways:

1) we make an class for every task type implementing the empty task interface give the worker an execute(task) function: in the implementation of execute(task) try to typecheck/cast task to all tasktypes that type of worker can perform. If none of the typechecks pass, we throw a taskNotSupportedException. the boss can now give tasks to workers until no exception is thrown.

2) we now do not have task classes but define an capability interface for every task type with the function dotaskType(taskinfo) a worker implementation can now implement capability interfaces dependant of what its capabilities are. the boss now can check until a worker with the proper capability is found (typechecking) and then give him the task knowing he can execute it.

I did not test 2 and I do not have that much experience in java but this should be possible (or something very similar).

I also prefer 2 over 1, because 1 seems improper (cascade of casts) and interfaces are a natural way to define what an instance of a class can do, also with interfaces you could group capabilities/create hierarchies. (also in my current implementation (1) the Task interface is empty, so tasks do not have much in common and info is passed through the constructor (which would be parameters of functions in method 2) and retrieved by gets.

I was wondering in what other way you would/could implement (adjusting 1 or 2?) this or which of the two you prefer and why. (efficiency is not important, modulation/abstraction/maintainability is!)

هل كانت مفيدة؟

المحلول

Don't use exceptions for this. Exceptions should be used for exceptional situations, and not being able to perform some task seems to be normal.

I would add a method

boolean isAbleToExecute(Task task)

in the Worker class, and have the boss iterate through his workers and find a worker able to execute the task before assigning it.

نصائح أخرى

Worker interface could include a method supportedTasks returning a list of tasks. Then you could maintain a Map, mapping task to list of workers supporting that task. Then your boss doesn't need to iterate, but can simply look up all workers that supports a given task.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top