Java模式之责任链

今天模仿tomcat的Filter,自己学着写了一个测试例子。没有实现init和destroy方法,后期有时间再完善吧。

首先定义Filter的抽象接口

package com.learning.chain;

public interface Filter {

	public void init();
	
	public void doFilter(FilterChain chain);
	
	public void destroy();
}

 然后定义Filter配置类

package com.learning.chain;

public class ApplicationFilterConfig {

	private Filter filter;

	public Filter getFilter() {
		return filter;
	}

	public void setFilter(Filter filter) {
		this.filter = filter;
	}
	
}

 然后提取责任链抽象接口

package com.learning.chain;

public interface FilterChain {
	public void doFilter();
}

 实现责任链抽象接口

package com.learning.chain;

public class ApplicationFilterChain implements FilterChain {

	private ApplicationFilterConfig[] filters = new ApplicationFilterConfig[0];
	
	private static int INCREMENT = 10;
	
	private int length = 0;
	
	private int pos = 0;
	
	void addFilter(ApplicationFilterConfig filterConfig) {
		if (length == filters.length) {
            ApplicationFilterConfig[] newFilters =
                new ApplicationFilterConfig[length + INCREMENT];
            System.arraycopy(filters, 0, newFilters, 0, length);
            filters = newFilters;
        }
        filters[length++] = filterConfig;
	}
	
	public void doFilter() {
		if (pos < length) {
			ApplicationFilterConfig filterConfig = filters[pos++];
			Filter filter = filterConfig.getFilter();
			filter.doFilter(this);
		}
	}
	
}

 实现责任链工厂类

package com.learning.chain;

public class ApplicationFilterFactory {

	private static ApplicationFilterFactory factory;
	
	public static ApplicationFilterFactory getInstance() {
		if (factory == null) {
			factory = new ApplicationFilterFactory();
		}
		return factory;
	}
	
	public ApplicationFilterChain createFilterChain() {
		return new ApplicationFilterChain();
	}
	
}

 写测试用例

package com.learning.chain;

import java.io.File;
import java.io.FileInputStream;
import java.util.Enumeration;
import java.util.Properties;

import org.junit.Test;

public class TestFilter {

	@Test
	public void test() throws Exception {
		// 创建过滤器责任链
		ApplicationFilterChain chain = ApplicationFilterFactory.getInstance().createFilterChain();
		
		File file = new File("chain.properties");
		Properties filterProperties = new Properties();
		// 载入过滤器
		filterProperties.load(new FileInputStream(file));
		
		Enumeration<?> names = filterProperties.propertyNames();
		while (names.hasMoreElements()) {
			String filterName = (String)filterProperties.get(names.nextElement());
			ApplicationFilterConfig filterConfig = new ApplicationFilterConfig();
			filterConfig.setFilter((Filter)Class.forName(filterName).newInstance());
			// 把过滤器加入到责任链中
			chain.addFilter(filterConfig);
		}
		
		// 开始执行
		chain.doFilter();
	}
}

 最后配置好配置文件

filter1=com.learning.chain.FilterA
filter2=com.learning.chain.FilterA
filter3=com.learning.chain.FilterB
filter4=com.learning.chain.FilterA

 ok,运行起了,效果还不错。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
责任模式是一种行为设计模式,用于解耦发送请求者和处理请求者之间的关系。在责任模式中,多个对象组成一条,每个对象都有机会处理请求,如果一个对象不能处理该请求,则将该请求传递给下一个对象。该模式允许请求在中的不同对象之间传递,直到找到一个能够处理该请求的对象为止。 在Java实现责任模式的一种方法是使用一个基类接口定义处理请求的方法,然后每个具体的处理者都实现这个接口。接口中可以定义一个指向下一个处理者的引用,以便处理者可以将请求传递给下一个对象。 下面是一个使用Java实现责任模式的示例: ```java // 定义一个处理请求的接口 interface Handler { void handleRequest(Request request); } // 具体的处理者类 class ConcreteHandler1 implements Handler { private Handler nextHandler; @Override public void handleRequest(Request request) { // 如果满足处理条件,则处理该请求,否则将请求传递给下一个处理者 if (request.getCondition()) { // 处理请求的代码 } else { nextHandler.handleRequest(request); } } // 设置下一个处理者 public void setNextHandler(Handler nextHandler) { this.nextHandler = nextHandler; } } class ConcreteHandler2 implements Handler { private Handler nextHandler; @Override public void handleRequest(Request request) { // 如果满足处理条件,则处理该请求,否则将请求传递给下一个处理者 if (request.getCondition()) { // 处理请求的代码 } else { nextHandler.handleRequest(request); } } // 设置下一个处理者 public void setNextHandler(Handler nextHandler) { this.nextHandler = nextHandler; } } // 请求类 class Request { private boolean condition; public boolean getCondition() { return condition; } } public class Main { public static void main(String[] args) { // 创建责任 Handler handler1 = new ConcreteHandler1(); Handler handler2 = new ConcreteHandler2(); handler1.setNextHandler(handler2); // 创建请求 Request request = new Request(); // 处理请求 handler1.handleRequest(request); } } ``` 在上面的示例中,我们定义了一个处理请求的接口`Handler`,并实现了两个具体的处理者类`ConcreteHandler1`和`ConcreteHandler2`。在`handleRequest`方法中,我们首先判断是否满足处理条件,如果满足则处理请求,否则将请求传递给下一个处理者。 在`Main`类中,我们创建了一个责任,并设置了处理者的顺序。然后创建一个请求对象,并调用`handleRequest`方法开始处理请求。请求会沿着责任依次传递,直到找到能够处理该请求的处理者为止。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值