·您的位置: 首页 » 资源教程 » 编程开发 » JAVA、JSP » 使用UML编写Java 设计模式例子 FactoryMethod Pattern

使用UML编写Java 设计模式例子 FactoryMethod Pattern

类别: JAVA教程  评论数:0 总得分:0
摘自久久学院
看了论坛上的文章,读FactoryMethod Pattern UML图,写了个小例子程序。做为文章的补充!
//Creator.java

public abstract class Creator
{
/**
* looks like a factory
* contains some products and some process methods
*/
protected Product duct;
abstract String processProduct();
abstract String processProductSpecial();
public String Operation()
{
return processProduct(); // FactoryMethod for implemention;
}
}

//ConcreteProduct.java

public class ConcreteProduct extends Product
{
public ConcreteProduct()
{
System.out.println("construct the ConcreteProduct object");
}
public String makeString(String str)
{
return "This is the product after processing:"+str;
}
}

//Product.java
public abstract class Product
{
public abstract String makeString(String str);
}

//ConcreteCreatorA.java

public class ConcreteCreatorA extends Creator
{
/**
* You can write some other class like this to override
* the same methods do diffrent thing;
*/
public ConcreteCreatorA()
{
duct=new ConcreteProduct();
System.out.println("instanced a product");

}
public String processProduct()
{
System.out.println("product A:");
return duct.makeString("Make @#a product");

}
public String processProductSpecial()
{
System.out.println("special product B:");
//add another method here
return duct.makeString("Make Special @#B product");
}
}



//TestFactoryMethod.java
public class TestFactoryMethod
{
public TestFactoryMethod()
{
}
public static void main(String[] arg)
{
ConcreteCreatorA cc=new ConcreteCreatorA();
System.out.println(cc.Operation());
System.out.println(cc.processProductSpecial());
}
}



-= 资 源 教 程 =-
文 章 搜 索
关键词:
类型:
范围:

纯粹空间 softpure.com
Copyright © 2006-2012 暖阳制作 版权所有
承接程序设计 网页设计
QQ: 15242663 (隐身在线 拒绝闲聊)  Email: faisun@sina.com

 纯粹空间 - 韩国酷站|酷站欣赏|教程大全|资源下载|免费博客|美女壁纸|设计素材|技术论坛  

百度搜索 谷歌搜索 Alexa搜索 | 粤ICP备19116064号-1