重学java 73.设计模式

本想送你一本沉思录,可该迷途知返的人是我

------ 24.6.18

设计模式

设计模式(Design pattern) ,是一套被反复使用、经过分类编目的、代码设计经验的总结,使用设计模式是为了可重用代码、保证代码可靠性、程序的重用性,稳定性 。

1995 年,GoF(Gang of Four,四人组)合作出版了《设计模式:可复用面向对象软件的基础》一书,某收录了 23种设计模式。<大话设计模式>

总体来说设计模式分为三大类:

创建型模式,共五种:工厂方法模式、抽象工厂模式、单例模式、建造者模式、原型模式。-->创建对象

结构型模式,共七种:适配器模式、装饰器模式、代理模式、外观模式、桥接模式、组合模式、享元模式。---> 对功能进行增强

行为型模式,共十一种:策略模式、模板方法模式、观察者模式、迭代子模式、责任链模式、命令模式、备忘录模式、状态模式、访问者模式、中介者模式、解释器模式。

一、模板方法设计模式

模板方法(Template Method)模式: 定义一个操作中的算法的骨架,而将一些步骤延迟到子类中。明确了一部分功能,而另一部分功能不明确。需要延伸到子类中实现

示例:

java 复制代码
public class QuanJuDe extends Hotel{
    @Override
    public void eatCai(){
        System.out.println("薄饼");
        System.out.println("卷烤鸭");
        System.out.println("蘸酱");
        System.out.println("葱丝");
        System.out.println("吃烤鸭");
    }
}
java 复制代码
public class ZhangLiang extends Hotel{
    @Override
    public void eatCai(){
        System.out.println("选菜");
        System.out.println("选味道");
        System.out.println("吃菜");
    }
}
java 复制代码
public class Demo300ModelPattern {
    public static void main(String[] args) {
        QuanJuDe quanJuDe = new QuanJuDe();
        quanJuDe.eat();

        System.out.println("__________________");
        ZhangLiang zhangLiang = new ZhangLiang();
        zhangLiang.eat();
    }
}

二、单例模式

1.目的

让一个类只产生一个对象,供外界使用

2.分类:

a.饿汉式

b.懒汉式

a、饿汉式

java 复制代码
public class SingletonHun {
    /*
    防止外界随意使用构造方法new对象,我们需要将构造私有化
     */
    private SingletonHun(){

    }

    /*
    为了赶紧new对象,我们new对象的时候变成静态的,让其随着类的加载而加载
    为了不让外界随便使用类名调用此静态对象,我们将其变成private
     */
    private static SingletonHun singleton = new SingletonHun();

    /*
    为了将内部new出来的对象给外界我们可以定义 一个方法,将内部的对象返回给外界
     */
    public static SingletonHun getSingleton(){
        return singleton;
    }
}
java 复制代码
public class Demo301Test {
    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            SingletonHun singleton = SingletonHun.getSingleton();
            System.out.println(singleton);
        }
    }
}

b、懒汉式

java 复制代码
public class SingletonLazy {
    /*
    防止外界随意使用构造方法new对象,我们需要将构造私有化
     */
    private SingletonLazy() {

    }

    /*
    懒汉式,不着急new对象
     */
    private static SingletonLazy singletonLazy = null;

    /*
    为了将内部new出来的对象给外界
    定义一个方法,将内部new出来的对象返回
     */
    public static SingletonLazy getSingletonLazy() {
        // 加锁 如果singletonLazy不是null,就没必要抢锁了,直接返回,是null在抢锁
        if (singletonLazy == null) {
            synchronized (SingletonLazy.class) {
                if (singletonLazy == null) {
                    singletonLazy = new SingletonLazy();
                }
            }
        }
        return singletonLazy;
    }
}
java 复制代码
public class Demo302Test02 {
    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            SingletonLazy singleton = SingletonLazy.getSingletonLazy();
            System.out.println(singleton);
        }
    }
}

小结:构造私有

对象私有 静态的

相关推荐
饮水桶_T6 小时前
Java调用大语言模型 笔记
笔记·设计模式·电脑·ai编程
ssshooter1 天前
Replit 项目本地跑:一份实测过的改造清单
前端·后端·设计模式
饮水桶_T1 天前
Java调用大语言模型、多轮对话 笔记
笔记·算法·设计模式·电脑·ai编程
AI酒旅实战开发2 天前
旅行 AI Agent 的 7 种设计模式:从工具调用到交易闭环
人工智能·设计模式
geovindu4 天前
rust: Functional Options Pattern
开发语言·后端·设计模式·rust·函数式选项模式·惯用法型模式
geovindu5 天前
rust: Timing Functions Pattern
开发语言·后端·设计模式·rust·计时函数模式·性能分析型模式
Mh10 天前
我是如何设计品牌站组件库的?
前端·css·设计模式
是2的10次方啊11 天前
设计模式与生活·总论:AI 能替你砌墙,替不了你打地基
设计模式
dahaihua33311 天前
用 SysML 表达航电系统架构(附建模示例)
设计模式·重构·系统架构·集成测试·需求分析·学习方法
wy_hhxx12 天前
《Agent设计模式 图解可复用智能体架构》上篇内容整理
设计模式·架构·ai agent