几种常用的Java设计模式

来源:互联网 发布:2017年淘宝天猫电商节 编辑:程序博客网 时间:2024/05/21 09:59

1.单例模式。

什么是单例模式,为什么要使用单例模式?

单例模式是指在整个程序的生命周期中保证内存中某个类只有一个实例。

在软件开发的过程中,我们都会用到一些实用类,这些类与业务逻辑无关专门提供一些公共的功能供别人调用,为了减少内存占用。

单例模式demo

在不考虑非大并发的情况下书写如下。
  1. public class Singleton {  
  2.   
  3.     private static Singleton sSingleton;  
  4.   
  5.     private Singleton() {  
  6.     }  
  7.   
  8.     public static Singleton getInstance() {  
  9.         if (sSingleton == null) {  
  10.             sSingleton = new Singleton();  // line 10
  11.         }  
  12.   
  13.         return sSingleton;  
  14.     }  
  15.   
  16. }  

如果程序并发交大,会有两个线程同时执行到第10行,内存中会存在两个Singleton实例这违背了单例模式的原则。可参考如下两种写法:
a.double-check(DCL)
  1. public class Singleton {  
  2.   
  3.     private volatile static Singleton sSingleton;  
  4.   
  5.     private Singleton() {  
  6.     }  
  7.   
  8.     public static Singleton getInstance() {  
  9.         if (sSingleton == null) { 
  10.             synchronized (Singleton.class) {   
  11.                 if (sSingleton == null)  
  12.                 sSingleton = new Singleton();  
  13.             }  
  14.         }  
  15.   
  16.         return sSingleton;  
  17.     }  
  18.   
  19. }  
b.静态内部类
  1. public class Singleton  
  2. {  
  3.     private Singleton(){  
  4.     }  
  5.   
  6.     private static class InstanceHolder{  
  7.         private static final Singleton instance = new Singleton();  
  8.     }  
  9.   
  10.     public static Singleton getInstance(){  
  11.         return InstanceHolder.instance;  
  12.     }  
  13.   
  14. }


2.工厂模式。

什么是工厂模式,为什么要使用工厂模式

将对象的创建统一起来,便于控制和代码的后期维护。能够避免代码中出现大量的new关键字。后期需要改构造函数时非常方便。

工厂模式有三种实现方式:简单工厂、工厂方法和抽象工厂

a.简单工厂 

  1. interface Person {  
  2.     void eat();  
  3. }  
  4.   
  5. class Man implements Person {  
  6.   
  7.     @Override  
  8.     public void eat() {  
  9.         System.out.println("Man eat");  
  10.     }  
  11.   
  12. }  
  13.   
  14. class Woman implements Person {  
  15.   
  16.     @Override  
  17.     public void eat() {  
  18.         System.out.println("Woman eat");  
  19.     }  
  20.   
  21. }  
  22.   
  23. class PersonFactory {  
  24.     public static Person creator(String personType) {  
  25.         if (personType.equals("Man")) {  
  26.             return new Man();  
  27.         } else if (personType.equals("Woman")) {  
  28.             return new Woman();  
  29.         } else {  
  30.             throw new UnsupportedOperationException("Not have this type person :" + personType  );  
  31.         }  
  32.     }  
  33. }  
  34.   
  35. public class A {  
  36.     public static void main(String args[]) {  
  37.         Person man = PersonFactory.creator("Man");  
  38.         man.eat();  
  39.         Person woman = PersonFactory.creator("Woman");  
  40.         woman.eat();  
  41.     }  
  42. }

b.工厂方法



定义一个用于创建对象的接口,让子类决定将哪一个类实例化。

3.适配器模式。

解决原本由于接口或类不兼容而不能一起工作的类可以一起工作。

4.抽象工厂模式。

提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。

0 0
原创粉丝点击