博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【代码积累】replace constructor with factory
阅读量:4099 次
发布时间:2019-05-25

本文共 893 字,大约阅读时间需要 2 分钟。

import java.util.concurrent.ConcurrentHashMap;public class Factory {	private ConcurrentHashMap
registedList = new ConcurrentHashMap
(); public void register(String name,Object object) { registedList.put(name, object); } private static Factory factory = null; public Vehicle createSpecifiedVehicle(String name) { return ((Vehicle)registedList.get(name)).createObject(); /*This is not gonna work as when createSpecifiedVehicle is called the static blocks in sub-classes has not been * executed yet so registedList is actually empty.Static block in a class will be executed at the time the class * is being load,and only when the class is needed it will be loaded by ClassLoader.*/ } public static Factory getInstance() { if( null == factory ) { synchronized(Factory.class) { if( null == factory ) { factory = new Factory(); } } } return factory; }}

转载地址:http://sthii.baihongyu.com/

你可能感兴趣的文章
【测试工具】在linux测试环境访问禅道数据库
查看>>
【Python】提升Python程序性能的好习惯2
查看>>
【工具】SecureCRT安装和注册
查看>>
【工具】FTP软件FileZilla下载和连接服务器
查看>>
【Python】random模块生成多种类型随机数
查看>>
【债券】可转换债券基本概念
查看>>
【股票】融资融券基本概念
查看>>
【性能测试】性能测试的基础理论
查看>>
【性能测试】性能测试的基本流程
查看>>
【性能测试】性能测试工具选择
查看>>
【性能测试】Linux系统监控-Top命令
查看>>
【测试工具】禅道项目管理工具设置触发邮箱
查看>>
【性能测试】Linux系统监控-CPU信息
查看>>
【Linux】Linux简介以及 与UNIX区别
查看>>
【视频】视频文件格式和视频编码
查看>>
【工具】Notepad++的一些常用配置
查看>>
【文字识别】Python3使用百度AI进行文字识别
查看>>
【图片】图像基本知识以及三原色原理 (rgb)
查看>>
【图片】Python对RGB颜色与16进制颜色进行互转
查看>>
【Python】pyinstaller模块将py文件打包为windows可执行文件exe
查看>>