JiveJdon Community Forums
在线213人   首页   主题总表   培训咨询   精华   查搜   注册    登陆
首页 » 论坛 » 设计模式、框架和架构
???en_US.forumThreadPrev.name??? 上一主题
  Go back to the topic 返回本主题   Go back to the topic listing返回主题列表
???en_US.forumThreadNext.name??? 下一主题
Go 总共有 6 回复 / 1
 发表新帖子   回复该主题贴
水雨澍

悄悄话
发表文章: 46
注册时间: 2003年10月07日 00:07
贴一个学习当中的写的代码 2003年10月07日 00:10 到本帖网址 加入本帖到收藏夹 发送到手机 回复该主题
标签列表
public interface CoffeeImp {
public void pourCoffeeImp();
}
public class MilkCoffeeImp implements CoffeeImp {
public void pourCoffeeImp() {
System.out.println("加奶");
}
}
public class CandyCoffeeImp implements CoffeeImp {
public void pourCoffeeImp() {
System.out.println("加糖");
}
}
public class CrotonCoffeeImp implements CoffeeImp {
public void pourCoffeeImp() {
System.out.println("加巴豆:)");
}
}

public interface Coffee {
public void addFavoring();
public void pourCoffee();
}
public class MediumCoffee implements Coffee {//标准中杯咖啡
public void addFavoring(){
System.out.println("普通调料");
}
public void pourCoffee() {
System.out.println("中杯咖啡");
}
}
public class DecMedCoffee implements Coffee {//加料中杯咖啡
private Coffee mediumCoffee;
private List otherAction = new ArrayList();
public DecMedCoffee(MediumCoffee _mediumCoffee) {
this.mediumCoffee = _mediumCoffee;
}
public void addFavoring(CoffeeImp coffeeImp) {
otherAction.add(coffeeImp);
}
public void pourCoffee() {
mediumcoffee.addFavoring();
mediumCoffee.pourCoffee();
otherMehtod();
}
private void otherMethod() {
if (it != null) {
Iterator it = otherAction.iterator();
while(it.hasNext()) {
(CoffeeImp)it.next().pourCoffeeImp();
}
}
}
}
public class CoffeeImpSingleton {
private static CoffeeImp coffeeImp;
public CoffeeImpSingleton(CoffeeImp _coffeeImp){
this.coffeeImp = _coffeeImp;
}
public static CoffeeImp getCoffeeImpInstance() {
return coffeeImp;
}
}

具体实现:
CoffeeImpSingleton milkSingleton = new CoffeeImpSingleton(new MilkCoffeeImp());
CoffeeImpSingleton candySingleton = new CoffeeImpSingleton(new CandyMilkCoffeeImp());
CoffeeImpSingleton crotonSingleton = new CoffeeImpSingleton(new CrotonCoffeeImp());

Coffee mediumCoffee = new DecMedCoffee();
mediumCoffee.addFavoring(milkSingleton.getCoffeeImpInstance());
mediumCoffee.addFavoring(candySingleton.getCoffeeImpInstance());
mediumCoffee.addFavoring(crotonSingleton.getCoffeeImpInstance());
mdeiumCoffee.pourCoffee();
banq

悄悄话
发表文章: 9312
注册时间: 2002年08月03日 17:08
Re: 贴一个学习当中的写的代码 2003年10月10日 14:13 到本帖网址 加入本帖到收藏夹 发送到手机 回复该主题
Bridge模式,Bridge模式在实际使用中经常不知不觉用上,自己都没意识到,也许Java血液里就是强调抽象和行为分开的。
水雨澍

悄悄话
发表文章: 46
注册时间: 2003年10月07日 00:07
Re: 贴一个学习当中的写的代码 2003年10月10日 23:20 到本帖网址 加入本帖到收藏夹 发送到手机 回复该主题
可是我写的时候,是按Decorator模式思维写的。
下面再贴几个,BANQ能不能帮我看看模式用的是否恰当。
水雨澍

悄悄话
发表文章: 46
注册时间: 2003年10月07日 00:07
CoR pattern 2003年10月10日 23:21 到本帖网址 加入本帖到收藏夹 发送到手机 回复该主题
//CoR pattern
public abstract class Handler {
public void handleRequest(String request) {
if (!doMyHandle(request)) {
Handler nextHandler = getNextHandler();
if (nextHandler == null) return;
getNextHandler().handleRequest(request);
}
execute();
}
public abstract Handler getNextHandler();
public abstract boolean doMyHandle(Request request);
protected abstract void execute();
}
public class HelpHandler extends Handler {
public Handler getNextHandler() {
return new PrintHandler();
}
public boolean doMyHandle(Request request) {
return request.equals("help");
}
protected void execute() {
System.out.println("recept help request");
}
}
public class PrintHandler extends Handler {
public Handler getNextHandler() {
return new FormatHandler();
}
public boolean doMyHandle(Request request) {
return request.equals("print");
}
protected void execute() {
System.out.println("recept print request");
}
}
public class FormatHandler extends Handler {
public Handler getNextHandler() { return null; }
public boolean doMyHandle(Request request) {
return request.equals("format");
}
protected void execute() {
System.out.println("recept format request");
}
}

