Skip to content

Commit

Permalink
Factory Method
Browse files Browse the repository at this point in the history
  • Loading branch information
dhsim86 committed Jul 29, 2019
1 parent a29c428 commit f192644
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.dongho.df.domain.creational.factory_method;

public class ConcreteProductA implements Product {

@Override
public String getProductName() {
return "concreteProductA";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.dongho.df.domain.creational.factory_method;

public class ConcreteProductACreator extends Creator {

@Override
protected Product createProduct() {
return new ConcreteProductA();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.dongho.df.domain.creational.factory_method;

public class ConcreteProductB implements Product {

@Override
public String getProductName() {
return "concreteProductB";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.dongho.df.domain.creational.factory_method;

public class ConcreteProductBCreator extends Creator {

@Override
protected Product createProduct() {
return new ConcreteProductB();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.dongho.df.domain.creational.factory_method;

public abstract class Creator {

abstract protected Product createProduct();

public String foo() {
Product product = createProduct();
return product.getProductName();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.dongho.df.domain.creational.factory_method;

public interface Product {

public String getProductName();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.dongho.df.domain.creational.factory_method;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import static org.assertj.core.api.Assertions.assertThat;

@RunWith(JUnit4.class)
public class FactoryMethodTest {

@Test
public void concreteProductATest() {
Creator creator;
String productName;

given: {
creator = new ConcreteProductACreator();
}

when: {
productName = creator.foo();
}

then: {
assertThat(productName).isEqualTo("concreteProductA");
}

}

@Test
public void concreteProductBTest() {
Creator creator;
String productName;

given: {
creator = new ConcreteProductBCreator();
}

when: {
productName = creator.foo();
}

then: {
assertThat(productName).isEqualTo("concreteProductB");
}

}

}

0 comments on commit f192644

Please sign in to comment.