2008-06-17
工厂模式
简单工厂模式
工厂方法模式
抽象工厂模式
// 产品接口
public interface Product {
public void getName();
}
// 具体产品A
public class ProductA implements Product {
public void getName() {
System.out.println(" I am ProductA ");
}
}
// 具体产品B
public class ProductB implements Product {
public void getName() {
System.out.println(" I am ProductB ");
}
}
// 工厂类
public class ProductCreator {
public Product createProduct(String type) {
if (" A ".equals(type)) {
return new ProductA();
}
if (" B ".equals(type)) {
return new ProductB();
} else
return null;
}
public static void main(String[] args) {
ProductCreator creator = new ProductCreator();
creator.createProduct(" A ").getName();
creator.createProduct(" B ").getName();
}
}
工厂方法模式
public interface Factory
{
Apple createApple();
}
public class ConcreteFactory1 implements Factory
{
public Apple createApple()
{
return new RedApple();
}
}
public class ConcreteFactory2 implements Factory
{
public Apple createApple()
{
return new GreenApple();
}
}
抽象工厂模式
public interface Factory
{
Apple createApple();
Grape createGrape();
}
public class ConcreteFactory1 implements Factory
{
public Apple createApple()
{
return new RedApple();
}
public Grape createGrape()
{
return new RedGrape();
}
}
public class ConcreteFactory2 implements Factory
{
public Apple createApple()
{
return new GreenApple();
}
public Grape createGrape()
{
return new GreenGrape();
}
}
发表评论
- 浏览: 8709 次
- 性别:

- 来自: 杭州

- 详细资料
搜索本博客
最近加入圈子
最新评论
-
Click framework 的 Page ...
继续~~加油
-- by avaj -
Click framework 导言(一 ...
Tapestry 5 我还没用过 , 有空去看下
-- by congpeixue -
Click framework 导言(一 ...
这个不就是有点模仿Tapestry么?它有一个极大的缺陷:页面的呈现完几乎使用j ...
-- by rrrrutdk -
Click framework 快速入门 ...
对的啊 , 是一个框架, 引用Click is a simple JEE we ...
-- by congpeixue -
Click framework 快速入门 ...
click jee framework 是一个J2EE框架吧?我都没有听说过.
-- by qichunren






评论排行榜