Java版本设计模式——创建型模式

Java版本设计模式——创建型模式

1、创建型模式

1.1、单例模式

单例模式顾名思义,就是全局就只有一个类。该类负责创建自己,同时需要保证只有单个对象被创建。单例模式有多种实现方案,具体如下:

1.1.1、懒汉模式

懒汉模式可以理解为,用到时再创建,否则不创建。

1.1.1.1、简单静态属性方式

此方式简单易懂,但线程不安全。

/**
 * 类似功能描述:
 *
 * @author Jeffwu
 */
public class Singleton {
   

    private static Singleton singleton = null;
    private Singleton(){
   }

    public static Singleton getInstance() {
   
    	//不安全点
        if (null == singleton) {
   
            singleton = new Singleton();
        }

        return singleton;
    }

    public static void main(String[] args) {
   
        Singleton instance = Singleton.getInstance();
        System.out.println(instance);
    }
}

改进版线程安全

/**
 * 类似功能描述:
 *
 * @author Jeffwu
 */
public class Singleton {
   

    private static Singleton singleton = null;
    private Singleton(){
   }

    public static Singleton getInstance() {
   
    	//不安全点
        if (null == singleton) {
   
            singleton = new Singleton();
        }

        return singleton;
    }

    public static synchronized void main(String[] args) {
   
        Singleton instance = Singleton.getInstance();
        System.out.println(instance);
    }
}

1.1.1.2、双重校验方式

该方式能实现懒加载,线程安全。不过此实现方式较为复杂。

/**
 * 类似功能描述:
 *
 * @author Jeffwu
 */
public class Singleton {
   

    private static volatile Singleton singleton = null;
    private Singleton(){
   }

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

        return singleton;
    }

    public static void main(String[] args) {
   
        Singleton instance = Singleton.getInstance();
        System.out.println(instance);
    }
}

1.1.1.3、静态内部类方式

该方式能实现懒加载,线程安全。不过此实现方式难度一般。之所以能懒加载与线程安全,得益于JVM的类加载。

/**
 * 类似功能描述:
 *
 * @author Jeffwu
 */
public class Singleton {
   

    static class SingletonHolder {
   
        private final static Singleton singleton = new Singleton();
    }
    private Singleton(){
   }

    public static Singleton getInstance() {
   
        return SingletonHolder.singleton;
    }

    public static void main(String[] args) {
   
        Singleton instance = Singleton.getInstance();
        System.out.println(instance);
    }
}
1.1.1.4、枚举方式

枚举是单例实现的最佳方法。因它简洁,自动支持序列化机制,防止反序列化重新创建对象,绝对防止多次实例化。线程安全,非懒加载方式。

/**
 * 类似功能描述:
 *
 * @author Jeffwu
 */
public enum Singleton {
   
    
    SINGLETON;

    public void test() {
   
        
    }

}
1.1.2、恶汉模式

恶汉模式可以理解为,不管用不用我,我都在这里。

/**
 * 类似功能描述:
 *
 * @author Jeffwu
 */
public class Singleton {
   

    private final static Singleton SINGLETON = new Singleton();

    public Singleton getSingleton() {
   
        return SINGLETON;
    }
}

1.2、静态工厂模式(方法)

**优点:**工厂模式提供一种将对象的实例化过程封装在工厂类中的方式。使用工厂模式,可以将对象的创建与使用进行解耦,提供一种统一的接口创建不同的对象。
**缺点:**新增新对象,需要改动工厂类的代码。

/**
 * 类似功能描述: 工厂设计模式:
 * 1、静态工厂
 * 2、抽象工厂
 *
 * @author Jeffwu
 */
public class ProductFactoryDemo {
   

    public static void main(String[] args) {
   
        Speed bicycle = SpeedFactory.speed("Bicycle");
        System.out.println("自行车速度:"+bicycle.speed());

        Speed saloon = SpeedFactory.speed("Saloon");
        System.out.println("轿车速度:"+saloon.speed());

        Speed truck = SpeedFactory.speed("Truck");
        System.out.println("卡车速度:"+truck.speed());
    }
}

/**
 * 获取速度的接口
 */
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值