You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: separated-interface/README.md
+31-12
Original file line number
Diff line number
Diff line change
@@ -10,23 +10,35 @@ tags:
10
10
11
11
12
12
## 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.
14
16
15
17
## Explanation
16
18
17
19
Real world example
18
20
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.
20
23
21
24
In plain words
22
25
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.
24
28
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).
26
35
27
36
**Programmatic Example**
28
37
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.
30
42
31
43
```java
32
44
publicclassInvoiceGenerator {
@@ -46,21 +58,23 @@ public class InvoiceGenerator {
46
58
47
59
}
48
60
```
49
-
The tax calculation logic is delegated to the ```TaxCalculator``` interface
50
61
51
-
```java
62
+
The tax calculation logic is delegated to the `TaxCalculator` interface.
52
63
64
+
```java
53
65
publicinterfaceTaxCalculator {
54
66
55
67
doublecalculate(doubleamount);
56
68
57
69
}
58
-
59
70
```
60
71
61
72
**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
*[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