博文

目前显示的是标签为“design pattern”的博文

Mediator Pattern

We mentioned Mediator Pattern when we look at Front-end Controller Pattern from the MVC. The Front-end Controller is a web application pattern which " provides a centralized entry point for handling requests ". Mediator Pattern Definition "The mediator pattern defines an object that encapsulates how a set of objects interact. This pattern is considered to be a behavioral pattern due to the way it can alter the program's running behavior." Analog To Real world An airport control tower is an excellent example of the mediator pattern. The tower looks after who can take off and land - all communications are done from the airplane to control tower, rather than having plane-to-plane communication. Implementation in Java API java.util.Timer (all schedule methods) java.util.concurrent.Executor#execute() java.util.concurrent.ExecutorService (the invokeXXX() and submit() methods) java.util.concurrent.ScheduledExecutorService (all scheduleXXX() methods) ...

Meta-programming and Groovy MOP

http://www.packtpub.com/article/metaprogramming-and-groovy-mop

Web application design pattern - prevent duplicated form submission

图片
这是一个web development中常见的问题。User经常会迫不及待的点击submit按钮很多次,这样便会submit很多requests到服务器,然后出现各种问题。 解决方法有很多: 在客户端使用javascript来disable submit button以防用户多次点击。 使用Post/Redirect/Get pattern来防止用户使用F5或者Back按钮产生的muliple submittion. 如果不使用Post/Redirect/Get pattern就像这样: 使用Post/Redirect/Get pattern就是这样: 在生成页面时在server产生一个token,然后把这个token放在页面的hidden field 里面,当submit这个form时同时发送这个token回到server进行验证,如果server有这个token就成功process然后remove这个token,如果server没有这个token就停止process.(这说明这个form已经被submit过了) 可是使用来产生token的方法比如 java.util.UUID 或者apache的RandomStringUitls.

Java中的Dynamic Proxy 动态代理

图片
Dynamic Proxy是这样一种class:它是在运行时生成的class,在生成它时你必须提供一组interface给它,然后该class就宣称它实现了这些 interface。你当然可以把该class的实例当作这些interface中的任何一个来用。当然啦,这个Dynamic Proxy其实就是一个Proxy,它不会替你作实质性的工作,在生成它的实例时你必须提供一个handler,由它接管实际的工作     。  动态代理的定义: 一个动态代理类在运行期implements一组interface,使得interface实现类的方法调用被分派至其他的类(另外的interface实现类或者任意的类)的方法 。 讲得更通俗一些,要了解动态代理,我们就要知道什么东西动态了,代理了什么?首先,一个Proxy代理了一组interface的方法。注意, 代理的是interface ,而不是Class,也不是abstract Class;其次, Proxy具有的型别由绑定的interface所决定的 ,动态就体现在此。 public   interface  Resource  {      public   void  operationA();      public   void  operationB(); }                                                  public   class  Concret...

Design Pattern - Singleton

The Singleton Design Pattern 1.1. Definition The singleton Pattern ensures a class has only one instance, and provides a global point of access to it. 1.2. Usage Singletons are useful to define classes which should only be available once, e.g. a model which is use in several view of an application, a logger or a device driver. An example is an application which reads the data from a file into another class A. This content should be available in the whole application. Therefore the class A is defined as a singleton. Another example would be the following View A and View B would like to display the same data of class with hold the data (model class). Using a singleton for the model class ensures that same data is used. 1.3. Code Example The possible implementation of Java depends on the version of Java you are using. As of Java 6 you can singletons with a single-element enum type. package mypackage...