Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ cd java-patterns-and-constructs
```
## Da List
### Patterns
* [Abstract Factory](src/main/java/com/penapereira/example/constructs/abstractfactory/)
* [Factory](src/main/java/com/penapereira/example/constructs/factory/)
* [Factory Method](src/main/java/com/penapereira/example/constructs/factorymethod/)
* [Observer](src/main/java/com/penapereira/example/constructs/observer/)
* [Singleton](src/main/java/com/penapereira/example/constructs/singleton/)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.penapereira.example.constructs.abstractfactory;

public interface AbstractFactory {
ProductA createProductA();
ProductB createProductB();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.penapereira.example.constructs.abstractfactory;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.penapereira.example.constructs.app.ExampleRunnerInterface;

@Component
public class AbstractFactoryExampleRunner implements ExampleRunnerInterface {

private static final Logger log =
LoggerFactory.getLogger(AbstractFactoryExampleRunner.class);

@Override
public void runExample() throws Exception {
log.trace("Executing Abstract Factory Pattern Implementation:");

AbstractFactory factory1 = new Factory1();
AbstractFactory factory2 = new Factory2();

log.trace(" " + factory1.createProductA().name());
log.trace(" " + factory1.createProductB().name());

log.trace(" " + factory2.createProductA().name());
log.trace(" " + factory2.createProductB().name());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.penapereira.example.constructs.abstractfactory;

public class Factory1 implements AbstractFactory {

@Override
public ProductA createProductA() {
return new ProductA1();
}

@Override
public ProductB createProductB() {
return new ProductB1();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.penapereira.example.constructs.abstractfactory;

public class Factory2 implements AbstractFactory {

@Override
public ProductA createProductA() {
return new ProductA2();
}

@Override
public ProductB createProductB() {
return new ProductB2();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.penapereira.example.constructs.abstractfactory;

public interface ProductA {
String name();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.penapereira.example.constructs.abstractfactory;

public class ProductA1 implements ProductA {

@Override
public String name() {
return "ProductA1";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.penapereira.example.constructs.abstractfactory;

public class ProductA2 implements ProductA {

@Override
public String name() {
return "ProductA2";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.penapereira.example.constructs.abstractfactory;

public interface ProductB {
String name();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.penapereira.example.constructs.abstractfactory;

public class ProductB1 implements ProductB {

@Override
public String name() {
return "ProductB1";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.penapereira.example.constructs.abstractfactory;

public class ProductB2 implements ProductB {

@Override
public String name() {
return "ProductB2";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.penapereira.example.constructs.factory;

public class ConcreteProductA implements Product {

@Override
public String name() {
return "Concrete Product A";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.penapereira.example.constructs.factory;

public class ConcreteProductB implements Product {

@Override
public String name() {
return "Concrete Product B";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.penapereira.example.constructs.factory;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.penapereira.example.constructs.app.ExampleRunnerInterface;

@Component
public class FactoryExampleRunner implements ExampleRunnerInterface {

private static final Logger log = LoggerFactory.getLogger(FactoryExampleRunner.class);

@Override
public void runExample() throws Exception {
log.trace("Executing Factory Pattern Implementation:");

ProductFactory factory = new ProductFactory();

Product productA = factory.createProduct("A");
Product productB = factory.createProduct("B");

log.trace(" " + productA.name());
log.trace(" " + productB.name());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.penapereira.example.constructs.factory;

public interface Product {
String name();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.penapereira.example.constructs.factory;

public class ProductFactory {

public Product createProduct(String type) {
return switch (type) {
case "A" -> new ConcreteProductA();
case "B" -> new ConcreteProductB();
default -> throw new IllegalArgumentException("Unknown type: " + type);
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.penapereira.example.constructs.abstractfactory;

import org.junit.jupiter.api.Test;

class AbstractFactoryExampleRunnerTests {
@Test
void runExampleRuns() throws Exception {
new AbstractFactoryExampleRunner().runExample();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.penapereira.example.constructs.abstractfactory;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

class AbstractFactoryTests {
@Test
void factoriesProduceCorrectProducts() {
AbstractFactory f1 = new Factory1();
AbstractFactory f2 = new Factory2();
assertEquals("ProductA1", f1.createProductA().name());
assertEquals("ProductB1", f1.createProductB().name());
assertEquals("ProductA2", f2.createProductA().name());
assertEquals("ProductB2", f2.createProductB().name());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.penapereira.example.constructs.factory;

import org.junit.jupiter.api.Test;

class FactoryExampleRunnerTests {
@Test
void runExampleRuns() throws Exception {
new FactoryExampleRunner().runExample();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.penapereira.example.constructs.factory;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

class FactoryTests {
@Test
void factoryCreatesConcreteProducts() {
ProductFactory factory = new ProductFactory();
Product a = factory.createProduct("A");
Product b = factory.createProduct("B");
assertEquals("Concrete Product A", a.name());
assertEquals("Concrete Product B", b.name());
}
}