June 2018
Beginner
722 pages
18h 47m
English
The purpose of an object factory is to hide the details of object creation so that the client does not need to change the code if the implementation changes. Let's create a factory that produces Calculator objects. We will put it in the same package, com.packt.javapath.ch06demo.api.impl, as the implementations of the Calculator interface reside in:

And we can change the test (client code) to use this factory:
@DisplayName("API Calculator tests")public class CalculatorTest { @Test @DisplayName("Happy multiplyByTwo()") void multiplyByTwo(){ Calculator calculator = CalculatorFactory.createInstance(); int i = 2; int result = calculator.multiplyByTwo(i) ...
Read now
Unlock full access