Skip to content

Commit 0a0e065

Browse files
committed
Added Factory Method pattern and tests.
1 parent 5444571 commit 0a0e065

2 files changed

Lines changed: 55 additions & 0 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* To change this template, choose Tools | Templates
3+
* and open the template in the editor.
4+
*/
5+
6+
package designpatterns;
7+
8+
/**
9+
*
10+
* @author jtherrell
11+
*/
12+
abstract class Creator {
13+
public Product newProduct(Log log) {
14+
return createProduct(log);
15+
}
16+
abstract protected Product createProduct(Log log);
17+
18+
}
19+
20+
class MyCreator extends Creator {
21+
protected Product createProduct(Log log) {
22+
log.append("Created new MyProduct!");
23+
return new MyProduct();
24+
}
25+
}
26+
27+
class Product {}
28+
class MyProduct extends Product {}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* To change this template, choose Tools | Templates
3+
* and open the template in the editor.
4+
*/
5+
6+
package designpatterns;
7+
8+
import org.junit.Test;
9+
import static org.junit.Assert.*;
10+
/**
11+
*
12+
* @author jtherrell
13+
*/
14+
public class FactoryMethodTest {
15+
16+
/**
17+
* Test of getInstance method, of class Singleton.
18+
*/ @Test
19+
public void testFactoryMethod() {
20+
Log log = new Log();
21+
Creator myCreator = new MyCreator();
22+
myCreator.newProduct(log);
23+
String expResult = "Created new MyProduct!";
24+
String result = log.toString();
25+
assertEquals(expResult, result);
26+
}
27+
}

0 commit comments

Comments
 (0)