java中23种设计模式详解

设计模式是软件开发中的重要概念,对于Java开发者来说,学习并理解23种设计模式是很有帮助的。下面我将为你提供每种设计模式的简短描述和示例代码。

  1. 抽象工厂(Abstract Factory)

提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。




public interface AnimalFactory {
    Animal createAnimal();
}
 
public class DogFactory implements AnimalFactory {
    public Animal createAnimal() {
        return new Dog();
    }
}
  1. 建造者模式(Builder)

将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。




public class Car {
    private String engine;
    private String wheel;
    // ...
}
 
public class CarBuilder {
    public Car buildCar(String engine, String wheel) {
        Car car = new Car();
        car.setEngine(engine);
        car.setWheel(wheel);
        return car;
    }
}
  1. 工厂方法(Factory Method)

定义一个用于创建对象的接口,让子类决定实例化哪个类。




public interface VehicleFactory {
    Vehicle createVehicle();
}
 
public class CarFactory implements VehicleFactory {
    public Vehicle createVehicle() {
        return new Car();
    }
}
  1. 原型(Prototype)

用原型实例指定创建对象的种类,并且通过复制这个原型来创建新的对象。




public class Car implements Cloneable {
    // ...
    public Car clone() {
        try {
            return (Car) super.clone();
        } catch (CloneNotSupportedException e) {
            throw new AssertionError();
        }
    }
}
  1. 单例(Singleton)

确保一个类只有一个实例,并提供一个全局访问点来访问这个唯一的实例。




public class DatabaseConnection {
    private static DatabaseConnection instance = null;
    private DatabaseConnection() {}
    public static synchronized DatabaseConnection getInstance() {
        if (instance == null) {
            instance = new DatabaseConnection();
        }
        return instance;
    }
}
  1. 适配器(Adapter)

将一个类的接口转换成客户希望的另一个接口。




public class AnimalAdapter implements Dog {
    private Animal animal;
    public AnimalAdapter(Animal animal) {
        this.animal = animal;
    }
    public void bark() {
        animal.makeNoise();
    }
}
  1. 桥接(Bridge)

将抽象部分与它的实现部分分离,使它们都可以独立地变化。




public interface Engine {
    void start();
}
 
public class V8Engine implements Engine {
    public void start() {
        System.out.println("V8 Engine starting.");
    }
}
  1. 组合(Composite)

将对象组合成树形结构以表示部分整体的关系,Composite使得用户对单个对象和组合对

最后修改于:2024年08月26日 13:11

评论已关闭

推荐阅读

Vue中使用mind-map实现在线思维导图
2024年08月04日
VUE
Web前端最全Vue实现免密登录跳转的方式_vue怎么样不登录返回首页,最强技术实现
2024年08月04日
VUE
vue3 项目搭建教程(基于create-vue,vite,Vite + Vue)
2024年08月04日
VUE
Vue-颜色选择器实现方案——>Vue-Color( 实战*1+ Demo*7)
2024年08月04日
VUE
Vue项目卡顿慢加载?这些优化技巧告诉你!_vue数据多渲染卡顿
2024年08月04日
VUE
vue中的keep-alive详解与应用场景
2024年08月04日
VUE
Vue、React实现excel导出功能(三种实现方式保姆级讲解)
2024年08月04日
vue-office/docx插件实现docx文件预览
2024年08月04日
VUE
java调用js文件的两种方法(支持V8引擎)
2024年08月04日
JavaScript:解决计算精度问题/mathjs/bignumber.js/big.js/decimal.js
2024年08月04日
两周从爬虫小白变大神 _yjs_js_security_passport
2024年08月04日
JS笔记(对象、函数、数组)
2024年08月04日
Markdown.js:强大的纯JavaScript Markdown解析器
2024年08月04日
Vue项目:js模拟点击a标签下载文件并重命名,URL文件地址下载方法、请求接口下载文件方法总结。
2024年08月04日
vue 父组件怎么获取子组件里面的data数据
2024年08月04日
VUE
个人开发实现AI套壳网站快速搭建(Vue+elementUI+SpringBoot)
2024年08月04日
el-table 表格封装并改造实现单元格可编辑
2024年08月04日
none
nodejs环境下创建vue项目、SSH密钥登陆!!!
2024年08月04日
vue+quill+element-ui实现视频、图片上传及缩放保姆级教程,轻松使用富文本
2024年08月04日
【three.js】22. Imported Models导入模型
2024年08月04日