 |
上一主题
eBay在中国收购了易趣的30%股份,eBay使用J2EE已经很多年,它的架构师John 谈了为什么eBay选择J2EE架构,而不是.Net,并且谈了核心模式中的两个新模式:
[url..
|
|
下一主题
Decorator定义:(动态给一个对象添加一些额外的职责,就象在墙上刷油漆.使用Decorator模式相比用生成子类方式达到功能的扩充显得更为灵活.
为什么使用Decorator?
我们通常可以..
|
|
|
|
|
|
|
|
贴一个学习当中的写的代码
|
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();
|
|
|
|
|
|
Re: 贴一个学习当中的写的代码
|
2003年10月10日 14:13
|
|
|
|
Bridge模式,Bridge模式在实际使用中经常不知不觉用上,自己都没意识到,也许Java血液里就是强调抽象和行为分开的。
|
|
|
|
|
|
Re: 贴一个学习当中的写的代码
|
2003年10月10日 23:20
|
|
|
可是我写的时候,是按Decorator模式思维写的。 下面再贴几个,BANQ能不能帮我看看模式用的是否恰当。
|
|
|
|
|
|
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]); } }
|
|
|
|
|
|
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(); } }
|
|
|
|
|
|
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)); } } }
|
|
|
|
|
|
Re: 贴一个学习当中的写的代码
|
2003年10月11日 14:21
|
|
|
冲咖啡这个例子你有两个接口Coffeee和CoffeeImp
Decorator模式的一个重要特点是接口不变,Decoratee被粉刷者和粉刷者Decorator应该继承的是同一个接口,完成同样的行为。
其它例子都基本不错,没有背离各自模式的定义
|
|
|
|
热点TAG:
AOP
cache
缓存
DDD
EJB
集群
设计模式
Hibernate
IOC
JiveJdon
OO
RBAC
Seam
Spring
Struts
anti spam
|