Skip to content

Commit 2e98dcf

Browse files
committed
Update README.md
1 parent 9d44eea commit 2e98dcf

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

separated-interface/README.md

+31-12
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,35 @@ tags:
1010

1111

1212
## Intent
13-
Separate the interface definition and implementation in different packages. This allows the client to be completely unaware of the implementation.
13+
14+
Separate the interface definition and implementation in different packages. This allows the client
15+
to be completely unaware of the implementation.
1416

1517
## Explanation
1618

1719
Real world example
1820

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.
21+
> An Invoice generator may be created with ability to use different Tax calculators that may be
22+
> added in the invoice depending upon type of purchase, region etc.
2023
2124
In plain words
2225

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.
26+
> Separated interface pattern encourages to keep the implementations of an interface decoupled from
27+
> the client and its definition, so the client is not dependent on the implementation.
2428
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).
29+
A client code may abstract some specific functionality to an interface, and define the definition of
30+
the interface as an SPI ([Service Programming Interface](https://en.wikipedia.org/wiki/Service_provider_interface)
31+
is an API intended and open to be implemented or extended by a third party). Another package may
32+
implement this interface definition with a concrete logic, which will be injected into the client
33+
code at runtime (with a third class, injecting the implementation in the client) or at compile time
34+
(using Plugin pattern with some configurable file).
2635

2736
**Programmatic Example**
2837

29-
**Client** An Invoice generator class accepts the cost of the product and calculates the total amount payable inclusive of tax
38+
**Client**
39+
40+
`InvoiceGenerator` class accepts the cost of the product and calculates the total
41+
amount payable inclusive of tax.
3042

3143
```java
3244
public class InvoiceGenerator {
@@ -46,21 +58,23 @@ public class InvoiceGenerator {
4658

4759
}
4860
```
49-
The tax calculation logic is delegated to the ```TaxCalculator``` interface
5061

51-
```java
62+
The tax calculation logic is delegated to the `TaxCalculator` interface.
5263

64+
```java
5365
public interface TaxCalculator {
5466

5567
double calculate(double amount);
5668

5769
}
58-
5970
```
6071

6172
**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.
73+
74+
In another package (which the client is completely unaware of) there exist multiple implementations
75+
of the `TaxCalculator` interface. `ForeignTaxCalculator` is one of them which levies 60% tax
76+
for international products.
77+
6478
```java
6579
public class ForeignTaxCalculator implements TaxCalculator {
6680

@@ -74,7 +88,8 @@ public class ForeignTaxCalculator implements TaxCalculator {
7488
}
7589
```
7690

77-
```DomesticTaxCalculator``` which levies 20% tax for international products.
91+
Another is `DomesticTaxCalculator` which levies 20% tax for international products.
92+
7893
```java
7994
public class DomesticTaxCalculator implements TaxCalculator {
8095

@@ -88,7 +103,8 @@ public class DomesticTaxCalculator implements TaxCalculator {
88103
}
89104
```
90105

91-
These both implementations are instantiated and injected in the client class by the ```App.java``` class
106+
These both implementations are instantiated and injected in the client class by the ```App.java```
107+
class.
92108

93109
```java
94110
var internationalProductInvoice = new InvoiceGenerator(PRODUCT_COST, new ForeignTaxCalculator());
@@ -101,9 +117,11 @@ These both implementations are instantiated and injected in the client class by
101117
```
102118

103119
## Class diagram
120+
104121
![alt text](./etc/class_diagram.png "Separated Interface")
105122

106123
## Applicability
124+
107125
Use the Separated interface pattern when
108126

109127
* You are developing a framework package, and your framework needs to call some application code through interfaces.
@@ -117,3 +135,4 @@ Use the Separated interface pattern when
117135
## Credits
118136

119137
* [Martin Fowler](https://www.martinfowler.com/eaaCatalog/separatedInterface.html)
138+
* [Patterns of Enterprise Application Architecture](https://www.amazon.com/gp/product/0321127420/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=javadesignpat-20&creative=9325&linkCode=as2&creativeASIN=0321127420&linkId=e08dfb7f2cf6153542ef1b5a00b10abc)

0 commit comments

Comments
 (0)