24种设计模式----(*独一无二*单例模式)
来源:互联网 发布:java框架批量添加数据 编辑:程序博客网 时间:2024/05/17 01:40
以写日志为例:
示例代码------------------------------------------------------------------------------------
package com.moshi.singleton;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
public class logmagage {
private static PrintWriter out;
private final static String filename="G://log.txt";//设置日志问题路径
//唯一实例 logmagage
private static logmagage logmagage;
private logmagage(){
outinit();
}
public static void outinit(){
if(out==null){
try {
out=new PrintWriter(new FileWriter(filename,true),true);
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.print("无法打开日志文件");
e.printStackTrace();
out=null;
}
}
}
public static synchronized void log(String msg){
if(logmagage==null||out==null){logmagage=new logmagage();}
if(out!=null){
out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+":"+msg);
}
}
public static void destory(){
if(out!=null)out.close();
}
}
//客户端调用
package com.moshi.singleton;
public class logclient {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
logmagage.log("javaweb程序设计");//写入日志
}
}
示例代码------------------------------------------------------------------------------------
package com.moshi.singleton;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
public class logmagage {
private static PrintWriter out;
private final static String filename="G://log.txt";//设置日志问题路径
//唯一实例 logmagage
private static logmagage logmagage;
private logmagage(){
outinit();
}
public static void outinit(){
if(out==null){
try {
out=new PrintWriter(new FileWriter(filename,true),true);
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.print("无法打开日志文件");
e.printStackTrace();
out=null;
}
}
}
public static synchronized void log(String msg){
if(logmagage==null||out==null){logmagage=new logmagage();}
if(out!=null){
out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+":"+msg);
}
}
public static void destory(){
if(out!=null)out.close();
}
}
//客户端调用
package com.moshi.singleton;
public class logclient {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
logmagage.log("javaweb程序设计");//写入日志
}
}
- 24种设计模式----(*独一无二*单例模式)
- Java设计模式:单例模式-独一无二的对象
- VB.NET“独一无二”之单例模式
- 单例模式:创建独一无二的对象
- 单件模式(独一无二的对象)
- 【设计模式与Android】单例模式——独一无二的皇帝
- 单例模式(Singleton Pattern)——独一无二的对象
- 我是独一无二的『单例模式』
- 设计模式(单例设计模式)
- 设计模式(单例设计模式)
- 设计模式之单例设计模式(饿汉单例设计模式&懒汉单例设计模式)
- 设计模式(工厂模式、单例模式、模板模式)
- 设计模式(单例模式,工厂模式,适配器模式)
- 模式五:单例模式(Singleton Pattern)——创建独一无二的对象
- 设计模式(24) ------------单例模式
- 23种设计模式-单例模式
- java23种设计模式-----单例模式
- 23种设计模式~单例模式
- Qt Creator + MinGW 在windows 下的调试
- 使用UltraISO将苹果dmg镜像转换标准ISO镜像
- Unity3d Webplayer 屏蔽缓存机制
- iOS: bundle name, bundle display name, bundle identifier...
- [转]链接服务器的 OLE DB 访问接口 "SQLNCLI" 无法启动分布式事务
- 24种设计模式----(*独一无二*单例模式)
- 基于verilog的三段式状态机
- nginx php-fpm安装配置
- 使用Cocoapods包管理工具
- webService
- cocos2d-X 节点(CCLayer.h)API
- /usr/include/i386-linux-gnu/bits/pthreadtypes.h
- c++ 重载、重写(覆盖)以及重定义的区别:
- ubuntu下eclipse桌面图标的创建