常见的设计模式

来源:互联网 发布:wifi音频接收器 软件 编辑:程序博客网 时间:2024/06/01 08:24

类的适配器模式

* 适配器模式将某个类的接口转换成客户端期望的另一个接口表示,目的是消除由于接口不匹配所造成的类的兼容性问题。主要分为三类:类的适配器模式、对象的适配器模式、接口的适配器模式 * 核心思想就是:有一个Source类,拥有一个方法,待适配,目标接口时Targetable,通过Adapter类,将Source的功能扩展到Targetable里 * @author Weilong Liu * * @date 20171212日 */public class AdapterTest {    public class Source {          public void method1() {              System.out.println("this is original method!");          }      }      public interface Targetable {          /* 与原类中的方法相同 */          public void method1();          /* 新类的方法 */          public void method2();      }      public class Adapter extends Source implements Targetable {          @Override          public void method2() {              System.out.println("this is the targetable method!");          }      }  }

责任链模式

package designpattern;
/**
* 责任链模式
* 有多个对象,每个对象持有对下一个对象的引用,这样就会形成一条链,请求在这条链上传递,直到某一对象决定处理该请求。
* 但是发出者并不清楚到底最终那个对象会处理该请求,所以,责任链模式可以实现,在隐瞒客户端的情况下,对系统进行动态的调整
* @author Weilong Liu
*
* @date 2017年12月12日
*/
public class ChainOfResponsibility {
public interface Handler {

    public void operator();  }   public abstract class AbstractHandler {      private Handler handler;      public Handler getHandler() {          return handler;      }      public void setHandler(Handler handler) {          this.handler = handler;      }  }  public class MyHandler extends AbstractHandler implements Handler {      private String name;      public MyHandler(String name) {          this.name = name;      }      @Override      public void operator() {          System.out.println(name+"deal!");          if(getHandler()!=null){              getHandler().operator();          }      }  }  /*public class Test {      public static void main(String[] args) {          MyHandler h1 = new MyHandler("h1");          MyHandler h2 = new MyHandler("h2");          MyHandler h3 = new MyHandler("h3");          h1.setHandler(h2);          h2.setHandler(h3);          h1.operator();      }  }  */

}

装饰模式

package designpattern;
/**
* 装饰模式
* 装饰模式就是给一个对象增加一些新的功能,而且是动态的,要求装饰对象和被装饰对象实现同一个接口,装饰对象持有被装饰对象的实例
* 不同点:
* 1. 代理类中会添加额外的私有方法并被接口方法调用,可以在接口方法调用前和调用后对输入参数或输出结果加以控制;
* 2. 而装饰模式没有添加额外的私有方法,而是在接口方法中直接添加一些功能并调用成员对象的接口方法,以起到丰富成员对象的方法。
* @author Weilong Liu
*
* @date 2017年12月12日
*/
public class DecoratorTest {

//接口public interface Sourceable {      public void method();  }  public class Source implements Sourceable {      @Override      public void method() {          System.out.println("the original method!");      }  }  public class Decorator implements Sourceable {      private Sourceable source;      //?      public Decorator(Sourceable source){          super();          this.source = source;      }      @Override      public void method() {          System.out.println("before decorator!");          source.method();          System.out.println("after decorator!");      }  }  

}

代理模式

package designpattern;/** * 代理模式 * 代理模式的应用场景: * 如果已有的方法在使用的时候需要对原有的方法进行改进,此时有两种办法: * 1、修改原有的方法来适应。这样违反了“对扩展开放,对修改关闭”的原则。 * 2、就是采用一个代理类调用原有的方法,且对产生的结果进行控制。这种方法就是代理模式。 * 使用代理模式,可以将功能划分的更加清晰,有助于后期维护! * @author Weilong Liu * * @date 2017年12月12日 */public class ProxyTest {    public interface Sourceable {          public void method();      }      public class Source implements Sourceable {          @Override          public void method() {              System.out.println("the original method!");          }      }      public class Proxy implements Sourceable {          private Source source;          public Proxy(){              super();              this.source = new Source();          }          @Override          public void method() {              before();              source.method();              atfer();          }          private void atfer() {              System.out.println("after proxy!");          }          private void before() {              System.out.println("before proxy!");          }      }  }

单例模式

package designpattern;/** * 单例模式 * 单例对象(Singleton)是一种常用的设计模式。在Java应用中,单例对象能保证在一个JVM中,该对象只有一个实例存在。这样的模式有几个好处: * 1、某些类创建比较频繁,对于一些大型的对象,这是一笔很大的系统开销。 * 2、省去了new操作符,降低了系统内存的使用频率,减轻GC压力。 * 3、有些类如交易所的核心交易引擎,控制着交易流程,如果该类可以创建多个的话,系统完全乱了。(比如一个军队出现了多个司令员同时指挥,肯定会乱成一团),所以只有使用单例模式,才能保证核心交易服务器独立控制整个流程。 * @author Weilong Liu * * @date 2017年12月12日 */public class Singleton {        /* 私有构造方法,防止被实例化 */          private Singleton() {          }          /* 此处使用一个内部类来维护单例 */          private static class SingletonFactory {             private static Singleton instance = new Singleton();          }          /* 获取实例 */          public static Singleton getInstance() {              return SingletonFactory.instance;          }          /* 如果该对象被用于序列化,可以保证对象在序列化前后保持一致 */          public Object readResolve() {              return getInstance();          }      }  

策略模式

package designpattern;
/**
* 策略模式
* 策略模式定义了一系列算法,并将每个算法封装起来,使他们可以相互替换,且算法的变化不会影响到使用算法的客户。
* 需要设计一个接口,为一系列实现类提供统一的方法,多个实现类实现该接口,设计一个抽象类(可有可无,属于辅助类),提供辅助函数
* @author Weilong Liu
*
* @date 2017年12月12日
*/
public class StrategyTest {
public interface ICalculator {
public int calculate(String exp);
}

public abstract class AbstractCalculator {      public int[] split(String exp,String opt){          String array[] = exp.split(opt);          int arrayInt[] = new int[2];          arrayInt[0] = Integer.parseInt(array[0]);          arrayInt[1] = Integer.parseInt(array[1]);          return arrayInt;      }  }  public class Plus extends AbstractCalculator implements ICalculator {      @Override      public int calculate(String exp) {          int arrayInt[] = split(exp,"\\+");          return arrayInt[0]+arrayInt[1];      }  }  public class Minus extends AbstractCalculator implements ICalculator {      @Override      public int calculate(String exp) {          int arrayInt[] = split(exp,"-");          return arrayInt[0]-arrayInt[1];      }  }  public class Multiply extends AbstractCalculator implements ICalculator {      @Override      public int calculate(String exp) {          int arrayInt[] = split(exp,"\\*");          return arrayInt[0]*arrayInt[1];      }  }  

}