一、单例模式也成单实例模式
1、什么是单例模式
是指在代码结构中只包含一个被称为单例的特殊类。单例模式保证了系统在运行过程中所有实例都是相等的。即这个类只有一个对象实例,比如 public class A{}
- A a = new A();
- A b = new A();
那么a==b吗?结论是false
若A是一个单例模式的类,那么a==b吗 结果是true。
2、开发要求
①、需要定义私有的构造方法
②、私有的静态的本类类型的变量(可以直接new 也可以设置null)
③、公开的静态的返回值是本类对象的方法
3、案例代码
- package com.rock.designpattern;
- /**
- * 单实例模式的代码实现
- * FileName: com.rock.designpattern MySingleton.java
- * TODO:
- * Copyright: Copyright (c) 2015-2016 All Rights Reserved. Company: 01skill-soft.com Inc.
- * @author: Rock
- * @Date: 2017年7月25日:上午10:52:46
- * @version: 1.0
- *
- * Modification History: Date Author Version Description
- * ----------------------------------------------------------------------
- * 2017年7月25日 Rock 1.0 1.0 Version
- */
- public class MySingleton {
- // ①、定义私有的静态的本类类型变量
- private static MySingleton singleton = null;
- // ②、定义私有构造方法
- private MySingleton() {}
- // ③、定义个公开的静态的返回值是本类类型的方法
- public static MySingleton newInstance() {
- if (singleton == null) singleton = new MySingleton();
- return singleton;
- }
- }
二、代理模式
1、什么是代理模式
代理模式实际上访问对象的一种方式,以代理方式控制对目标对象的访问,即代理对象是在访问者和目标对象之间起到中介的作用。比如,烟草公司生产的香烟是不针对个人用户销售的,你要买烟怎么办,你只能去指定的烟草销售点(比如超市,商店等),那么”香烟”即是目标对象,而”超市”即为代理对象,”你”即为请求对象或者客户端
那么代理模式在什么情况下使用呢?某些情况下,要想调用对象中的部分(全部)方法,是有特殊要求的,比如要完成安全性检查,之后才能调用。或者在目标方法执行完毕后,要做一些收尾工作,比如释放资源、日志存储等工作
2、代理模式分类
① 、静态代理模式 ②、动态代理模式。我在这里向大家阐述静态代理模式
3、开发步骤
开发步骤:
①、定义目标对象(也可以是抽象的) 在其中定义run()方法
②、定义代理类
需要有一个目标对象的变量,然后在代理类中定义到目标类的访问的方法(推荐,代理类和目标类(被代理类)方法名一致) 比如run()方法
③、在代理类中调用目标类的方法前或者后 ,执行其他的一些功能。
4、代码案例
- package com.rock.designpattern;
- /**
- * 代理模式: 目标对象,即为被代理的对象
- * FileName: com.rock.designpattern TargetObject.java
- * TODO:
- * Copyright: Copyright (c) 2015-2016 All Rights Reserved. Company: 01skill-soft.com Inc.
- * @author: Rock
- * @Date: 2017年7月25日:上午11:10:19
- * @version: 1.0
- *
- * Modification History: Date Author Version Description
- * ----------------------------------------------------------------------
- * 2017年7月25日 Rock 1.0 1.0 Version
- *
- */
- public class TargetObject {
- // 目标对象的方法,将来用于被代理类代理
- public void run() {
- System.out.println("TargetObject.run() is running.....");
- }
- }
- package com.rock.designpattern;
- /**
- * 静态代理: 代理对象,即提供了对TargetObject 对象的统一访问点
- * FileName: com.rock.designpattern ObjectProxy.java
- * TODO:
- * Copyright: Copyright (c) 2015-2016 All Rights Reserved. Company: 01skill-soft.com Inc.
- * @author: Rock
- * @Date: 2017年7月25日:上午11:11:40
- * @version: 1.0
- *
- * Modification History: Date Author Version Description
- * ----------------------------------------------------------------------
- * 2017年7月25日 Rock 1.0 1.0 Version
- *
- */
- public class ObjectProxy {
- // 定义目标类型(被代理的类型)的对象,用于对目标类型的访问
- private TargetObject obj = new TargetObject();
- /**
- * @Author :Rock
- * @Date :2017年7月25日 下午1:36:56
- * @TODO :可以在代理类的这个方法中完成安全性检查、初始化、日志处理等相关工作。
- * 在执行目标方法前,或者是执行完毕后调用
- * @Arguments:
- */
- public void run() {
- System.out.println("doSomeThing ...before");
- obj.run();
- System.out.println("doSomeThing ...after");
- }
- }
- package com.rock.designpattern;
- import static org.junit.Assert.*;
- import org.junit.Test;
- /**
- *
- * FileName: com.rock.designpattern PatternTest.java
- * TODO: 单例模式&代理模式统一测试
- * Copyright: Copyright (c) 2015-2016 All Rights Reserved. Company: 01skill-soft.com Inc.
- * @author: Rock
- * @Date: 2017年7月25日:上午10:56:10
- * @version: 1.0
- *
- * Modification History: Date Author Version Description
- * ----------------------------------------------------------------------
- * 2017年7月25日 Rock 1.0 1.0 Version
- */
- public class PatternTest {
- @Test
- public void testSingleton() {
- MySingleton m1=MySingleton.newInstance();
- MySingleton m2=MySingleton.newInstance();
- MySingleton m3=MySingleton.newInstance();
- System.out.println( m3==m2);
- }
- @Test
- public void run(){
- System.out.println("PatternTest.run() is running......");
- }
- @Test
- public void testProxy(){
- ObjectProxy proxy=new ObjectProxy();
- proxy.run();
- }
- }
三、工厂模式
顾名思义,工厂模式适用于产品的生成及管理 有一个接口(抽象类)产品,多个产品接口的实现类,工厂类
①、普通工厂模式 :需要以一个产品的抽象父类(接口),多个产品实现类,一个工厂类
②、工厂方法工厂模式 :在工厂类中定义生产不同类型产品的方法 createBMW() createBYD() createAudi()
③、抽象工厂模式
*注意:工厂类:其中有一个方法,这个方法的返回值是产品的抽象父类(接口)
public Car createCar(String brand) 功能是生产产品的方法*
1、案例代码
1)、基本代码,产品相关对象
- package com.rock.designpattern;
- /**
- * FileName: com.rock.designpattern Car.java
- * TODO: 定义抽象父类
- * Copyright: Copyright (c) 2015-2016 All Rights Reserved. Company: 01skill-soft.com Inc.
- * @author: Rock
- * @Date: 2017年7月25日:上午11:24:41
- * @version: 1.0
- *
- * Modification History: Date Author Version Description
- * ----------------------------------------------------------------------
- * 2017年7月25日 Rock 1.0 1.0 Version
- */
- public interface Car {
- public void run();
- }
- package com.rock.designpattern;
- /**
- *
- * FileName: com.rock.designpattern Audi.java
- * TODO: 定义具体产品
- * Copyright: Copyright (c) 2015-2016 All Rights Reserved. Company: 01skill-soft.com Inc.
- * @author: Rock
- * @Date: 2017年7月25日:上午11:26:05
- * @version: 1.0
- *
- * Modification History: Date Author Version Description
- * ----------------------------------------------------------------------
- * 2017年7月25日 Rock 1.0 1.0 Version
- */
- public
- class
- Audi
- implements
- Car
- {
- /**
- * @Author :Rock
- * @Date :2017年7月25日 上午11:26:22
- * @TODO :
- * @see com.rock.designpattern.Car#run()
- */
- @Override
- public void run() {
- System.out.println("Audi.run() is running....");
- }
- }
- package com.rock.designpattern;
- /**
- *
- * FileName: com.rock.designpattern BMW.java
- * TODO: 定义具体产品
- * Copyright: Copyright (c) 2015-2016 All Rights Reserved. Company: 01skill-soft.com Inc.
- * @author: Rock
- * @Date: 2017年7月25日:上午11:25:36
- * @version: 1.0
- *
- * Modification History: Date Author Version Description
- * ----------------------------------------------------------------------
- * 2017年7月25日 Rock 1.0 1.0 Version
- */
- public
- class
- BMW
- implements
- Car
- {
- /**
- * @Author :Rock
- * @Date :2017年7月25日 上午11:25:50
- * @TODO :
- * @see com.rock.designpattern.Car#run()
- */
- @Override
- public void run() {
- System.out.println("BMW.run() is running.....");
- }
- }
- package com.rock.designpattern;
- /**
- *
- * FileName: com.rock.designpattern BYD.java
- * TODO: 定义具体产品
- * Copyright: Copyright (c) 2015-2016 All Rights Reserved. Company: 01skill-soft.com Inc.
- * @author: Rock
- * @Date: 2017年7月25日:上午11:29:50
- * @version: 1.0
- *
- * Modification History: Date Author Version Description
- * ----------------------------------------------------------------------
- * 2017年7月25日 Rock 1.0 1.0 Version
- *
- */
- public
- class
- BYD
- implements
- Car
- {
- /**
- * @Author :Rock
- * @Date :2017年7月25日 上午11:29:59
- * @TODO :
- * @see com.rock.designpattern.Car#run()
- */
- @Override
- public void run() {
- System.out.println("BYD.run() is running.......");
- }
- }
2、工厂模式设置,工厂模式比较常用的有如下几种方式,普通工厂模式、抽象工厂模式、工厂方法工厂模式的实现,下面一一通过代码向大家阐述:
1)、 普通工厂模式代码实现
- package com.rock.designpattern;
- /**
- *
- * FileName: com.rock.designpattern CarFactory.java
- * TODO: 工厂类:专门用于生产汽车的工厂类
- * Copyright: Copyright (c) 2015-2016 All Rights Reserved. Company: 01skill-soft.com Inc.
- * @author: Rock
- * @Date: 2017年7月25日:上午11:26:55
- * @version: 1.0
- *
- * Modification History: Date Author Version Description
- * ----------------------------------------------------------------------
- *
- * 2017年7月25日 Rock 1.0 1.0 Version
- *
- */
- public class CarFactory {
- /**
- * @Author :Rock
- * @Date :2017年7月25日 下午1:40:42
- * @TODO :当前工厂的主要功能是完成汽车产品的生产,这个方法会根据你的需要创建不同品牌的汽车
- * @Arguments:@param brand
- * @Arguments:@return
- */
- public Car createCar(String brand) {
- if ("BMW".toLowerCase().equals(brand)) {
- return new BMW();
- } else if ("AUDI".toLowerCase().equals(brand.toLowerCase())) {
- return new Audi();
- } else return new BYD();
- }
- }
2)、抽象工厂的工厂模式实现
- package com.rock.designpattern;
- /**
- * FileName: com.rock.designpattern CarAbstractFactory.java
- * TODO: 抽象工厂的工厂模式:工厂是抽象的
- * Copyright: Copyright (c) 2015-2016 All Rights Reserved. Company: 01skill-soft.com Inc.
- * @author: Rock
- * @Date: 2017年7月25日:下午1:25:20
- * @version: 1.0
- *
- * Modification History: Date Author Version Description
- * ----------------------------------------------------------------------
- * 2017年7月25日 Rock 1.0 1.0 Version
- *
- */
- public abstract class CarAbstractFactory {
- /**
- * @Author :Rock
- * @Date :2017年7月25日 下午1:44:03
- * @TODO :定义抽象的生产汽车的方法
- * @Arguments:@return
- */
- public abstract Car createCar();
- }
3)、工厂方法工厂模式实现
- package com.rock.designpattern;
- /**
- * FileName: com.rock.designpattern CarMethodFactory.java
- * TODO: 工厂方法工厂模式:在共产类中定义不同类型产品的生产方法,用于生成不同的汽车产品
- * Copyright: Copyright (c) 2015-2016 All Rights Reserved. Company: 01skill-soft.com Inc.
- * @author: Rock
- * @Date: 2017年7月25日:下午1:18:15
- * @version: 1.0
- *
- * Modification History: Date Author Version Description
- * ----------------------------------------------------------------------
- * 2017年7月25日 Rock 1.0 1.0 Version
- */
- public class CarMethodFactory {
- /**
- * @Author :Rock
- * @Date :2017年7月25日 下午1:42:44
- * @TODO :生产BMW汽车的方法
- * @Arguments:@return
- */
- public BMW createBMW() {
- return new BMW();
- }
- public Car createAUDI() {
- return new Audi();
- }
- public BYD createBYD() {
- return new BYD();
- }
- }
来源: http://blog.csdn.net/dl0246/article/details/77966611