1.单例设计模式
所谓单例设计模式简单说就是无论程序如何运行,采用单例设计模式的类(Singleton类)永远只会有一个实例化对象产生。具体实现步骤如下:
(1) 将采用单例设计模式的类的构造方法私有化(采用private修饰)。
(2) 在其内部产生该类的实例化对象,并将其封装成private static类型。
(3) 定义一个静态方法返回该类的实例。
示例代码如下:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | class Singleton { private static Singleton instance = new Singleton(); // 在内部产生本类的实例化对象 public static Singleton getInstance() { // 通过静态方法返回instance对象 return instance; } private Singleton() { // 将构造方法封装为私有化 } public void print() { System.out.println( "Hello World!!!" ); } } public class SingletonDemo { public static void main(String args[]) { Singleton s1 = null ; // 声明对象 Singleton s2 = null ; // 声明对象 Singleton s3 = null ; // 声明对象 s1 = Singleton.getInstance(); // 取得实例化对象 s2 = Singleton.getInstance(); // 取得实例化对象 s3 = Singleton.getInstance(); // 取得实例化对象 s1.print(); // 调用方法 s2.print(); // 调用方法 s3.print(); // 调用方法 } } |
一、单例模式的介绍
Singleton是一种创建型模式,指某个类采用Singleton模式,则在这个类被创建后,只可能产生一个实例供外部访问,并且提供一个全局的访问点
二、单例模式的实现
实现的方式有如下四种:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | /** * * 单例模式的实现:饿汉式,线程安全 但效率比较低 */ public class SingletonTest { private SingletonTest() { } private static final SingletonTest instance = new SingletonTest(); public static SingletonTest getInstancei() { return instance; } } |
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | /** * 单例模式的实现:饱汉式,非线程安全 * */ public class SingletonTest { private SingletonTest() { } private static SingletonTest instance; public static SingletonTest getInstance() { if (instance == null ) instance = new SingletonTest(); return instance; } } |
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | /** * 线程安全,但是效率非常低 * @author vanceinfo * */ public class SingletonTest { private SingletonTest() { } private static SingletonTest instance; public static synchronized SingletonTest getInstance() { if (instance == null ) instance = new SingletonTest(); return instance; } } |
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | /** * 线程安全 并且效率高 * */ public class SingletonTest { private static SingletonTest instance; private SingletonTest() { } public static SingletonTest getIstance() { if (instance == null ) { synchronized (SingletonTest. class ) { if (instance == null ) { instance = new SingletonTest(); } } } return instance; } } |
2.工厂设计模式
程序在接口和子类之间加入了一个过渡端,通过此过渡端可以动态取得实现了共同接口的子类实例化对象。
示例代码如下:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | interface Animal { // 定义一个动物的接口 public void say(); // 说话方法 } class Cat implements Animal { // 定义子类Cat @Override public void say() { // 覆写say()方法 System.out.println( "我是猫咪,喵呜!" ); } } class Dog implements Animal { // 定义子类Dog @Override public void say() { // 覆写say()方法 System.out.println( "我是小狗,汪汪!" ); } } class Factory { // 定义工厂类 public static Animal getInstance(String className) { Animal a = null ; // 定义接口对象 if ( "Cat" .equals(className)) { // 判断是哪个子类的标记 a = new Cat(); // 通过Cat子类实例化接口 } if ( "Dog" .equals(className)) { // 判断是哪个子类的标记 a = new Dog(); // 通过Dog子类实例化接口 } return a; } } public class FactoryDemo { public static void main(String[] args) { Animal a = null ; // 定义接口对象 a = Factory.getInstance(args[ 0 ]); // 通过工厂获取实例 if (a != null ) { // 判断对象是否为空 a.say(); // 调用方法 } } } [java] view plain |
3.代理设计模式
指由一个代理主题来操作真实主题,真实主题执行具体的业务操作,而代理主题负责其他相关业务的处理。比如生活中的通过代理访问网络,客户通过网络代理连接网络(具体业务),由代理服务器完成用户权限和访问限制等与上网相关的其他操作(相关业务)。
示例代码如下:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | interface Network { // 定义Network接口 public void browse(); // 定义浏览的抽象方法 } class Real implements Network { // 真实的上网操作 public void browse() { // 覆写抽象方法 System.out.println( "上网浏览信息!" ); } } class Proxy implements Network { // 代理上网 private Network network; public Proxy(Network network) { // 设置代理的真实操作 this .network = network; // 设置代理的子类 } public void check() { // 身份验证操作 System.out.println( "检查用户是否合法!" ); } public void browse() { this .check(); // 调用具体的代理业务操作 this .network.browse(); // 调用真实的上网操作 } } public class ProxyDemo { public static void main(String args[]) { Network net = null ; // 定义接口对象 net = new Proxy( new Real()); // 实例化代理,同时传入代理的真实操作 net.browse(); // 调用代理的上网操作 } } |