#1313 Rename DomesticTax,ForeignTax and review fixes
This commit is contained in:
parent
a2967c5a40
commit
9088ac51f6
@ -3,7 +3,7 @@ layout: pattern
|
|||||||
title: Separated Interface
|
title: Separated Interface
|
||||||
folder: separated-interface
|
folder: separated-interface
|
||||||
permalink: /patterns/separated-interface/
|
permalink: /patterns/separated-interface/
|
||||||
categories: Architectural
|
categories: Structural
|
||||||
tags:
|
tags:
|
||||||
- Decoupling
|
- Decoupling
|
||||||
---
|
---
|
||||||
@ -22,7 +22,7 @@ In plain words
|
|||||||
|
|
||||||
> 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.
|
> 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.
|
||||||
|
|
||||||
A client code may abstract some specific functionality to an interface, and define the definition of the interface as an SPI. 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).
|
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).
|
||||||
|
|
||||||
**Programmatic Example**
|
**Programmatic Example**
|
||||||
|
|
||||||
@ -60,9 +60,9 @@ public interface TaxCalculator {
|
|||||||
|
|
||||||
**Implementation package**
|
**Implementation package**
|
||||||
In another package (which the client is completely unaware of) there exist multiple implementations of the ```TaxCalculator``` interface
|
In another package (which the client is completely unaware of) there exist multiple implementations of the ```TaxCalculator``` interface
|
||||||
```ForeignTax``` which levies 60% tax for international products.
|
```ForeignTaxCalculator``` which levies 60% tax for international products.
|
||||||
```java
|
```java
|
||||||
public class ForeignTax implements TaxCalculator {
|
public class ForeignTaxCalculator implements TaxCalculator {
|
||||||
|
|
||||||
public static final double TAX_PERCENTAGE = 60;
|
public static final double TAX_PERCENTAGE = 60;
|
||||||
|
|
||||||
@ -74,9 +74,9 @@ public class ForeignTax implements TaxCalculator {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
```DomesticTax``` which levies 20% tax for international products.
|
```DomesticTaxCalculator``` which levies 20% tax for international products.
|
||||||
```java
|
```java
|
||||||
public class DomesticTax implements TaxCalculator {
|
public class DomesticTaxCalculator implements TaxCalculator {
|
||||||
|
|
||||||
public static final double TAX_PERCENTAGE = 20;
|
public static final double TAX_PERCENTAGE = 20;
|
||||||
|
|
||||||
@ -91,11 +91,11 @@ public class DomesticTax implements TaxCalculator {
|
|||||||
These both implementations are instantiated and injected in the client class by the ```App.java``` class
|
These both implementations are instantiated and injected in the client class by the ```App.java``` class
|
||||||
|
|
||||||
```java
|
```java
|
||||||
var internationalProductInvoice = new InvoiceGenerator(PRODUCT_COST, new ForeignTax());
|
var internationalProductInvoice = new InvoiceGenerator(PRODUCT_COST, new ForeignTaxCalculator());
|
||||||
|
|
||||||
LOGGER.info("Foreign Tax applied: {}", "" + internationalProductInvoice.getAmountWithTax());
|
LOGGER.info("Foreign Tax applied: {}", "" + internationalProductInvoice.getAmountWithTax());
|
||||||
|
|
||||||
var domesticProductInvoice = new InvoiceGenerator(PRODUCT_COST, new DomesticTax());
|
var domesticProductInvoice = new InvoiceGenerator(PRODUCT_COST, new DomesticTaxCalculator());
|
||||||
|
|
||||||
LOGGER.info("Domestic Tax applied: {}", "" + domesticProductInvoice.getAmountWithTax());
|
LOGGER.info("Domestic Tax applied: {}", "" + domesticProductInvoice.getAmountWithTax());
|
||||||
```
|
```
|
||||||
|
Binary file not shown.
Before Width: | Height: | Size: 32 KiB After Width: | Height: | Size: 32 KiB |
@ -8,14 +8,14 @@ package com.iluwatar.separatedinterface {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
package com.iluwatar.separatedinterface.taxes {
|
package com.iluwatar.separatedinterface.taxes {
|
||||||
class DomesticTax {
|
class DomesticTaxCalculator {
|
||||||
+ TAX_PERCENTAGE : double {static}
|
+ TAX_PERCENTAGE : double {static}
|
||||||
+ DomesticTax()
|
+ DomesticTaxCalculator()
|
||||||
+ calculate(amount : double) : double
|
+ calculate(amount : double) : double
|
||||||
}
|
}
|
||||||
class ForeignTax {
|
class ForeignTaxCalculator {
|
||||||
+ TAX_PERCENTAGE : double {static}
|
+ TAX_PERCENTAGE : double {static}
|
||||||
+ ForeignTax()
|
+ ForeignTaxCalculator()
|
||||||
+ calculate(amount : double) : double
|
+ calculate(amount : double) : double
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -31,6 +31,6 @@ package com.iluwatar.separatedinterface.invoice {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
InvoiceGenerator --> "-taxCalculator" TaxCalculator
|
InvoiceGenerator --> "-taxCalculator" TaxCalculator
|
||||||
DomesticTax ..|> TaxCalculator
|
DomesticTaxCalculator ..|> TaxCalculator
|
||||||
ForeignTax ..|> TaxCalculator
|
ForeignTaxCalculator ..|> TaxCalculator
|
||||||
@enduml
|
@enduml
|
@ -24,8 +24,8 @@
|
|||||||
package com.iluwatar.separatedinterface;
|
package com.iluwatar.separatedinterface;
|
||||||
|
|
||||||
import com.iluwatar.separatedinterface.invoice.InvoiceGenerator;
|
import com.iluwatar.separatedinterface.invoice.InvoiceGenerator;
|
||||||
import com.iluwatar.separatedinterface.taxes.DomesticTax;
|
import com.iluwatar.separatedinterface.taxes.DomesticTaxCalculator;
|
||||||
import com.iluwatar.separatedinterface.taxes.ForeignTax;
|
import com.iluwatar.separatedinterface.taxes.ForeignTaxCalculator;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@ -51,11 +51,12 @@ public class App {
|
|||||||
*/
|
*/
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
//Create the invoice generator with product cost as 50 and foreign product tax
|
//Create the invoice generator with product cost as 50 and foreign product tax
|
||||||
var internationalProductInvoice = new InvoiceGenerator(PRODUCT_COST, new ForeignTax());
|
var internationalProductInvoice = new InvoiceGenerator(PRODUCT_COST,
|
||||||
|
new ForeignTaxCalculator());
|
||||||
LOGGER.info("Foreign Tax applied: {}", "" + internationalProductInvoice.getAmountWithTax());
|
LOGGER.info("Foreign Tax applied: {}", "" + internationalProductInvoice.getAmountWithTax());
|
||||||
|
|
||||||
//Create the invoice generator with product cost as 50 and domestic product tax
|
//Create the invoice generator with product cost as 50 and domestic product tax
|
||||||
var domesticProductInvoice = new InvoiceGenerator(PRODUCT_COST, new DomesticTax());
|
var domesticProductInvoice = new InvoiceGenerator(PRODUCT_COST, new DomesticTaxCalculator());
|
||||||
LOGGER.info("Domestic Tax applied: {}", "" + domesticProductInvoice.getAmountWithTax());
|
LOGGER.info("Domestic Tax applied: {}", "" + domesticProductInvoice.getAmountWithTax());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -28,7 +28,7 @@ import com.iluwatar.separatedinterface.invoice.TaxCalculator;
|
|||||||
/**
|
/**
|
||||||
* TaxCalculator for Domestic goods with 20% tax.
|
* TaxCalculator for Domestic goods with 20% tax.
|
||||||
*/
|
*/
|
||||||
public class DomesticTax implements TaxCalculator {
|
public class DomesticTaxCalculator implements TaxCalculator {
|
||||||
|
|
||||||
public static final double TAX_PERCENTAGE = 20;
|
public static final double TAX_PERCENTAGE = 20;
|
||||||
|
|
@ -28,7 +28,7 @@ import com.iluwatar.separatedinterface.invoice.TaxCalculator;
|
|||||||
/**
|
/**
|
||||||
* TaxCalculator for foreign goods with 60% tax.
|
* TaxCalculator for foreign goods with 60% tax.
|
||||||
*/
|
*/
|
||||||
public class ForeignTax implements TaxCalculator {
|
public class ForeignTaxCalculator implements TaxCalculator {
|
||||||
|
|
||||||
public static final double TAX_PERCENTAGE = 60;
|
public static final double TAX_PERCENTAGE = 60;
|
||||||
|
|
@ -26,13 +26,13 @@ package com.iluwatar.separatedinterface.taxes;
|
|||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
public class DomesticTaxTest {
|
public class DomesticTaxCalculatorTest {
|
||||||
|
|
||||||
private DomesticTax target;
|
private DomesticTaxCalculator target;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testTaxCaluclation(){
|
public void testTaxCalculation(){
|
||||||
target = new DomesticTax();
|
target = new DomesticTaxCalculator();
|
||||||
|
|
||||||
var tax=target.calculate(100.0);
|
var tax=target.calculate(100.0);
|
||||||
Assertions.assertEquals(tax,20.0);
|
Assertions.assertEquals(tax,20.0);
|
@ -26,13 +26,13 @@ package com.iluwatar.separatedinterface.taxes;
|
|||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
public class ForeignTaxTest {
|
public class ForeignTaxCalculatorTest {
|
||||||
|
|
||||||
private ForeignTax target;
|
private ForeignTaxCalculator target;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testTaxCaluclation(){
|
public void testTaxCalculation(){
|
||||||
target = new ForeignTax();
|
target = new ForeignTaxCalculator();
|
||||||
|
|
||||||
var tax=target.calculate(100.0);
|
var tax=target.calculate(100.0);
|
||||||
Assertions.assertEquals(tax,60.0);
|
Assertions.assertEquals(tax,60.0);
|
Loading…
x
Reference in New Issue
Block a user