java 单例模式和工厂模式实例详解
作者:wuxiao5570 时间:2023-04-07 22:10:19
单例模式根据实例化对象时机的不同分为两种:一种是饿汉式单例
,一种是懒汉式单例
。
私有的构造方法
指向自己实例的私有静态引用
以自己实例为返回值的静态的公有的方法
饿汉式单例
public class Singleton {
private static Singleton singleton = new Singleton();
private Singleton(){}
public static Singleton getInstance(){
return singleton;
}
}
懒汉式单例
public class Singleton {
private static Singleton singleton;
private Singleton(){}
public static synchronized Singleton getInstance(){
if(singleton==null){
singleton = new Singleton();
}
return singleton;
}
}
工厂方法模式代码
interface IProduct {
public void productMethod();
}
class Product implements IProduct {
public void productMethod() {
System.out.println("产品");
}
}
interface IFactory {
public IProduct createProduct();
}
class Factory implements IFactory {
public IProduct createProduct() {
return new Product();
}
}
public class Client {
public static void main(String[] args) {
IFactory factory = new Factory();
IProduct prodect = factory.createProduct();
prodect.productMethod();
}
}抽象工厂模式代码
interface IProduct1 {
public void show();
}
interface IProduct2 {
public void show();
}
class Product1 implements IProduct1 {
public void show() {
System.out.println("这是1型产品");
}
}
class Product2 implements IProduct2 {
public void show() {
System.out.println("这是2型产品");
}
}
interface IFactory {
public IProduct1 createProduct1();
public IProduct2 createProduct2();
}
class Factory implements IFactory{
public IProduct1 createProduct1() {
return new Product1();
}
public IProduct2 createProduct2() {
return new Product2();
}
}
public class Client {
public static void main(String[] args){
IFactory factory = new Factory();
factory.createProduct1().show();
factory.createProduct2().show();
}
}
希望本文对各位朋友有所帮助
来源:http://blog.csdn.net/wuxiao5570/article/details/70154159
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java 继承与多态超详细梳理
![](https://img.aspxhome.com/file/2023/6/59736_0s.png)
关于mybatis遇到Integer类型的参数时动态sql需要注意条件
详解JAVA 弱引用
学会IDEA REST Client后就可以丢掉postman了
![](https://img.aspxhome.com/file/2023/6/62766_0s.png)
Swagger注解-@ApiModel和@ApiModelProperty的用法
Android中调用另一个Activity并返回结果(选择头像功能为例)
![](https://img.aspxhome.com/file/2023/0/130310_0s.gif)
Android实现Tab布局的4种方式(Fragment+TabPageIndicator+ViewPager)
![](https://img.aspxhome.com/file/2023/6/114336_0s.gif)
Springboot 全局时间格式化操作
![](https://img.aspxhome.com/file/2023/2/94332_0s.jpg)
Java+opencv3.2.0实现人脸检测功能
![](https://img.aspxhome.com/file/2023/6/64556_0s.jpg)
C#中Request.Cookies 和 Response.Cookies 的区别分析
java开发ServiceLoader实现机制及SPI应用
![](https://img.aspxhome.com/file/2023/8/69728_0s.png)
Java多线程同步工具类CyclicBarrier的使用
SpringFramework应用接入Apollo配置中心过程解析
Hibernate中的多表查询及抓取策略
一次排查@CacheEvict注解失效的经历及解决
![](https://img.aspxhome.com/file/2023/0/59200_0s.png)
在Spring Boot中使用Spring-data-jpa实现分页查询
![](https://img.aspxhome.com/file/2023/5/89595_0s.png)
Java集合之Set接口及其实现类精解
![](https://img.aspxhome.com/file/2023/0/63670_0s.png)
Unity游戏之存储数据
![](https://img.aspxhome.com/file/2023/6/108686_0s.jpg)
OpenGL ES实现光照效果(六)
![](https://img.aspxhome.com/file/2023/6/137706_0s.gif)
C#中的静态成员、静态方法、静态类介绍
![](https://img.aspxhome.com/file/2023/1/92961_0s.jpg)