2.2.2 省略抽象产品类
如果系统仅有一个具体产品角色的话,那么就可以省略掉抽象产品角色。省略掉投产产品类后的简略类图如下所示:
而此时类图结构如下所示:
(1) 具体产品类
/**
* @author Geloin
*/
package com.geloin.pattern.factory.simple.noproduct;
/**
* 具体产品角色,因只有一个产品,故不需要抽象产品角色
*
* @author Geloin
*
*/
public class ConcreteProduct {
/**
* 测试方法
*
* @author Geloin
*/
public void sayHello() {
System.out.println("I dont't have a parent class or interface.");
}
}
(2) 工厂类角色
/**
* @author Geloin
*/
package com.geloin.pattern.factory.simple.noproduct;
/**
* @author Geloin
*
*/
public class Creator {
/**
* 工厂,没有抽象产品类
*
* @author Geloin
* @return
*/
public static ConcreteProduct factory() {
return new ConcreteProduct();
}
}
(3) 测试 类
/**
* @author Geloin
*/
package com.geloin.pattern.factory.simple.noproduct;
import junit.framework.Assert;
import org.junit.Test;
/**
* @author Geloin
*
*/
public class Main {
/**
* 测试方法
*
* @author Geloin
*/
@Test
public void test() {
ConcreteProduct product = Creator.factory();
Assert.assertTrue(product instanceof ConcreteProduct);
product.sayHello();
}
}