设计模式 Day02
文章总结自B站尚硅谷
1. UML类图
是一种用于软件系统和分析的语言工具,用于帮助软件开发人员进行思考和记录思考的结果。用来描述类与类之间的关系。
在idea中确实没有找到类似于老师使用的eclipse的插件,网上找了许多教程也没有找到相似的直接可以画的uml类图插件,所以就使用了idea原生的,但是缺点是必须要先写了代码才能去看类图!!
使用方法:
就会出现类似于这样的。
类之间的关系有五种:
-
依赖:在类中用到了对方。图中线就代表依赖关系。
-
泛化(继承):泛化关系就是继承关系,是依赖关系的一种特例。图中蓝线表示继承(泛化)。
-
实现:一个类实现了接口就存在实现关系,也是依赖关系的特例。
-
关联:实际上就是类与类之间的联系,也是依赖关系的特例。具有到航性:双向或者单向关系。
上图就是两个双向关系。下图为单向关系。
-
聚合:表示的是整体和部分的关系,整体与部分可以分开,它是关联关系的特例。
-
组合:表示的是整体和部分的关系,整体与部分不可以分开,它是关联关系的特例。
上图中head类与person就是组合关系,IDcard类与perso就是聚合关系。
总结:
所有的类与类之间的关系,都为依赖关系(除非没有关系),其它的关系不过是特殊的依赖关系。泛化就是继承关系,实现就是一个类实现的接口,就称接口与类有实现关系,关联关系就是指两个类之间的关系,有指定性(双向或者单向),单向就是你中有我我中没你,双向就是你中有我我中也有你,组合和聚合也是特殊的关联关系,组合是同生共死不能分开(代码实现是获得类的实例化对象),聚合是你死了我也一样活着。
2. 设计模式
设计模式代表了对某类问题通用的最佳解决方案,本质是提高软件的维护性,通用性和扩展性,并且降低软件的复杂度。
设计模式共分为三种类型:
- 构建型:单例模式、抽象工厂模式、原型模式、构造者模式、工厂模式。
- 结构型:适配器模式、桥接模式、装饰模式、组合模式、外观模式、享元模式、代理模式。
- 行为型:模板方法模式、命令模式、访问者模式、迭代器模式、观察者模式、中介者模式、备忘录模式、解释器模式、状态模式、策略模式、责任链(职责链)模式。
2. 单例模式
单例模式就是采取一定的方法保证整个系统中,对某个类只能存在一个对象实例,并且只提供一个该类获取其对象实例的方法。
2.1 饿汉式(静态常量)
/**
* @date 2020/8/19 15:07
* 静态变量 饿汉式
*/
public class Demo01 {
public static void main(String[] args) {
Singleton instance = Singleton.getInstance();
Singleton instance2 = Singleton.getInstance();
System.out.println(instance.hashCode()+"----------------------------"+instance2.hashCode());
}
}
// 饿汉式 静态变量
class Singleton{
// 私有构造器方法 不能 new 对象
private Singleton(){
}
// 本类内部创建对象实例
private final static Singleton instance = new Singleton();
// 提供公有的静态方法,返回对象实例
public static Singleton getInstance(){
return instance;
}
}
优点:写法简单,在类加载的时候就完成了实例化,避免了线程同步问题。
缺点:在类转载的时候就会完成实例化,没有达到懒加载的效果,如果从始至终都没使用过这实例,则会照成内存浪费。
2.2. 饿汉式(静态代码块)
/**
* @date 2020/8/19 15:23
* 饿汉式 静态代码块
*/
public class Demo02 {
public static void main(String[] args) {
Singleton02 singleton = Singleton02.getInstance();
Singleton02 singleton02 = Singleton02.getInstance();
System.out.println(singleton.hashCode() == singleton02.hashCode());
}
}
// 静态代码块 饿汉式
class Singleton02{
// 私有化构造方法 外部不能new
private Singleton02(){
}
// 类内部实例化对象
private static Singleton02 instance;
// 在静态代码块中创建单例对象
static {
instance = new Singleton02();
}
// 返回实例对象
public static Singleton02 getInstance(){
return instance;
}
}
优缺点与使用静态常量一样,只将实例化的过程放在了静态代码块中。
2.3 懒汉式(线程不安全)
/**
* @date 2020/8/20 8:05
* 单例模式 懒汉式 线程不安全
*/
public class Demo03 {
public static void main(String[] args) {
for (int i = 0; i < 20; i++) {
new Thread(()-> {
Singleton03 singleton03 = Singleton03.getInstance();
System.out.println(Thread.currentThread().getName()+"-------"+singleton03.hashCode());
},"线程"+i).start();
}
}
}
class Singleton03{
private static Singleton03 instance;
private Singleton03(){
}
// 懒汉式 调用方法时才会创建实例化对象
public static Singleton03 getInstance(){
if (instance == null){
instance = new Singleton03();
}
return instance;
}
}
优点:懒加载
缺点:单线程才可以使用;在多线程下另一个线程也进入了if
循环,就会产生多个实例。
2.4 懒汉式(线程安全)
/**
* @date 2020/8/20 8:14
* 单例模式 懒汉式 线程安全
*/
public class Demo04 {
public static void main(String[] args) {
for (int i = 0; i < 20; i++) {
new Thread(()-> {
Singleton04 singleton04 = Singleton04.getInstance();
System.out.println(Thread.currentThread().getName()+"-------"+singleton04.hashCode());
},"线程"+i).start();
}
}
}
class Singleton04{
private Singleton04(){
}
private static Singleton04 instance;
// 加入 synchronized 关键字 使得线程同步 解决的多线程下不同步方法的问题。
public synchronized static Singleton04 getInstance(){
if (instance == null){
instance = new Singleton04();
}
return instance;
}
}
相较于线程不安全的方法,在构建实例对象的方法上添加了synchronized
使得方法为同步方法,解决了在多线程条件下,线程不同步的问题。但是在执行过程中必须要等待前方线程执行完毕再次执行getInstance
方法,而且这个方法返回的是同一个instance
所以并不需要多次执行该方法,使得效率非常低!
2.5 懒汉式(同步代码块)
/**
* @date 2020/8/20 8:21
* 单例模式 懒汉式 同步代码块
*/
/**
* @date 2020/8/20 8:21
* 单例模式 懒汉式 同步代码块
*/
/**
* @date 2020/8/20 8:21
* 单例模式 懒汉式 同步代码块
*/
public class Demo05 {
public static void main(String[] args) {
for (int i = 0; i < 20; i++) {
new Thread(()-> {
Singleton05 singleton05 = Singleton05.getInstance();
System.out.println(Thread.currentThread().getName()+"-------"+singleton05.hashCode());
},"线程"+i).start();
}
}
}
class Singleton05{
private Singleton05(){
}
private static Singleton05 instance;
public static Singleton05 getInstance(){
if (instance == null){
synchronized(Singleton05.class){
instance = new Singleton05();
}
}
return instance;
}
}
这种方式实际上并没有起到线程同步的作用,因为在线程同步之前就对instance
进行的判断。
2.6 双重检查
/**
* @date 2020/8/20 8:30
* 单例模式 双重检查
*/
public class Demo06 {
public static void main(String[] args) {
for (int i = 0; i < 20; i++) {
new Thread(()-> {
Singleton06 singleton06 = Singleton06.getInstance();
System.out.println(Thread.currentThread().getName()+"-------"+singleton06.hashCode());
},"线程"+i).start();
}
}
}
class Singleton06{
private static volatile Singleton06 instance;
private Singleton06(){
}
public static Singleton06 getInstance(){
if (instance == null){
synchronized (Singleton06.class){
if (instance == null){
instance = new Singleton06();
}
}
}
return instance;
}
}
在解决线程安全问题的同时也解决了懒加载的问题,同时也保证了效率。
2.7 静态内部类
/**
* @date 2020/8/20 8:53
* 单例模式 静态内部类
*/
public class Demo07 {
public static void main(String[] args) {
for (int i = 0; i < 20; i++) {
new Thread(()->{
Singleton07 instance = Singleton07.getInstance();
System.out.println(Thread.currentThread().getName()+"---------"+instance.hashCode());
},"线程"+i).start();
}
}
}
class Singleton07{
private static volatile Singleton07 instance;
private Singleton07(){}
// 静态内部类
private static class SingletonInstance{
private static final Singleton07 INSTANCE = new Singleton07();
}
// 提供静态方法返回静态内部类中的实例化对象。
public static Singleton07 getInstance(){
return SingletonInstance.INSTANCE;
}
}
采用了类装载的机制,来保证初始化实例时只有一个线程。类的静态属性只会在第一个加载类的时候进行实例化,在调用getInstance
方法时才会装在SingletonInstance
类。
优点:线程安全,效率高。
2.8 枚举
/**
* @date 2020/8/20 9:05
* 单例模式 枚举
*/
public class Demo08 {
public static void main(String[] args) {
Singleton08 instance = Singleton08.INSTANCE;
Singleton08 instance1 = Singleton08.INSTANCE;
System.out.println(instance == instance1);
}
}
enum Singleton08{
INSTANCE;
public void sayOK(){
System.out.println("OK");
}
}
不仅能够避免线程同步问题,而且能防止反序列化重新创建新的对象。
2.9 总述
集合上方代码,推荐使用:静态内部类、枚举、双重检查的方式创建单例对象。
单例模式保证了系统内存中只有一个对象,节省了系统资源,对于一些需要频繁销毁的对象,使用单例模式可以提高性能。