美文网首页
java中常用的设计模式

java中常用的设计模式

作者: One_仑 | 来源:发表于2019-03-27 17:26 被阅读0次

Java中一般认为有23种设计模式,我们不需要所有的都会,但是其中常用的几种设计模式应该去掌握。下面列出了所有的设计模式。需要掌握的设计模式我单独列出来了,当然能掌握的越多越好。
总体来说设计模式分为三大类:

  1. 创建型模式,共五种:单例模式工厂方法模式抽象工厂模式建造者模式、原型模式。
  2. 结构型模式,共七种:适配器模式、装饰器模式、代理模式、外观模式、桥接模式、组合模式、享元模式
1、单例设计模式

最好理解的一种设计模式,分为懒汉式和饿汉式。
饿汉式:

public class Singleton {
  // 直接创建对象
  public static Singleton instance = new Singleton();
  // 私有话构造函数
  private Singleton(){
  }
  // 返回对象实例
  public static Singleton getInstance(){
    return instance;
  }
}

懒汉式:

public class Singleton {
  // 直接创建对象
  public static Singleton instance = null;
  // 私有话构造函数
  private Singleton(){
  }
  // 返回对象实例
  public static Singleton getInstance(){
    if(instance == null) {
      synchronized(Singleton.class){
        if(instance == null) {
          instance = new Singleton();
        }
      }
    }
    return instance;
  }
}
2、工厂模式(略)
3、建造者模式Builder

工厂类模式提供的是创建单个类的模式,而建造者模式则是将各种产品集中起来进行管理,用来创建复合对象,
所谓复合对象就是指某个类具有不同的属性。

public class Builder { 
  private List<Sender> list = new ArrayList<Sender>(); 
  public void produceMailSender(int count) {
    for (int i=0; i<count; i++) {
      list.add(new SmsSender());
    }
  }
  public void produceSmsSender(int count) {
    for (int i=0; i<count; i++) {
      list.add(new SmsSender());
    }
  }
}
public class TestBuilder { 
  public static void main(String[] args) { 
    Builder builder = new Builder(); 
    builder.produceMailSender(10); 
  }
}
4、适配器设计模式

适配器模式将某个类的接口转换成客户端期望的另一个接口表示,目的是消除由于接口不匹配所造成的类的兼容性问题。主要分为三类:类的适配器模式、对象的适配器模式、接口的适配器模式。
类的适配器:

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!");
  }
}
public class AdapterTest {
  public static void main(String[] args) {
    Targetable target = new Adapter();
    target.method1();
    target.method2(); 
  }
}

对象适配器:

public class Wrapper implements Targetable {
  private Source source;
  public Wrapper(Source source) {
    super();
    this.source = source;
  }
  @Override
  public void method2() {
    System.out.println("this is the targetable method!");
  } 
  @Override
  public void method1() {
    source.method1();
  } 
}
public class AdapterTest {
  public static void main(String[] args) {
    Source source = new Source();
    Targetable target = new Wrapper(source);
    target.method1();
    target.method2();
  }
}

接口适配器:
有时我们写的一个接口中有多个抽象方法,当我们写该接口的实现类时,必须实现该接口的所有方法,这明显有时比较浪费,因为并不是所有的方法都是我们需要的,有时只需要某一些,此处为了解决这个问题,我们引入了接口的适配器模式,借助于一个抽象类,该抽象类实现了该接口,实现了所有的方法,而我们不和原始的接口打交道,只和该抽象类取得联系,所以我们写一个类,继承该抽象类,重写我们需要的方法就行。

5、装饰模式

顾名思义,装饰模式就是给一个对象增加一些新的功能,而且是动态的,要求装饰对象和被装饰对象实现同一个。

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!");
  }
} 
public class DecoratorTest {
  public static void main(String[] args) {
    Sourceable source = new Source();
    Sourceable obj = new Decorator(source); 
    obj.method();
  }
}
6、策略模式
7、观察者模式

相关文章

  • Java 常用设计模式简例

    简述Java常用设计模式 简述Java常用设计模式及设计原则 strate---------策略模式针对接口编程,...

  • 单例模式

    JAVA设计模式之单例模式 十种常用的设计模式 概念: java中单例模式是一种常见的设计模式,单例模式的写法...

  • 工厂模式

    java设计模式-工厂模式 工厂模式: 工厂模式是java设计模式里最常用的设计模式之一。 工厂模式属于创建型模式...

  • java中常用的设计模式?

    java中常用的设计模式?

  • 并行设计模式(一)-- Future模式

    Java多线程编程中,常用的多线程设计模式包括:Future模式、Master-Worker模式、Guarded ...

  • 设计模式

    海滩拾贝,隽永文章遗满地 23种设计模式介绍以及在Java中的实现简介:简介了Java常用设计模式: 创建型模式6...

  • 知识复盘

    1:熟练使用Android常用性能调优 2:Java常用设计模式 3:Android常用设计模式(架构) 4:An...

  • java中单例模式实现(包含double-check)

    单例模式是24中设计模式中的最常用、也是最为简单的一种设计模式。java中实现单例模式的方式,大致分为两种: 1、...

  • java动态代理(JDK和cglib)(转载自http://ww

    java动态代理(JDK和cglib) JAVA的动态代理 代理模式 代理模式是常用的java设计模式,他的特征是...

  • 设计模式之工厂模式

    工厂模式 工厂模式(Factory Pattern)是Java中最常用的设计模式之一,这种设计模式属于创建型模式,...

网友评论

      本文标题:java中常用的设计模式

      本文链接:https://www.haomeiwen.com/subject/dbozvqtx.html