Code
public interface INumberReader
{
void readNumbers();
}
SO we start off with the base INumberReader interface. This exposes one method to readNumbers. This allows for any application that wishes to be a NumberReader to specify how it will read these numbers, without any of its comsumers carryign about the implementation details. The idea is simple: someone wants you to read numbers, they don't care how. Just read them.
Code
public interface INumberReaderFactory
{
INumberReader createNumberReader(INumberReaderConfig config);
}
Next we will use a Factory pattern to generate the NumberReaders. This lets a consuming application create a new NumberReader without carrying about how. It isn't a NumberReader expert. It doesn't need to know how to make one, so we will supply it an interface that will expose a createNumberReader method so that they can get a NumberReader whenever they want without needing to know implementation details.
Code
import java.io.InputStream;
import java.io.PrintStream;
public interface INumberReaderConfig
{
Number getMaximumNumber();
Number getMinimumNumber();
int getAmountOfNumbers();
InputStream getInputer();
PrintStream getPrinter();
}
Now, there are some things a NumberReader needs. But all NumberReaders are not the same. So let's make those things configurable so that we can be more dynamic with how our NumberReader will work. The things a NumberReader needs in order to do its job is the range of numbers it can input, the amount of numbers it can input, and then of course HOW it is going to input. This means we can control whether they are inputed from the console, a file, or any datasource really. We can just give it a stream to the input or output source.
Code
import java.io.InputStream;
import java.io.PrintStream;
public class StaticDoubleReaderConstants
{
public static final Double MAXIMUM_NUMBER = Double.MAX_VALUE;
public static final Double MINIMUM_NUMBER = Double.MIN_VALUE;
public static final int AMOUNT_OF_NUMBERS = Integer.MAX_VALUE;
public static final InputStream INPUTER = System.in;
public static final PrintStream PRINTER = System.out;
}
Code
import java.io.InputStream;
import java.io.PrintStream;
public class StaticDoubleReaderConfig implements INumberReaderConfig
{
public Number getMaximumNumber() { return StaticDoubleReaderConstants.MAXIMUM_NUMBER; }
public Number getMinimumNumber() { return StaticDoubleReaderConstants.MINIMUM_NUMBER; }
public int getAmountOfNumbers() { return 2; }
public InputStream getInputer() { return StaticDoubleReaderConstants.INPUTER; }
public PrintStream getPrinter() { return StaticDoubleReaderConstants.PRINTER; }
}
Next we have the implementation of the NumberReaderConfig. This one is for a DoubleReader, but not just a DoubleReader, these is for a StaticDoubleReader. All of these values are static and not changed. If we ever want to change them, we just need to update the constants in the constants class.
To be continued.