`

从spring开始--002

 
阅读更多

单例模式(Singleton):保证一个类仅有一个实例,并提供一个访问它的全局访问点。

 

 

 几种经典的实现方式

package com.ghods.lesson1.designpattern.singleton;

/**
 * 线程安全的 懒汉模式
 */
public class Singleton {
	private Singleton() {
	}

	private static Singleton instance;

	public static synchronized Singleton getInstance() {
		if (instance == null) {
			instance = new Singleton();
		}
		return instance;
	}
}

/**
 * 饿汉模式
 */
class Singleton2 {
	private Singleton2() {
	}

	private static Singleton2 instance = new Singleton2();

	public static Singleton2 getInstance() {
		return instance;
	}
}

/**
 * 双重校验锁 的懒汉模式
 */
class Singleton3 {
	private Singleton3() {
	}

	private static Singleton3 singleton = null;

	public static Singleton3 getInstance() {
		if (singleton == null) {
			synchronized (Singleton3.class) {
				if (singleton == null) {
					singleton = new Singleton3();
				}
			}
		}
		return singleton;
	}
}

 推荐blog文章:http://www.blogjava.net/kenzhh/archive/2013/07/05/357824.html

 Prototype(原型模式):用原型实例指定创建对象的种类,并且通过拷贝这个原型来创建新的对象。

 

package com.ghods.lesson1.designpattern.prototype;

/**
 * 抽象原型角色
 */
public abstract class Prototype implements Cloneable {
	private A a;

	public A getA() {
		return a;
	}

	public void setA(A a) {
		this.a = a;
	}

	/**
	 * 克隆自身方法
	 * 
	 * @return
	 */
	public Object clone() throws CloneNotSupportedException {
		return super.clone();
	}

}

class A {
        public A(){}
	public A(String str) {
		this.str = str;
	}

	private String str = "默认值";

	public String getStr() {
		return str;
	}

	public void setStr(String str) {
		this.str = str;
	}
}

 

 

package com.ghods.lesson1.designpattern.prototype;

/**
 * 具体原型实现
 */
public class ConcretePrototype extends Prototype{
    
    public static void main(String [] args) throws CloneNotSupportedException{
        Prototype prototype = new ConcretePrototype();
        prototype.setA(new A("原型的值"));
        ConcretePrototype concretePrototype1 = (ConcretePrototype) prototype.clone();
        //将会输出‘原型的值’,而不是‘默认值’;因此这是浅复制
        System.out.println(concretePrototype1.getA().getStr());
    }
}

 如果需要深度复制,当然需要重写Prototype的clone方法了。

 

	public Object clone() throws CloneNotSupportedException {
		this.a = new A();
		return super.clone();
	}

 

其它设计模式可以参见《Design Patterns: Elements of Reusable Object-Oriented Software》(即后述《设计模式》一书),由 Erich Gamma、Richard Helm、Ralph Johnson 和 John Vlissides 合著(Addison-Wesley,1995)。这几位作者常被称为"四人组(Gang of Four)",而这本书也就被称为"四人组(或 GoF)"书。

 


 

 

  • 大小: 4.7 KB
分享到:
评论

相关推荐

    开发工具 spring-web-4.3.6.RELEASE

    开发工具 spring-web-4.3.6.RELEASE开发工具 spring-web-4.3.6.RELEASE开发工具 spring-web-4.3.6.RELEASE开发工具 spring-web-4.3.6.RELEASE开发工具 spring-web-4.3.6.RELEASE开发工具 spring-web-4.3.6.RELEASE...

    开发工具 框架JAR spring-framework-4.3.6.RELEASE-dist

    开发工具 框架JAR spring-framework-4.3.6.RELEASE-dist开发工具 框架JAR spring-framework-4.3.6.RELEASE-dist开发工具 框架JAR spring-framework-4.3.6.RELEASE-dist开发工具 框架JAR spring-framework-4.3.6....

    spring-ai-core-0.8.1

    spring-ai-core 0.8.1,解决大家使用2023.0.1.0 版本 Spring Cloud Alibaba 依赖,代码依赖下载报错问题, <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies <version>...

    spring-context-support-1.0.10-API文档-中文版.zip

    赠送jar包:spring-context-support-1.0.10.jar; 赠送原API文档:spring-context-support-1.0.10-javadoc.jar; 赠送源代码:spring-context-support-1.0.10-sources.jar; 赠送Maven依赖信息文件:spring-context-...

    spring-aop.jar各个版本

    spring-aop-1.1.1.jar spring-aop-1.2.6.jar spring-aop-1.2.9.jar spring-aop-2.0.2.jar spring-aop-2.0.6.jar spring-aop-2.0.7.jar spring-aop-2.0.8.jar spring-aop-2.0.jar spring-aop-2.5.1.jar spring-aop-...

    spring-cglib-repack-3.2.0.jar和spring-objenesis-repack-2.1.jar

    在这个问题中,我们遇到了两个关键的jar包:`spring-cglib-repack-3.2.0.jar`和`spring-objenesis-repack-2.1.jar`。这两个jar包对于理解Spring框架的工作原理以及它们在实际应用中的作用至关重要。 首先,`spring-...

    开发工具 spring-aop-4.3.6.RELEASE

    开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE...

    spring-plugin-core-2.0.0.RELEASE-API文档-中文版.zip

    赠送jar包:spring-plugin-core-2.0.0.RELEASE.jar; 赠送原API文档:spring-plugin-core-2.0.0.RELEASE-javadoc.jar; 赠送源代码:spring-plugin-core-2.0.0.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring...

    spring-context.jar

    spring-context-1.2.8.jar, spring-context-1.2.9.jar, spring-context-2.0-m2.jar, spring-context-2.0.1.jar, spring-context-2.0.2.jar, spring-context-2.0.4.jar, spring-context-2.0.6.jar, spring-context-...

    开发工具 spring-core-4.3.6.RELEASE

    开发工具 spring-core-4.3.6.RELEASE开发工具 spring-core-4.3.6.RELEASE开发工具 spring-core-4.3.6.RELEASE开发工具 spring-core-4.3.6.RELEASE开发工具 spring-core-4.3.6.RELEASE开发工具 spring-core-4.3.6....

    spring-data-commons-2.0.6.RELEASE-API文档-中文版.zip

    赠送jar包:spring-data-commons-2.0.6.RELEASE.jar; 赠送原API文档:spring-data-commons-2.0.6.RELEASE-javadoc.jar; 赠送源代码:spring-data-commons-2.0.6.RELEASE-sources.jar; 赠送Maven依赖信息文件:...

    spring-session-data-redis-2.0.4.RELEASE-API文档-中英对照版.zip

    赠送jar包:spring-session-data-redis-2.0.4.RELEASE.jar; 赠送原API文档:spring-session-data-redis-2.0.4.RELEASE-javadoc.jar; 赠送源代码:spring-session-data-redis-2.0.4.RELEASE-sources.jar; 赠送...

    spring-security-crypto-5.6.1-API文档-中文版.zip

    赠送jar包:spring-security-crypto-5.6.1.jar; 赠送原API文档:spring-security-crypto-5.6.1-javadoc.jar; 赠送源代码:spring-security-crypto-5.6.1-sources.jar; 赠送Maven依赖信息文件:spring-security-...

    thymeleaf-spring5-3.0.12.RELEASE-API文档-中文版.zip

    赠送jar包:thymeleaf-spring5-3.0.12.RELEASE.jar; 赠送原API文档:thymeleaf-spring5-3.0.12.RELEASE-javadoc.jar; 赠送源代码:thymeleaf-spring5-3.0.12.RELEASE-sources.jar; 赠送Maven依赖信息文件:...

    seata-spring-boot-starter-1.3.0-API文档-中文版.zip

    赠送jar包:seata-spring-boot-starter-1.3.0.jar; 赠送原API文档:seata-spring-boot-starter-1.3.0-javadoc.jar; 赠送源代码:seata-spring-boot-starter-1.3.0-sources.jar; 赠送Maven依赖信息文件:seata-...

    spring-security-crypto-5.5.2-API文档-中文版.zip

    赠送jar包:spring-security-crypto-5.5.2.jar; 赠送原API文档:spring-security-crypto-5.5.2-javadoc.jar; 赠送源代码:spring-security-crypto-5.5.2-sources.jar; 赠送Maven依赖信息文件:spring-security-...

    spring-security-oauth2-2.3.5.RELEASE-API文档-中文版.zip

    赠送jar包:spring-security-oauth2-2.3.5.RELEASE.jar; 赠送原API文档:spring-security-oauth2-2.3.5.RELEASE-javadoc.jar; 赠送源代码:spring-security-oauth2-2.3.5.RELEASE-sources.jar; 赠送Maven依赖信息...

    spring-security-core-5.2.0.RELEASE-API文档-中文版.zip

    赠送jar包:spring-security-core-5.2.0.RELEASE.jar; 赠送原API文档:spring-security-core-5.2.0.RELEASE-javadoc.jar; 赠送源代码:spring-security-core-5.2.0.RELEASE-sources.jar; 赠送Maven依赖信息文件:...

    spring-security-core-5.3.9.RELEASE-API文档-中文版.zip

    赠送jar包:spring-security-core-5.3.9.RELEASE.jar; 赠送原API文档:spring-security-core-5.3.9.RELEASE-javadoc.jar; 赠送源代码:spring-security-core-5.3.9.RELEASE-sources.jar; 赠送Maven依赖信息文件:...

    nacos-config-spring-boot-autoconfigure-0.2.7-API文档-中文版.zip

    赠送jar包:nacos-config-spring-boot-autoconfigure-0.2.7.jar; 赠送原API文档:nacos-config-spring-boot-autoconfigure-0.2.7-javadoc.jar; 赠送源代码:nacos-config-spring-boot-autoconfigure-0.2.7-sources...

Global site tag (gtag.js) - Google Analytics