发帖    主题    评论    推荐    标签    作者    订阅    查搜    注册   登陆   关注
 
面向对象 设计模式 领域驱动设计 企业架构 框架 开发教程 微服务 大数据 扩展性 并发编程 事件驱动 分布式 SOA
1 2 下一页 Go 2

贴一个学习当中的写的代码

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();

2003-10-10 14:13

Bridge模式,Bridge模式在实际使用中经常不知不觉用上,自己都没意识到,也许Java血液里就是强调抽象和行为分开的。

2003-10-10 23:20

可是我写的时候,是按Decorator模式思维写的。
下面再贴几个,BANQ能不能帮我看看模式用的是否恰当。

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]);
}
}

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();
}
}

2Go 1 2 下一页

赞助商链接

赞助商链接

返回顶部

移动版 关于本站 使用帮助 联系管理员 最佳分辨率1366x768
OpenSource JIVEJDON Powered by JdonFramework Code © 2002-20 jdon.com