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

03-10-07 水雨澍

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
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 下一页