1

I was wondering whether it is possible to generalize the input and output part of a Java program via specific design pattern? I am looking for a way to use a pattern to add different types of implementation in the future. For example, let's say I have a method which computes something based on the input, and it works through Console. How can I generalize the input-output side of this application in a way that other implementation of input/output like GUI or web service can be added easily?

public int compute(int input){
   return input+2;
}
Ali
  • 1,759
  • 2
  • 32
  • 69
  • You mean something like [Function](https://docs.oracle.com/javase/8/docs/api/java/util/function/Function.html) and [BiFunction](https://docs.oracle.com/javase/8/docs/api/java/util/function/BiFunction.html)? – SME_Dev Feb 27 '17 at 12:40
  • @SME_Dev No, I am looking for a design pattern or class hierarchy to provide such a behavior. – Ali Feb 27 '17 at 12:49

2 Answers2

2

How can I generalize the input-output side of this application in a way that other implementation of input/output like GUI or web service can be added easily

Makes me think about Model View Controller, where you want to be able to change the 'view'? (view can also be webservice)

Wikipedia: MVC

rdhaese
  • 139
  • 8
0

Looks like a case for Strategy pattern for me, where interface InputOutputStrategy has methods input() and output(), which get and send data to the needed streams or to the UI in the concrete implementations.

Gyrotank
  • 153
  • 1
  • 2
  • 9