Skip to content

Commit ebc0e8b

Browse files
authored
Merge pull request iluwatar#1522 from swarajsaaj/master
iluwatar#1313 Add Separated Interface pattern
2 parents c8f7a8f + 9088ac5 commit ebc0e8b

14 files changed

Lines changed: 621 additions & 0 deletions

File tree

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@
196196
<module>transaction-script</module>
197197
<module>filterer</module>
198198
<module>factory</module>
199+
<module>separated-interface</module>
199200
</modules>
200201

201202
<repositories>

separated-interface/README.md

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
---
2+
layout: pattern
3+
title: Separated Interface
4+
folder: separated-interface
5+
permalink: /patterns/separated-interface/
6+
categories: Structural
7+
tags:
8+
- Decoupling
9+
---
10+
11+
12+
## Intent
13+
Separate the interface definition and implementation in different packages. This allows the client to be completely unaware of the implementation.
14+
15+
## Explanation
16+
17+
Real world example
18+
19+
> An Invoice generator may be created with ability to use different Tax calculators that may be added in the invoice depending upon type of purchase, region etc.
20+
21+
In plain words
22+
23+
> Separated interface pattern encourages to keep the implementations of an interface decoupled from the client and its definition, so the client is not dependent on the implementation.
24+
25+
A client code may abstract some specific functionality to an interface, and define the definition of the interface as an SPI ([Service Programming Interface](https://en.wikipedia.org/wiki/Service_provider_interface) is an API intended and open to be implemented or extended by a third party). Another package may implement this interface definition with a concrete logic, which will be injected into the client code at runtime (with a third class, injecting the implementation in the client) or at compile time (using Plugin pattern with some configurable file).
26+
27+
**Programmatic Example**
28+
29+
**Client** An Invoice generator class accepts the cost of the product and calculates the total amount payable inclusive of tax
30+
31+
```java
32+
public class InvoiceGenerator {
33+
34+
private final TaxCalculator taxCalculator;
35+
36+
private final double amount;
37+
38+
public InvoiceGenerator(double amount, TaxCalculator taxCalculator) {
39+
this.amount = amount;
40+
this.taxCalculator = taxCalculator;
41+
}
42+
43+
public double getAmountWithTax() {
44+
return amount + taxCalculator.calculate(amount);
45+
}
46+
47+
}
48+
```
49+
The tax calculation logic is delegated to the ```TaxCalculator``` interface
50+
51+
```java
52+
53+
public interface TaxCalculator {
54+
55+
double calculate(double amount);
56+
57+
}
58+
59+
```
60+
61+
**Implementation package**
62+
In another package (which the client is completely unaware of) there exist multiple implementations of the ```TaxCalculator``` interface
63+
```ForeignTaxCalculator``` which levies 60% tax for international products.
64+
```java
65+
public class ForeignTaxCalculator implements TaxCalculator {
66+
67+
public static final double TAX_PERCENTAGE = 60;
68+
69+
@Override
70+
public double calculate(double amount) {
71+
return amount * TAX_PERCENTAGE / 100.0;
72+
}
73+
74+
}
75+
```
76+
77+
```DomesticTaxCalculator``` which levies 20% tax for international products.
78+
```java
79+
public class DomesticTaxCalculator implements TaxCalculator {
80+
81+
public static final double TAX_PERCENTAGE = 20;
82+
83+
@Override
84+
public double calculate(double amount) {
85+
return amount * TAX_PERCENTAGE / 100.0;
86+
}
87+
88+
}
89+
```
90+
91+
These both implementations are instantiated and injected in the client class by the ```App.java``` class
92+
93+
```java
94+
var internationalProductInvoice = new InvoiceGenerator(PRODUCT_COST, new ForeignTaxCalculator());
95+
96+
LOGGER.info("Foreign Tax applied: {}", "" + internationalProductInvoice.getAmountWithTax());
97+
98+
var domesticProductInvoice = new InvoiceGenerator(PRODUCT_COST, new DomesticTaxCalculator());
99+
100+
LOGGER.info("Domestic Tax applied: {}", "" + domesticProductInvoice.getAmountWithTax());
101+
```
102+
103+
## Class diagram
104+
![alt text](./etc/class_diagram.png "Separated Interface")
105+
106+
## Applicability
107+
Use the Separated interface pattern when
108+
109+
* You are developing a framework package, and your framework needs to call some application code through interfaces.
110+
* You have separate packages implementing the functionalities which may be plugged in your client code at runtime or compile-time.
111+
* Your code resides in a layer that is not allowed to call the interface implementation layer by rule. For example, a domain layer needs to call a data mapper.
112+
113+
## Tutorial
114+
115+
* [Separated Interface Tutorial](https://www.youtube.com/watch?v=d3k-hOA7k2Y)
116+
117+
## Credits
118+
119+
* [Martin Fowler](https://www.martinfowler.com/eaaCatalog/separatedInterface.html)
32.2 KB
Loading
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
@startuml
2+
package com.iluwatar.separatedinterface {
3+
class App {
4+
- LOGGER : Logger {static}
5+
+ PRODUCT_COST : double {static}
6+
+ App()
7+
+ main(args : String[]) {static}
8+
}
9+
}
10+
package com.iluwatar.separatedinterface.taxes {
11+
class DomesticTaxCalculator {
12+
+ TAX_PERCENTAGE : double {static}
13+
+ DomesticTaxCalculator()
14+
+ calculate(amount : double) : double
15+
}
16+
class ForeignTaxCalculator {
17+
+ TAX_PERCENTAGE : double {static}
18+
+ ForeignTaxCalculator()
19+
+ calculate(amount : double) : double
20+
}
21+
}
22+
package com.iluwatar.separatedinterface.invoice {
23+
class InvoiceGenerator {
24+
- amount : double
25+
- taxCalculator : TaxCalculator
26+
+ InvoiceGenerator(amount : double, taxCalculator : TaxCalculator)
27+
+ getAmountWithTax() : double
28+
}
29+
interface TaxCalculator {
30+
+ calculate(double) : double {abstract}
31+
}
32+
}
33+
InvoiceGenerator --> "-taxCalculator" TaxCalculator
34+
DomesticTaxCalculator ..|> TaxCalculator
35+
ForeignTaxCalculator ..|> TaxCalculator
36+
@enduml

separated-interface/pom.xml

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
4+
The MIT License
5+
Copyright © 2014-2019 Ilkka Seppälä
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
25+
-->
26+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
27+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
28+
<modelVersion>4.0.0</modelVersion>
29+
<parent>
30+
<groupId>com.iluwatar</groupId>
31+
<artifactId>java-design-patterns</artifactId>
32+
<version>1.24.0-SNAPSHOT</version>
33+
</parent>
34+
<artifactId>separated-interface</artifactId>
35+
<dependencies>
36+
<dependency>
37+
<groupId>org.junit.jupiter</groupId>
38+
<artifactId>junit-jupiter-engine</artifactId>
39+
<scope>test</scope>
40+
</dependency>
41+
<dependency>
42+
<groupId>org.junit.jupiter</groupId>
43+
<artifactId>junit-jupiter-params</artifactId>
44+
<scope>test</scope>
45+
</dependency>
46+
<dependency>
47+
<groupId>org.mockito</groupId>
48+
<artifactId>mockito-core</artifactId>
49+
<scope>test</scope>
50+
</dependency>
51+
</dependencies>
52+
<build>
53+
<plugins>
54+
<plugin>
55+
<groupId>org.apache.maven.plugins</groupId>
56+
<artifactId>maven-assembly-plugin</artifactId>
57+
<executions>
58+
<execution>
59+
<configuration>
60+
<archive>
61+
<manifest>
62+
<mainClass>com.iluwatar.separatedinterface.App</mainClass>
63+
</manifest>
64+
</archive>
65+
</configuration>
66+
</execution>
67+
</executions>
68+
</plugin>
69+
</plugins>
70+
</build>
71+
</project>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* The MIT License
3+
* Copyright © 2014-2019 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package com.iluwatar.separatedinterface;
25+
26+
import com.iluwatar.separatedinterface.invoice.InvoiceGenerator;
27+
import com.iluwatar.separatedinterface.taxes.DomesticTaxCalculator;
28+
import com.iluwatar.separatedinterface.taxes.ForeignTaxCalculator;
29+
import org.slf4j.Logger;
30+
import org.slf4j.LoggerFactory;
31+
32+
/**
33+
* <p>The Separated Interface pattern encourages to separate the interface definition and
34+
* implementation in different packages. This allows the client to be completely unaware of the
35+
* implementation.</p>
36+
*
37+
* <p>In this class the {@link InvoiceGenerator} class is injected with different instances of
38+
* {@link com.iluwatar.separatedinterface.invoice.TaxCalculator} implementations located in separate
39+
* packages, to receive different responses for both of the implementations.</p>
40+
*/
41+
public class App {
42+
43+
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
44+
45+
public static final double PRODUCT_COST = 50.0;
46+
47+
/**
48+
* Program entry point.
49+
*
50+
* @param args command line args
51+
*/
52+
public static void main(String[] args) {
53+
//Create the invoice generator with product cost as 50 and foreign product tax
54+
var internationalProductInvoice = new InvoiceGenerator(PRODUCT_COST,
55+
new ForeignTaxCalculator());
56+
LOGGER.info("Foreign Tax applied: {}", "" + internationalProductInvoice.getAmountWithTax());
57+
58+
//Create the invoice generator with product cost as 50 and domestic product tax
59+
var domesticProductInvoice = new InvoiceGenerator(PRODUCT_COST, new DomesticTaxCalculator());
60+
LOGGER.info("Domestic Tax applied: {}", "" + domesticProductInvoice.getAmountWithTax());
61+
}
62+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* The MIT License
3+
* Copyright © 2014-2019 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package com.iluwatar.separatedinterface.invoice;
25+
26+
/**
27+
* InvoiceGenerator class generates an invoice, accepting the product cost and calculating the total
28+
* price payable inclusive tax (calculated by {@link TaxCalculator}).
29+
*/
30+
public class InvoiceGenerator {
31+
32+
/**
33+
* The TaxCalculator interface to calculate the payable tax.
34+
*/
35+
private final TaxCalculator taxCalculator;
36+
37+
/**
38+
* The base product amount without tax.
39+
*/
40+
private final double amount;
41+
42+
public InvoiceGenerator(double amount, TaxCalculator taxCalculator) {
43+
this.amount = amount;
44+
this.taxCalculator = taxCalculator;
45+
}
46+
47+
public double getAmountWithTax() {
48+
return amount + taxCalculator.calculate(amount);
49+
}
50+
51+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* The MIT License
3+
* Copyright © 2014-2019 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package com.iluwatar.separatedinterface.invoice;
25+
26+
public interface TaxCalculator {
27+
28+
double calculate(double amount);
29+
30+
}

0 commit comments

Comments
 (0)