The subject is example of adapter pattern.
When you need to a different service as like existing servisce behaviour, you can use adapter pattern.
This example, you use sum service, but you want to use multiply service. However you say your service contract is sum service. To use multiply service, you use a multiply service adapter. This adapter implements sum service but uses multiply service inside. Example code is below.
Output:
8
after adapter
15
When you need to a different service as like existing servisce behaviour, you can use adapter pattern.
This example, you use sum service, but you want to use multiply service. However you say your service contract is sum service. To use multiply service, you use a multiply service adapter. This adapter implements sum service but uses multiply service inside. Example code is below.
package com.design.pattern.adapter; public class AdapterPatternSumMultiply { public static void main(String[] args) { User user = new AdapterPatternSumMultiply().new User(); SumService service; MultiplyService multiplyService = new AdapterPatternSumMultiply().new MultiplyServiceImpl(); service = new AdapterPatternSumMultiply().new SumServiceImpl(); user.use(service, 3, 5); System.out.println("after adapter"); service = new AdapterPatternSumMultiply().new MultiplyAdapter(multiplyService); user.use(service, 3, 5); } class User { public void use(SumService service, int a, int b) { int x = service.getTotal(a, b); System.out.println(x); } } interface SumService { int getTotal(int a, int b); } class SumServiceImpl implements SumService { @Override public int getTotal(int a, int b) { return a + b; } } interface MultiplyService { int getProduct(int a, int b); } class MultiplyServiceImpl implements MultiplyService { @Override public int getProduct(int a, int b) { return a * b; } } class MultiplyAdapter implements SumService { private MultiplyService multiplyService; public MultiplyAdapter(MultiplyService multiplyService) { this.multiplyService = multiplyService; } @Override public int getTotal(int a, int b) { return multiplyService.getProduct(a, b); } } }
Output:
8
after adapter
15