单例模式有什么好处,你熟悉的设计模式有哪些?写出单例模式的实现代码
答案:1 悬赏:50 手机版
解决时间 2021-03-21 07:25
- 提问者网友:爱唱彩虹
- 2021-03-21 00:35
单例模式有什么好处,你熟悉的设计模式有哪些?写出单例模式的实现代码
最佳答案
- 五星知识达人网友:十年萤火照君眠
- 2021-03-21 01:37
一共23种设计模式!
引用《软件秘笈-设计模式那点事》书籍:
按照目的来分,设计模式可以分为创建型模式、结构型模式和行为型模式。
创建型模式用来处理对象的创建过程;结构型模式用来处理类或者对象的组合;行为型模式用来对类或对象怎样交互和怎样分配职责进行描述。
创建型模式用来处理对象的创建过程,主要包含以下5种设计模式:
? 工厂方法模式(Factory Method Pattern)
? 抽象工厂模式(Abstract Factory Pattern)
? 建造者模式(Builder Pattern)
? 原型模式(Prototype Pattern)
? 单例模式(Singleton Pattern)
结构型模式用来处理类或者对象的组合,主要包含以下7种设计模式:
? 适配器模式(Adapter Pattern)
? 桥接模式(Bridge Pattern)
? 组合模式(Composite Pattern)
? 装饰者模式(Decorator Pattern)
? 外观模式(Facade Pattern)
? 享元模式(Flyweight Pattern)
? 代理模式(Proxy Pattern)
行为型模式用来对类或对象怎样交互和怎样分配职责进行描述,主要包含以下11种设计模式:
? 责任链模式(Chain of Responsibility Pattern)
? 命令模式(Command Pattern)
? 解释器模式(Interpreter Pattern)
? 迭代器模式(Iterator Pattern)
? 中介者模式(Mediator Pattern)
? 备忘录模式(Memento Pattern)
? 观察者模式(Observer Pattern)
? 状态模式(State Pattern)
? 策略模式(Strategy Pattern)
? 模板方法模式(Template Method Pattern)
? 访问者模式(Visitor Pattern)
单例模式实现1:
public class Singleton {
// 类共享实例对象
private static Singleton singleton = null;
// 私有构造方法
private Singleton() {
System.out.println(-- this is Singleton!!!);
}
// 获得单例方法
public synchronized static Singleton getInstance() {
// 判断 共享对象是否为null ,如何为null则new一个新对象
if (singleton == null) {
singleton = new Singleton();
}
return singleton;
}
}
单例模式实现2:
public class Singleton {
// 类共享实例对象 实例化
private s......余下全文>>
引用《软件秘笈-设计模式那点事》书籍:
按照目的来分,设计模式可以分为创建型模式、结构型模式和行为型模式。
创建型模式用来处理对象的创建过程;结构型模式用来处理类或者对象的组合;行为型模式用来对类或对象怎样交互和怎样分配职责进行描述。
创建型模式用来处理对象的创建过程,主要包含以下5种设计模式:
? 工厂方法模式(Factory Method Pattern)
? 抽象工厂模式(Abstract Factory Pattern)
? 建造者模式(Builder Pattern)
? 原型模式(Prototype Pattern)
? 单例模式(Singleton Pattern)
结构型模式用来处理类或者对象的组合,主要包含以下7种设计模式:
? 适配器模式(Adapter Pattern)
? 桥接模式(Bridge Pattern)
? 组合模式(Composite Pattern)
? 装饰者模式(Decorator Pattern)
? 外观模式(Facade Pattern)
? 享元模式(Flyweight Pattern)
? 代理模式(Proxy Pattern)
行为型模式用来对类或对象怎样交互和怎样分配职责进行描述,主要包含以下11种设计模式:
? 责任链模式(Chain of Responsibility Pattern)
? 命令模式(Command Pattern)
? 解释器模式(Interpreter Pattern)
? 迭代器模式(Iterator Pattern)
? 中介者模式(Mediator Pattern)
? 备忘录模式(Memento Pattern)
? 观察者模式(Observer Pattern)
? 状态模式(State Pattern)
? 策略模式(Strategy Pattern)
? 模板方法模式(Template Method Pattern)
? 访问者模式(Visitor Pattern)
单例模式实现1:
public class Singleton {
// 类共享实例对象
private static Singleton singleton = null;
// 私有构造方法
private Singleton() {
System.out.println(-- this is Singleton!!!);
}
// 获得单例方法
public synchronized static Singleton getInstance() {
// 判断 共享对象是否为null ,如何为null则new一个新对象
if (singleton == null) {
singleton = new Singleton();
}
return singleton;
}
}
单例模式实现2:
public class Singleton {
// 类共享实例对象 实例化
private s......余下全文>>
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