public class Singleton3 {
private static MapString,Singleton3 map = new HashMapString,Singleton3();
static{
Singleton3 single = new Singleton3();
map.put(single.getClass().getName(), single);
}
//保护的默认构造子
protected Singleton3(){}
//静态工厂方法,返还此类惟一的实例
public static Singleton3 getInstance(String name) {
if(name == null) {
name = Singleton3.class.getName();
System.out.println("name == null"+"---name="+name);
}
if(map.get(name) == null) {
try {
map.put(name, (Singleton3) Class.forName(name).newInstance());
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
return map.get(name);
}
//一个示意性的商业方法
public String about() {
return "Hello, I am RegSingleton.";
}
public static void main(String[] args) {
Singleton3 single3 = Singleton3.getInstance(null);
System.out.println(single3.about());
}
}
JAVA 采用单例设计模式设计日志处理类,并写出代码import java.util.Date;
public class Logger {
private static Logger instance;
private static Class clazz;
private Logger() {
}
private Logger(Class clazz) {
this.instance = new Logger();
this.clazz = clazz;
}
public static Logger getLogger(Class clazz) {
if (instance == null) {
synchronized (Logger.class) {
if (instance == null) {
instance = new Logger(clazz);
}
}
}
return instance;
}
public void info(String msg) {
System.out.println(clazz.getName() + "[" + new Date().toLocaleString()
+ "][info]:" + msg);
}
public void error(String msg) {
System.out.println(clazz.getName() + "[" + new Date().toLocaleString()
+ "][error]:" + msg);
}
public void warn(String msg) {
System.out.println(clazz.getName() + "[" + new Date().toLocaleString()
+ "][warn]:" + msg);
}
public void log(String msg) {
System.out.println(clazz.getName() + "[" + new Date().toLocaleString()
+ "][log]:" + msg);
}
public static void main(String args[]) {
Logger s = Logger.getLogger(String.class);
s.info("普通消息");
}
}
Java设计模式,需要用到两种设计模式,急需!工厂模式java设计模式附代码:
Product代码:
public interface Work {
void doWork();
}
ConcreteProduct代码:
public class StudentWork implements Work {
public void doWork() {
System.out.println("学生做作业!");
}
}
public class TeacherWork implements Work {
public void doWork() {
System.out.println("老师审批作业!");
}
}
Creator代码:
public interface IWorkFactory {
Work getWork();
}
ConcreteCreator代码:
public class StudentWorkFactory implements IWorkFactory {
public Work getWork() {
return new StudentWork();
}
}
public class TeacherWorkFactory implements IWorkFactory {
public Work getWork() {
return new TeacherWork();
}
}
Test代码:
public class Test {
public static void main(String[] args) {
IWorkFactory studentWorkFactory = new StudentWorkFactory();
studentWorkFactory.getWork().doWork();
IWorkFactory teacherWorkFactory = new TeacherWorkFactory();
teacherWorkFactory.getWork().doWork();
}
}
追问一下java设计模式附代码,给java设计模式附代码你写第二个 。
北大青鸟java培训:java的十种设计模式?在java培训java设计模式附代码的过程中java设计模式附代码,我们需要java设计模式附代码了解到关于javajava设计模式附代码的设计模式,下面是成都java培训介绍的关于java设计模式的相关介绍 。
推荐阅读
- 直播平台上买猫,直播卖猫注意什么
- jquery依赖注入,依赖注入 php
- microsoftnet安装失败,microsoftnetframework40怎么安装不了
- pes2013下载,pes2013下载地址
- python中函数导入 python 导入函数
- 电脑下载itunes,电脑下载itunes没反应
- 计算机毕业设计推荐,计算机类毕业设计知乎
- 理财技巧公益直播,理财直播课是真的吗
- vb.net随机固定数据 vb随机数字的代码