单例、多例设计模式、枚举
2016-10-30 18:44
204 查看
单例设计模式:
特征是构造方法私有化,而后在类的内部提供有一个static本类对象,并且利用static方法取得此对象。
代码如下:
class Singleton{
private static final Singleton INSTANCE = new Singleton();
private Singleton(){}
public static Singleton getInstance(){
return INSTANCE;
}
public void print(){
System.out.println("hello world");
}
}
public class TestDemo{
public static void main(String[] args)throws Exception {
Singleton s = Singleton.getInstance();
s.print();
}
}
一定要记住封装不只是private一种方式,还包含有protect权限,往往出现在一些跨包的操作上。
如果存在有多个对象,那么就是多例设计模式(在类的内部存在有多个对象的信息)。
代码如下:
class Color{
private static final Color RED = new Color("红色");
private static final Color GREEN = new Color("绿色");
private static final Color BLUE = new Color("蓝色");
private String title;
private Color(String title){
this.title=title;
}
public static Color getInstance(int ch){
switch(ch){
case 0:
return RED;
case 1:
return GREEN;
case 2:
return BLUE;
default:
return null;
}
}
public String toString(){
return this.title;
}
}
public class TestDemo{
public static void main(String[] args)throws Exception {
System.out.println(Color.getInstance(0));
System.out.println(Color.getInstance(1));
System.out.println(Color.getInstance(2));
System.out.println(Color.getInstance(3));
}
}
在单例设计模式里面只有一个对象,所以当调用了getInstance 方法的时候可以很方便的找到制定的对象,但是如果是多例设计模式需要记住编号,太麻烦了,为此才会在jdk1.5后提供了枚举类型。
枚举代码如下:
enum Color{
RED("红色"),GREEN("绿色"),BLUE("蓝色");
private String title;
private Color(String title){
this.title= title;
}
public String toString(){
return this.title;
}
}
public class TestDemo{
public static void main(String[] args)throws Exception {
System.out.println(Color.RED);
System.out.println(Color.GREEN);
System.out.println(Color.BLUE);
}
}
面试题:enum和Enum有什么的区别?哪个类使用了单例设计模式?
-enum是一个关键字而Enum是一个类;
-使用enum定义的枚举相当于一个类继承了Enum类;
-Runtime类使用了单例设计模式;
特征是构造方法私有化,而后在类的内部提供有一个static本类对象,并且利用static方法取得此对象。
代码如下:
class Singleton{
private static final Singleton INSTANCE = new Singleton();
private Singleton(){}
public static Singleton getInstance(){
return INSTANCE;
}
public void print(){
System.out.println("hello world");
}
}
public class TestDemo{
public static void main(String[] args)throws Exception {
Singleton s = Singleton.getInstance();
s.print();
}
}
一定要记住封装不只是private一种方式,还包含有protect权限,往往出现在一些跨包的操作上。
如果存在有多个对象,那么就是多例设计模式(在类的内部存在有多个对象的信息)。
代码如下:
class Color{
private static final Color RED = new Color("红色");
private static final Color GREEN = new Color("绿色");
private static final Color BLUE = new Color("蓝色");
private String title;
private Color(String title){
this.title=title;
}
public static Color getInstance(int ch){
switch(ch){
case 0:
return RED;
case 1:
return GREEN;
case 2:
return BLUE;
default:
return null;
}
}
public String toString(){
return this.title;
}
}
public class TestDemo{
public static void main(String[] args)throws Exception {
System.out.println(Color.getInstance(0));
System.out.println(Color.getInstance(1));
System.out.println(Color.getInstance(2));
System.out.println(Color.getInstance(3));
}
}
在单例设计模式里面只有一个对象,所以当调用了getInstance 方法的时候可以很方便的找到制定的对象,但是如果是多例设计模式需要记住编号,太麻烦了,为此才会在jdk1.5后提供了枚举类型。
枚举代码如下:
enum Color{
RED("红色"),GREEN("绿色"),BLUE("蓝色");
private String title;
private Color(String title){
this.title= title;
}
public String toString(){
return this.title;
}
}
public class TestDemo{
public static void main(String[] args)throws Exception {
System.out.println(Color.RED);
System.out.println(Color.GREEN);
System.out.println(Color.BLUE);
}
}
面试题:enum和Enum有什么的区别?哪个类使用了单例设计模式?
-enum是一个关键字而Enum是一个类;
-使用enum定义的枚举相当于一个类继承了Enum类;
-Runtime类使用了单例设计模式;
相关文章推荐
- 多例设计模式与枚举
- 设计模式之多例模式(Multition Pattern)
- 设计模式之多例模式
- 黑马程序员--张孝祥Java高新技术-JDK1.5新特性(二)【基本数据类型的自动拆箱与装箱,枚举】以及享元设计模式
- 单例模式——php单例模式的运用,用单例模式设计实现多例
- php设计模式之单例、多例设计模式的应用分析
- java面向对象(十三) 构造方法私有化(单例与多例设计模式)
- 设计模式(2)——多例设计
- 设计模式之多例模式
- php设计模式之单例、多例设计模式的应用分析
- java_23种设计模式之多例模式
- Java_多例设计模式
- 黑马程序员——><基础加强一(eclipse-静态导入-享元设计模式-自动拆装箱-枚举-反射-类加载器)>
- [转载]php设计模式之单例、多例设计模式
- 设计模式---->多例设计
- Java高新技术1---增强for循环 ---装箱与拆箱享元设计模式--自己写枚举--可变长度的参数
- Java高新技术1---增强for循环 ---装箱与拆箱享元设计模式--自己写枚举--可变长度的参数
- day01:枚举类型实现单例设计模式
- 黑马程序员---枚举单例设计模式
- 设计模式学习笔记--单例(Singleton)和多例(Multiton)