Entries Tagged ‘DesignPatterns’:

实现一个排斥性(exclude)过滤器

说到排斥性过滤器,大家会一头雾水,搞不明白这其中含义。何为排斥性(exclude)过滤器呢,其实是本人自己定义出来的,呵呵。

排斥性过滤器是相对于规范所定义的Filter而言的,Java EE 规范中的过滤器是对web.xml中所列出的url进行过滤,而排斥性过滤器则恰恰相反,不对这些web.xml中列出的url执行过滤,而是对除这些url外的url进行过滤逻辑操作。

作为一个多年的Java开发人员,在实际开发中遇到这种情况,这便是有此动机的原因。

下面就讲讲这个exclude filter的原理,其实很简单。在拦截所有请求时,我们检查这些请求的url是否在url列表之内,如果在,那么就不进行过滤逻辑,直接调用chain.doFilter(xxx);否则的话,我们就执行一些过滤逻辑操作,然后再chain.doFilter(xxx)。

其中,检查url分2种方式:精确匹配(equals)和模糊匹配(contains),精确匹配优先于模糊匹配

对于代码中的URI获取,可能要根据实际情况作些更改,代码中URI只是使用request.getRequestURI(),得到的不是完整的URL,可视实际情况做出调整。

AbstractExcludeFilter 类将不该被override的方法都设置为了final,developer应该实现唯一的一个abstract method filter(),并且需要在该方法内部适当位置调用 chain.doFilter(xx)。

请看实现代码:


Tags: , - Views: 257 - Trackback -

Leave a Comment

Encapsulate What Varies Principle — 读《Head first Design Patterns》

Design Principle

Identify the aspects of your application that vary and separate them
from what stays the same

意思是,要尽可能把变化的部分与不变的部分分离开.


Tags: , - Views: 272 - Trackback -

Leave a Comment

Singleton Pattern 的几种方式

public class Singleton {
    private final static Singleton instance = new Singleton();
    // Private constructor suppresses generation of a (public) default constructor

    private Singleton() {}

    public static Singleton getInstance() {
        return instance;
   }
}
public class Singleton {
  private static Singleton instance;
  // Private constructor suppresses generation of a (public) default constructor

  private Singleton() {}

  public static synchronized Singleton getInstance() {
    if(instance == null) {
      instance = new Singleton();
    }

    return instance;
  }
}
public class Singleton {
  // Private constructor suppresses generation of a (public) default constructor
  private Singleton() {}
  private static class SingletonHolder {
    private static Singleton instance = new Singleton();
  }

  public static Singleton getInstance() {
    return SingletonHolder.instance;
  }
}
public class Singleton {
  private volatile static Singleton instance;
  // Private constructor suppresses generation of a (public) default constructor

  private Singleton() {}

  public static Singleton getInstance() {
    if(instance == null) {
      synchronized(Singleton.class) {
        if(instance == null) {
          instance = new Singleton();
        }
      }
    }
    return instance;
  }
}

Tags: , - Views: 293 - Trackback -

Leave a Comment

Dependency Inversion Principle — 读《Head first Design Patterns》

Design Principle

Depend upon abstractions. Do not depend upon concrete classes.


Tags: , - Views: 252 - Trackback -

Leave a Comment

The Open-Closed Principle — 读《Head first Design Patterns》

Another new OO design principle:

Design Principle

Classes should be open for extension, but closed for modification

e.g. Decorator Pattern


Tags: , - Views: 255 - Trackback -

Leave a Comment

Loose Coupling — 读《Head first Design Patterns》

The next principle i got is :

Design Principle:

Strive for loosely coupled designs between objects that interact


Tags: , - Views: 238 - Trackback -

Leave a Comment

HAS-A is better than IS-A — 读《Head first Design Patterns》

书中提到的第3个设计原则是:

Design Principle

Favor composition over inheritance

这是因为我们可以为HAS-A关系中的从属个体提供多种具体实现(通过面向接口编程),而且这些具体实现也可以在运行时(runtime)动态的变换(通过setter方法注入)。


Tags: , - Views: 241 - Trackback -

Leave a Comment

面向接口编程,而非面向实现编程 — 读《Head first Design Patterns》

《Head first Design Patterns》中提到的第二个编程法则:

Design Principle

Program to an interface, not an implementation

这样做的目的是有利于替换接口的具体实现


Tags: , - Views: 258 - Trackback -

Leave a Comment