If you want you System only have one Object .
On this Ocassion ,we can use the Single Pattern .
The main Format are as follows (in JAVA )
public class BS { //Because our teacher use the BookSystem as an Example.
private static bs= new BS()
private BS(){
//You can write code at this
}
public static BS getBS(){
return bs;
}
}
I have see this Design pattern before,but today I answer the teacher'question,I said that the Factory Pattern can solve the problem that one class in the System that only Create one instance.I make a mistake.It's a very excellent Design Pattern.
博客介绍了若想系统中只有一个对象,可使用单例模式,并给出了Java代码示例。同时作者分享自己曾答错老师问题,误将工厂模式认为能解决系统中一个类只创建一个实例的问题,强调单例模式是优秀的设计模式。
2142

被折叠的 条评论
为什么被折叠?



