BAEL-5768 - Coupling in Java (#11) (#12881)

* BAEL-5768 - Coupling in Java (#11)

* BAEL-5768 - Coupling in Java
This commit is contained in:
alemoles 2022-10-18 00:24:12 -03:00 committed by GitHub
parent b522237512
commit 16c5169c86
16 changed files with 255 additions and 0 deletions

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>patterns-modules</artifactId>
<groupId>com.baeldung</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>coupling</artifactId>
</project>

View File

@ -0,0 +1,14 @@
package com.baeldung.loose;
import java.io.File;
import java.util.List;
public class CSVExport implements ExportMetadata {
@Override
public File export(List<Object> metadata) {
System.out.println("Exporting data...");
// Export Metadata
File outputCSV = null;
return outputCSV;
}
}

View File

@ -0,0 +1,8 @@
package com.baeldung.loose;
import java.io.File;
import java.util.List;
public interface ExportMetadata {
File export(List<Object> metadata);
}

View File

@ -0,0 +1,7 @@
package com.baeldung.loose;
import java.util.List;
public interface FetchMetadata {
List<Object> fetchMetadata();
}

View File

@ -0,0 +1,12 @@
package com.baeldung.loose;
import java.util.ArrayList;
import java.util.List;
public class JSONFetch implements FetchMetadata{
@Override
public List<Object> fetchMetadata() {
System.out.println("Fetching some json data");
return new ArrayList<>();
}
}

View File

@ -0,0 +1,26 @@
package com.baeldung.loose;
import java.util.List;
public class MetadataCollector {
private final FetchMetadata fetchMetadata;
private final ExportMetadata exportMetadata;
public MetadataCollector(FetchMetadata fetchMetadata, ExportMetadata exportMetadata) {
this.fetchMetadata = fetchMetadata;
this.exportMetadata = exportMetadata;
}
public void collectMetadata() {
List<Object> metadata = fetchMetadata.fetchMetadata();
exportMetadata.export(metadata);
}
public FetchMetadata getFetchMetadata() {
return fetchMetadata;
}
public ExportMetadata getExportMetadata() {
return exportMetadata;
}
}

View File

@ -0,0 +1,14 @@
package com.baeldung.loose;
import java.io.File;
import java.util.List;
public class PDFExport implements ExportMetadata {
@Override
public File export(List<Object> metadata) {
System.out.println("PDF Export");
// Some logic
File outputPDF = null;
return outputPDF;
}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.loose;
import java.util.ArrayList;
import java.util.List;
public class XMLFetch implements FetchMetadata {
@Override
public List<Object> fetchMetadata() {
List<Object> metadata = new ArrayList<>();
// Do some stuff
return metadata;
}
}

View File

@ -0,0 +1,14 @@
package com.baeldung.tight;
import java.io.File;
import java.util.List;
public class CSVExport {
public File export(List<Object> metadata) {
System.out.println("Exporting data...");
// Export Metadata
File outputCSV = null;
return outputCSV;
}
}

View File

@ -0,0 +1,12 @@
package com.baeldung.tight;
import java.util.ArrayList;
import java.util.List;
public class JSONFetch {
public List<Object> fetchMetadata() {
List<Object> metadata = new ArrayList<>();
// Do some stuff
return metadata;
}
}

View File

@ -0,0 +1,35 @@
package com.baeldung.tight;
import java.util.List;
public class MetadataCollector {
private XMLFetch xmlFetch = new XMLFetch();
private JSONFetch jsonFetch = new JSONFetch();
private CSVExport csvExport = new CSVExport();
private PDFExport pdfExport = new PDFExport();
public void collectMetadata() {
List<Object> metadata = xmlFetch.fetchMetadata();
csvExport.export(metadata);
}
public void collectMetadata(int inputType, int outputType) {
if (outputType == 1) {
List<Object> metadata = null;
if (inputType == 1) {
metadata = xmlFetch.fetchMetadata();
} else {
metadata = jsonFetch.fetchMetadata();
}
csvExport.export(metadata);
} else {
List<Object> metadata = null;
if (inputType == 1) {
metadata = xmlFetch.fetchMetadata();
} else {
metadata = jsonFetch.fetchMetadata();
}
pdfExport.export(metadata);
}
}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.tight;
import java.io.File;
import java.util.List;
public class PDFExport {
public File export(List<Object> metadata) {
System.out.println("Exporting data...");
// Export Metadata
File outputPDF = null;
return outputPDF;
}
}

View File

@ -0,0 +1,12 @@
package com.baeldung.tight;
import java.util.ArrayList;
import java.util.List;
public class XMLFetch {
public List<Object> fetchMetadata() {
List<Object> metadata = new ArrayList<>();
// Do some stuff
return metadata;
}
}

View File

@ -0,0 +1,38 @@
package com.baeldung.loose;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
class LooselyCouplingUnitTest {
@Test
public void givenMetadataCollector_thenCollectMetadataXMLAndExportCSV() {
FetchMetadata metadata = new XMLFetch();
ExportMetadata exportMetadata = new CSVExport();
MetadataCollector collector = new MetadataCollector(metadata, exportMetadata);
collector.collectMetadata();
assertTrue(collector.getExportMetadata() instanceof CSVExport);
assertTrue(collector.getFetchMetadata() instanceof XMLFetch);
}
@Test
public void givenMetadataCollector_thenCollectMetadataUsingJSONAndExportPDF() {
FetchMetadata metadata = new JSONFetch();
ExportMetadata exportMetadata = new PDFExport();
MetadataCollector collector = new MetadataCollector(metadata, exportMetadata);
collector.collectMetadata();
assertTrue(collector.getExportMetadata() instanceof PDFExport);
assertTrue(collector.getFetchMetadata() instanceof JSONFetch);
}
@Test
public void givenMetadataCollector_thenCollectMetadataUsingXMLAndExportPDF() {
FetchMetadata metadata = new XMLFetch();
ExportMetadata exportMetadata = new PDFExport();
MetadataCollector collector = new MetadataCollector(metadata, exportMetadata);
collector.collectMetadata();
assertTrue(collector.getExportMetadata() instanceof PDFExport);
assertTrue(collector.getFetchMetadata() instanceof XMLFetch);
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.tight;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;
import org.junit.jupiter.api.Test;
class TightlyCouplingUnitTest {
@Test
public void givenMetadataCollector_thenCollectMetadata() {
MetadataCollector collector = mock(MetadataCollector.class);
doNothing().when(collector)
.collectMetadata();
}
@Test
public void givenMetadataCollectorWithDifferentInput_thenCollectMetadata() {
MetadataCollector collector = new MetadataCollector();
collector.collectMetadata(1, 1);
}
}

View File

@ -29,6 +29,7 @@
<module>solid</module> <module>solid</module>
<module>clean-architecture</module> <module>clean-architecture</module>
<module>enterprise-patterns</module> <module>enterprise-patterns</module>
<module>coupling</module>
</modules> </modules>
<dependencyManagement> <dependencyManagement>