Skip to content

Commit ddf20b8

Browse files
committed
docs: separated interface
1 parent 25d12fd commit ddf20b8

File tree

1 file changed

+77
-56
lines changed

1 file changed

+77
-56
lines changed

separated-interface/README.md

Lines changed: 77 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,31 @@ title: Separated Interface
33
category: Structural
44
language: en
55
tag:
6-
- Decoupling
6+
- API design
7+
- Decoupling
8+
- Interface
79
---
810

11+
## Also known as
12+
13+
* API Segregation
14+
* Client-Server Interface
915

1016
## Intent
1117

12-
Separate the interface definition and implementation in different packages. This allows the client
13-
to be completely unaware of the implementation.
18+
To define a client interface in a separate package from its implementation to allow for easier swapping of implementations and better separation of concerns.
1419

1520
## Explanation
1621

1722
Real world example
1823

19-
> An Invoice generator may be created with ability to use different Tax calculators that may be
20-
> added in the invoice depending upon type of purchase, region etc.
24+
> Consider a restaurant where the menu (interface) is separate from the kitchen operations (implementation).
25+
>
26+
> In this analogy, the menu lists the dishes customers can order, without detailing how they are prepared. Different restaurants (or even different chefs within the same restaurant) can use their own recipes and methods to prepare the dishes listed on the menu. This separation allows the restaurant to update its menu or change its chefs without disrupting the overall dining experience. Similarly, in software, the Separated Interface pattern decouples the interface from its implementation, allowing changes and variations in the implementation without affecting the client code that relies on the interface.
2127
2228
In plain words
2329

