3

In my application , We are creating two types of callable object based on some condition.

We are executing all the callable objects using executer service from the main thread and updating the DB.

 public interface CallableTask<T> extends Callable<T>{

 }

 public class Task1 implements    CallableTask<TaskOutCome>{

 }

 public class Task2 implements    CallableTask<TaskOutCome>{

 }

Can we use factory design pattern to create callable objects and dispatcher design pattern to execute all callable object from the main thread.

Warren Dew
  • 8,790
  • 3
  • 30
  • 44
VIKASH SINHA
  • 250
  • 3
  • 17
  • 1
    [This](http://stackoverflow.com/a/17263854/4790490) might help you. – Hardik Pithva May 19 '16 at 12:53
  • 4
    "Can we..." Nobody's stopping you. Is there some reason why you have doubts? (i.e., what are you _really_ trying to ask here?) – Solomon Slow May 19 '16 at 13:04
  • So far you haven't indicated any reason for using the factory design pattern. Making a different kind of object depending on a condition is the sort of thing you can do with "if" statements – Matt Timmermans May 19 '16 at 13:11
  • 1
    I can do it with "If" statement, write everything in a single class[ it will also work]. Just wanted to know there is any design pattern to use in above scenario – VIKASH SINHA May 19 '16 at 13:41
  • This question asks about clear cut application of particular design patterns to a particular problem; there's very little opinion in the answers. I notice that none of those who voted to put it on hold actually articulated a reason for doing so in the comments. – Warren Dew May 23 '16 at 20:35

2 Answers2

1

You can certainly use factories to create the tasks. A dispatcher might not be appropriate for executing the tasks, because a dispatcher dispatches events to multiple observers. When the events are tasks, and if your observers were task executors, you might end up executing each task multiple times, which might not be what you want.

Warren Dew
  • 8,790
  • 3
  • 30
  • 44
1

Based what you've told us, you are going to be creating multiple tasks depending on a condition, and factory pattern is a great choice here because it allows you to isolate the decision making from the main logic. As an added bonus, you can later use dependency injection to make your code easier to test.

I'd say, go for it.

Slava Imeshev
  • 1,360
  • 10
  • 14