[list]
[1]工厂方法模式
在上一个博客中【[url]http://tjychyangchenhui.iteye.com/admin/blogs/1571293[/url]】
介绍了[Simple Factory Pattern],接下来将会探讨[Factory Method Pattern]与[Simple Factory Method Pattern] 模式的异同之处和[Factory Method Pattern] 的优势所在.
[2][Factory Method Pattern] 的非静态工厂代码示例
[3] [Factory Method Pattern] 的UML 类图
[img]http://dl.iteye.com/upload/attachment/0070/1319/3201fc16-89c3-329d-bc5e-d5986c139f78.jpg[/img]
[/list]
[1]工厂方法模式
在上一个博客中【[url]http://tjychyangchenhui.iteye.com/admin/blogs/1571293[/url]】
介绍了[Simple Factory Pattern],接下来将会探讨[Factory Method Pattern]与[Simple Factory Method Pattern] 模式的异同之处和[Factory Method Pattern] 的优势所在.
[2][Factory Method Pattern] 的非静态工厂代码示例
package com.tjych.factorypattern.factorymethod;
public interface IFactory {
/**
* @label Creates
*/
/*#com.tjych.factorypattern.factorymethod.Product lnkProduct*/
public IProduct createProduct();
}
package com.tjych.factorypattern.factorymethod;
public interface IFactorySecond extends IFactory {
}
package com.tjych.factorypattern.factorymethod;
/**
* 工厂类,用于产生客户希望的Product
*
* @author 杨晨辉
*
*/
public class FactoryDemo1 implements IFactory{
/**
* @label Create
*/
/*#com.tjych.factorypattern.factorymethod.Product1 lnkProduct1*/
@Override
public IProduct createProduct() {
return new Product1();
}
}
package com.tjych.factorypattern.factorymethod;
public class FactoryDemo2 implements IFactory {
/**
* @label Create
*/
/*#com.tjych.factorypattern.factorymethod.Product2 lnkProduct2*/
@Override
public IProduct createProduct() {
return new Product2();
}
}
package com.tjych.factorypattern.factorymethod;
public class FactorySecond1 implements IFactorySecond {
@Override
public IProduct createProduct() {
return new ProductSecond1();
}
}
package com.tjych.factorypattern.factorymethod;
public class FactorySecond2 implements IFactorySecond {
@Override
public IProduct createProduct() {
return new ProductSecond2();
}
}
package com.tjych.factorypattern.factorymethod;
public interface IProduct {
public void sampleMethod();
}
package com.tjych.factorypattern.factorymethod;
public interface IProductSecond extends IProduct {
void test();
}
package com.tjych.factorypattern.factorymethod;
public class Product1 implements IProduct {
@Override
public void sampleMethod() {
System.out.println("This is the method sampleMethod of Product1");
}
}
package com.tjych.factorypattern.factorymethod;
/**
*
* @author 杨晨辉
*
*/
public class Product2 implements IProduct {
@Override
public void sampleMethod() {
System.out.println("this is the sampleMethod of Product2");
}
}
package com.tjych.factorypattern.factorymethod;
public class ProductSecond1 implements IProductSecond {
@Override
public void sampleMethod() {
System.out.println("this is the sampleMethod of ProductSecond1");
}
@Override
public void test() {
// TODO Auto-generated method stub
}
}
package com.tjych.factorypattern.factorymethod;
public class ProductSecond2 implements IProductSecond {
@Override
public void sampleMethod() {
System.out.println("this is the sampleMethod of ProductSecond2");
}
@Override
public void test() {
// TODO Auto-generated method stub
}
}
[3] [Factory Method Pattern] 的UML 类图
[img]http://dl.iteye.com/upload/attachment/0070/1319/3201fc16-89c3-329d-bc5e-d5986c139f78.jpg[/img]
[/list]