2.2.3 工厂角色与抽象产品角色合并
(1) 具体产品角色
/**
* @author Geloin
*/
package com.geloin.pattern.factory.simple.mergefactoryandcreator;
/**
* 具体产品角色
*
* @author Geloin
*
*/
public class ConcreteProduct extends ProductAbst {
}
(2) 抽象产品角色(仅能为抽象类)
/**
* @author Geloin
*/
package com.geloin.pattern.factory.simple.mergefactoryandcreator;
/**
* 抽象产品角色与工厂角色合并
*
* @author Geloin
*
*/
public abstract class ProductAbst {
/**
* 工厂
*
* @author Geloin
* @return
*/
public static ProductAbst factory() {
return new ConcreteProduct();
}
}
(3) 测试类
/**
* @author Geloin
*/
package com.geloin.pattern.factory.simple.mergefactoryandcreator;
import junit.framework.Assert;
import org.junit.Test;
/**
* @author Geloin
*
*/
public class Main {
/**
* 测试方法
*
* @author Geloin
*/
@Test
public void test() {
ProductAbst abst = ProductAbst.factory();
Assert.assertTrue(abst instanceof ConcreteProduct);
}
}
本文介绍了一种设计模式——工厂角色与抽象产品角色合并模式。该模式将工厂与抽象产品的职责结合到一起,简化了类的设计。通过具体实现展示了如何创建具体产品,并通过测试验证了模式的有效性。
666

被折叠的 条评论
为什么被折叠?



