- 前言
- 一、单例模式
- 1.1饿汉式
- 1.2 懒汉式
- 1.3 静态内部类
- 1.4 枚举
- 二、策略模式
- 2.1策略方法
- 2.2 策略实现
- 2.3 使用策略
前言
工作中常用到单例模式和策略模式。简单记录下使用心得
一、单例模式当需要使用的对象在全局中仅需要一份的时候,就需要使用单例模式了。
举个例子,常见的spring IOC容器就可以保证单例。
工作中一般使用这种
public class ResourceMgrV1 {
//2、静态成员变量,在类加载的时候,classloader会初时话静态方法和变量
// 由jvm虚拟机保证线程安全
private static ResourceMgrV1 resourceMgrV1 = new ResourceMgrV1();
//1、构造方法私有
private ResourceMgrV1() {
}
public static ResourceMgrV1 getInstance() {
return resourceMgrV1;
}
}
测试:
生成的hashcode都一样
public class Main {
public static void main(String[] args) throws Exception {
for (int i = 0; i < 1000; i++) {
new Thread(() -> System.out.println(ResourceMgrV1.getInstance().hashCode())).start();
}
Thread.sleep(1000);
}
}
优点:简单
缺点:服务一起的就加载对象,但是也无伤大雅吧(个人觉得)
public class ResourceMgrV2 {
//2、需要加volatile 防止高并发的时候,cpu指令重排,导致线程不安全多new出新对象
private static volatile ResourceMgrV2 resourceMgrV2 = null;
//1、私有化构造方法
private ResourceMgrV2() {
}
//3、DCL(double check lock) 双重检测
public static ResourceMgrV2 getInstance() {
if (resourceMgrV2 == null) {
synchronized (ResourceMgrV2.class) {
if (resourceMgrV2 == null) {
resourceMgrV2 = new ResourceMgrV2();
}
}
}
return resourceMgrV2;
}
}
测试
生成hashcode一致
public class Main {
public static void main(String[] args) throws Exception {
for (int i = 0; i < 1000; i++) {
new Thread(() -> System.out.println(ResourceMgrV2.getInstance().hashCode())).start();
}
Thread.sleep(1000);
}
}
优点:按需获取
缺点:代码复杂
public class ResourceMgrV3 {
public static class ResourceMgrHolder {
//2、ResourceMgrHolder静态内部类不会随着ResourceMgrV3加载到内存时被调用
//只有在显示调用静态内部类的resourceMgrV3成员变量时,才会被加载到内存
public static ResourceMgrV3 resourceMgrV3 = new ResourceMgrV3();
}
//1、私有化构造方法
private ResourceMgrV3() {
}
public static ResourceMgrV3 getInstance() {
return ResourceMgrHolder.resourceMgrV3;
}
}
测试:
生成hashcode一致
public class Main {
public static void main(String[] args) throws Exception {
for (int i = 0; i < 1000; i++) {
new Thread(() -> System.out.println(ResourceMgrV3.getInstance().hashCode())).start();
}
Thread.sleep(1000);
}
}
如果是处女座的话,这个方法相对就是比较完美的了
1.4 枚举public enum ResourceMgrV4 {
INSTANCE;
public static ResourceMgrV4 getInstance() {
return INSTANCE;
}
}
测试
生成hashcode一致
public class Main {
public static void main(String[] args) throws Exception {
for (int i = 0; i < 1000; i++) {
new Thread(() -> System.out.println(ResourceMgrV4.getInstance().hashCode())).start();
}
Thread.sleep(1000);
}
}
这算是最完美的方式了,并且相比上面三种防止,枚举可以防止别人反射生成对象。
但是工作中个人基本不用
public interface ProtocolStrategy {
void process(String message);
}
2.2 策略实现
策略方法实现1
public class FtpProtocolStrategy implements ProtocolStrategy {
@Override
public void process(String message) {
System.out.println("处理ftp协议 ==>" + message);
}
}
策略方法实现2
public class HttpProtocolStrategy implements ProtocolStrategy {
@Override
public void process(String message) {
System.out.println("处理http协议 ==>" + message);
}
}
2.3 使用策略
public class Main {
public static void main(String[] args) {
String receiveMsg = "哈哈哈";
//将来可以使用apollo等配置中心,在上面配置key=com.cch.strategy.FtpProtocolStrategy
//然后在下面的地方,利用反射 Class.forName() 产生对应对象
ProtocolStrategy protocolStrategy = new FtpProtocolStrategy();
protocolStrategy.process(receiveMsg);
}
}
实际工作中,有这样一种场景应用到策略模式
比如当设备上下线或者dp按键名称更改,则会收到上游发过来的kafka消息。
这个时候,我们会根据不同情况,做不同策略。