24-
> Separated interface pattern encourages to keep the implementations of an interface decoupled from
25-
> the client and its definition, so the client is not dependent on the implementation.
30+
> Defines a client interface separate from its implementation to allow for flexible and interchangeable components.
2631
2732
A client code may abstract some specific functionality to an interface, and define the definition of
2833
the interface as an SPI ([Service Programming Interface](https://en.wikipedia.org/wiki/Service_provider_interface)
@@ -33,104 +38,120 @@ code at runtime (with a third class, injecting the implementation in the client)
3338

3439
**Programmatic Example**
3540

36-
**Client**
37-
38-
`InvoiceGenerator` class accepts the cost of the product and calculates the total
39-
amount payable inclusive of tax.
40-
41-
```java
42-
public class InvoiceGenerator {
41+
The Separated Interface design pattern is a software design pattern that encourages the separation of the definition of an interface from its implementation. This allows the client to be completely unaware of the implementation, promoting loose coupling and enhancing flexibility.
4342

44-
private final TaxCalculator taxCalculator;
43+
In the given code, the `InvoiceGenerator` class is the client that uses the `TaxCalculator` interface to calculate tax. The `TaxCalculator` interface is implemented by two classes: `ForeignTaxCalculator` and `DomesticTaxCalculator`. These implementations are injected into the `InvoiceGenerator` class at runtime, demonstrating the Separated Interface pattern.
4544

46-
private final double amount;
45+
Let's break down the code:
4746

48-
public InvoiceGenerator(double amount, TaxCalculator taxCalculator) {
49-
this.amount = amount;
50-
this.taxCalculator = taxCalculator;
51-
}
52-
53-
public double getAmountWithTax() {
54-
return amount + taxCalculator.calculate(amount);
55-
}
56-
57-
}
58-
```
59-
60-
The tax calculation logic is delegated to the `TaxCalculator` interface.
47+
First, we have the `TaxCalculator` interface. This interface defines a single method `calculate` that takes an amount and returns the calculated tax.
6148

6249
```java
6350
public interface TaxCalculator {
64-
6551
double calculate(double amount);
66-
6752
}
6853
```
6954

70-
**Implementation package**
71-
72-
In another package (which the client is completely unaware of) there exist multiple implementations
73-
of the `TaxCalculator` interface. `ForeignTaxCalculator` is one of them which levies 60% tax
74-
for international products.
55+
Next, we have two classes `ForeignTaxCalculator` and `DomesticTaxCalculator` that implement the `TaxCalculator` interface. These classes provide the concrete logic for tax calculation.
7556

7657
```java
7758
public class ForeignTaxCalculator implements TaxCalculator {
78-
7959
public static final double TAX_PERCENTAGE = 60;
8060

8161
@Override
8262
public double calculate(double amount) {
8363
return amount * TAX_PERCENTAGE / 100.0;
8464
}
85-
8665
}
87-
```
88-
89-
Another is `DomesticTaxCalculator` which levies 20% tax for international products.
9066

91-
```java
9267
public class DomesticTaxCalculator implements TaxCalculator {
93-
9468
public static final double TAX_PERCENTAGE = 20;
9569

9670
@Override
9771
public double calculate(double amount) {
9872
return amount * TAX_PERCENTAGE / 100.0;
9973
}
74+
}
75+
```
76+
77+
The `InvoiceGenerator` class is the client that uses the `TaxCalculator` interface. It doesn't know about the concrete implementations of the `TaxCalculator` interface. It just knows that it has a `TaxCalculator` that can calculate tax.
78+
79+
```java
80+
public class InvoiceGenerator {
81+
private final TaxCalculator taxCalculator;
82+
private final double amount;
83+
84+
public InvoiceGenerator(double amount, TaxCalculator taxCalculator) {
85+
this.amount = amount;
86+
this.taxCalculator = taxCalculator;
87+
}
10088

89+
public double getAmountWithTax() {
90+
return amount + taxCalculator.calculate(amount);
91+
}
10192
}
10293
```
10394

104-
These both implementations are instantiated and injected in the client class by the ```App.java```
105-
class.
95+
Finally, in the `App` class, we create instances of `InvoiceGenerator` with different `TaxCalculator` implementations. This demonstrates how the Separated Interface pattern allows us to inject different implementations at runtime.
10696

10797
```java
108-
var internationalProductInvoice = new InvoiceGenerator(PRODUCT_COST, new ForeignTaxCalculator());
98+
public class App {
99+
public static final double PRODUCT_COST = 50.0;
109100

101+
public static void main(String[] args) {
102+
var internationalProductInvoice = new InvoiceGenerator(PRODUCT_COST, new ForeignTaxCalculator());
110103
LOGGER.info("Foreign Tax applied: {}", "" + internationalProductInvoice.getAmountWithTax());
111104

112105
var domesticProductInvoice = new InvoiceGenerator(PRODUCT_COST, new DomesticTaxCalculator());
113-
114106
LOGGER.info("Domestic Tax applied: {}", "" + domesticProductInvoice.getAmountWithTax());
107+
}
108+
}
115109
```
116110

111+
In this way, the Separated Interface pattern allows us to decouple the interface of a component from its implementation, enhancing flexibility and maintainability.
112+
117113
## Class diagram
118114

119-
![alt text](./etc/class_diagram.png "Separated Interface")
115+
![Separated Interface](./etc/class_diagram.png "Separated Interface")
120116

121117
## Applicability
122118

123-
Use the Separated interface pattern when
119+
* Use when you want to decouple the interface of a component from its implementation.
120+
* Useful in large systems where different teams work on different parts of the system.
121+
* Ideal when the implementation might change over time or vary between deployments.
122+
123+
## Tutorial
124+
125+
* [Separated Interface Tutorial - YouTube](https://www.youtube.com/watch?v=d3k-hOA7k2Y)
126+
127+
## Known Uses
128+
129+
* Java's JDBC (Java Database Connectivity) API separates the client interface from the database driver implementations.
130+
* Remote Method Invocation (RMI) in Java, where the client and server interfaces are defined separately from the implementations.
131+
132+
## Consequences
133+
134+
Benefits:
135+
136+
* Enhances flexibility by allowing multiple implementations to coexist.
137+
* Facilitates testing by allowing mock implementations.
138+
* Improves maintainability by isolating changes to specific parts of the code.
139+
140+
Trade-offs:
124141

125-
* You are developing a framework package, and your framework needs to call some application code through interfaces.
126-
* You have separate packages implementing the functionalities which may be plugged in your client code at runtime or compile-time.
127-
* 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.
142+
* Initial setup might be more complex.
143+
* May lead to increased number of classes and interfaces in the codebase.
128144

129-
## Tutorial
145+
## Related Patterns
130146

131-
* [Separated Interface Tutorial](https://www.youtube.com/watch?v=d3k-hOA7k2Y)
147+
* Adapter: Adapts one interface to another, which can be used alongside Separated Interface to integrate different implementations.
148+
* Dependency Injection: Often used to inject the implementation of a separated interface, promoting loose coupling.
149+
* Bridge: Separates an object’s interface from its implementation, similar to Separated Interface but usually applied to larger-scale architectural issues.
132150

133151
## Credits
134152

135-
* [Martin Fowler](https://www.martinfowler.com/eaaCatalog/separatedInterface.html)
136-
* [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)
153+
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3w0pvKI)
154+
* [Effective Java](https://amzn.to/4cGk2Jz)
155+
* [Pattern-Oriented Software Architecture Volume 1: A System of Patterns](https://amzn.to/3xZ1ELU)
156+
* [Patterns of Enterprise Application Architecture](https://amzn.to/3WfKBPR)
157+
* [Separated Interface - Martin Fowler](https://www.martinfowler.com/eaaCatalog/separatedInterface.html)

0 commit comments

Comments
 (0)