public class Test {
public static void main(String[] args) {
if (args.length != 1) {
System.err.println("Usage:java Test help|print|format");
return;
}
Handler handler = new HelpHandler();
handler.handlerRequest(args[0]);
}
}
水雨澍

悄悄话
发表文章: 46
注册时间: 2003年10月07日 00:07
Command pattern 2003年10月10日 23:22 到本帖网址 加入本帖到收藏夹 发送到手机 回复该主题
//Command pattern
public interface Handler {
public void handleRequest();
}

public class HelpHandler implements Handler {
public void handleRequest() {
System.out.println("help command");
}
}
public class PrintHandler implements Handler {
public void handleRequest() {
System.out.println("print command");
}
}
public class FormatHandler implements Handler {
public void handleRequest() {
System.out.println("format command");
}
}

public class Producer {
public static HashMap producerRequest() {
HashMap hs = new HashMap();
hs.put("help",new HelpHandler);
hs.put("print",new PrintHandler();
hs.put("format",new FormatHandler();
return hs;
}
}

public class TestCommand {
public static void main(String[] args) {
if (args.length != 1) {
System.err.println("Usage:java TestCommand help|print|format");
return;
}
HashMap hs = Producer.producerRequest();
if (!hs.containsKey(args[0])) return;
(Handler)hs.get(args[0]).handleRequest();
}
}
public interface Handler {
public void handleRequest();
}

public class HelpHandler implements Handler {
public void handleRequest() {
System.out.println("help command");
}
}
public class PrintHandler implements Handler {
public void handleRequest() {
System.out.println("print command");
}
}
public class FormatHandler implements Handler {
public void handleRequest() {
System.out.println("format command");
}
}

public class Producer {
public static HashMap producerRequest() {
HashMap hs = new HashMap();
hs.put("help",new HelpHandler);
hs.put("print",new PrintHandler();
hs.put("format",new FormatHandler();
return hs;
}
}

public class TestCommand {
public static void main(String[] args) {
if (args.length != 1) {
System.err.println("Usage:java TestCommand help|print|format");
return;
}
HashMap hs = Producer.producerRequest();
if (!hs.containsKey(args[0])) return;
(Handler)hs.get(args[0]).handleRequest();
}
}
水雨澍

悄悄话
发表文章: 46
注册时间: 2003年10月07日 00:07
Visitor pattern 2003年10月10日 23:23 到本帖网址 加入本帖到收藏夹 发送到手机 回复该主题
//Visitor pattern
public interface Visitor {
public void visit(Visitable visitable);
}
public interface VisitLikedList {
public void visitLinkedList(LinkedList linkedList);
}
public interface VisitArrayList {
public void visitLinkedList(ArrayList arrayList);
}

public interface Visitable {
public void accept(Visitor visitor);
}

public class LinkedListElement implements Visitable {
private LinkedList lList;
public LinkedListElement(LinkedList lList) {
this.lList = lList;
}
public LinkedList getList() {
return lList;
}
protected void accept(Visitor visitor) {
(VisitLinkedList)visitor.visitLinkedList(this.getList());
}
}
public class ArrayListElement implements Visitable {
private ArrayList aList;
public ArrayListElement(ArrayList aList) {
this.aList = aList;
}
public ArrayList getList() {
return aList;
}
protected void accept(Visitor visitor) {
(VisitArrayList)visitor.visitArrayList(this.getList();
}
}

public class ConcreteVisitor implements Vistior,VisitLinkedList,VisitArrayList {
public void visit(Visitable visitable) {
visitable.accept(this);
}
public void visitLinkedList(LinkedList lList) {
Iteraotr it = lList.iterator();
while(it.hasNext) {
System.out.println(it.Next());
}
}
public void visitArrayList(ArrayList aList) {
for(int i=0;i<aList.size();i++) {
System.out.println(aList.get(i));
}
}
}
banq

悄悄话
发表文章: 9312
注册时间: 2002年08月03日 17:08
Re: 贴一个学习当中的写的代码 2003年10月11日 14:21 到本帖网址 加入本帖到收藏夹 发送到手机 回复该主题
冲咖啡这个例子你有两个接口Coffeee和CoffeeImp

Decorator模式的一个重要特点是接口不变,Decoratee被粉刷者和粉刷者Decorator应该继承的是同一个接口,完成同样的行为。

其它例子都基本不错,没有背离各自模式的定义
这个主题有 6 回复 / 1Go
???en_US.forumThreadPrev.name??? 上一主题
  Go back to the topic 返回本主题   Go back to the topic listing返回主题列表    返回页首返回页首
???en_US.forumThreadNext.name??? 下一主题
热点TAG: AOP cache 缓存 DDD EJB 集群 设计模式 Hibernate IOC JiveJdon OO RBAC Seam Spring Struts
正在读取,请等待...
google yahoo 新浪ViVi 365Key网摘 天极网摘 CSDN网摘 添加到百度搜藏 POCO网摘 博采网摘
查询本论坛内 回复超过的热门帖子
     回复该主题贴
标题
 
粗体 斜体 下划线 插入图片 插入代码 插入url链接 插入附件
内容
 

手机阅读 add to google add to yahoo
解惑之道在J道 ,打造中国最具影响力的的企业软件社区
OpenSource JIVEJDON v3.0 Powered by JdonFramework Code © 2002-08 jdon.com
anti spam