JavaScript设计模式161
在JavaScript中,设计模式是指一组可重用的解决方案,用于解决常见编程问题。它们提供了经过验证的方法来构建健壮、可维护和可扩展的应用程序。
单例模式
单例模式确保一个类只有一个实例。它用于创建全局对象或确保只有一个实例被用来执行特定任务。示例代码如下:```javascript
class Singleton {
static instance;
constructor() {
if (!) {
= this;
}
return ;
}
}
```
工厂模式
工厂模式为创建对象提供一个接口,而不指定具体类。这允许在不修改客户端代码的情况下更改类的创建方式。示例代码如下:```javascript
class ShapeFactory {
static createShape(type) {
switch (type) {
case 'circle':
return new Circle();
case 'square':
return new Square();
default:
return null;
}
}
}
```
观察者模式
观察者模式定义了一种一对多的依赖关系,其中一个对象(主题)的状态变化会通知和更新所有依赖的对象(观察者)。示例代码如下:```javascript
class Subject {
observers = [];
subscribe(observer) {
(observer);
}
notifyObservers() {
(observer => ());
}
}
class Observer {
update() {
('Observer notified');
}
}
```
策略模式
策略模式允许不同的算法或行为在运行时动态地互换。它将算法与使用它的类分离,使其可以独立更改。示例代码如下:```javascript
class Context {
strategy;
constructor(strategy) {
= strategy;
}
executeStrategy() {
();
}
}
class StrategyA {
execute() {
('Strategy A executed');
}
}
class StrategyB {
execute() {
('Strategy B executed');
}
}
```
命令模式
命令模式将动作封装在对象中,允许将动作排队、记录和撤消。它将动作的执行与请求动作的对象分离。示例代码如下:```javascript
class Command {
execute() {
throw new Error('Abstract method');
}
}
class LightOnCommand extends Command {
constructor(light) {
super();
= light;
}
execute() {
();
}
}
class LightOffCommand extends Command {
constructor(light) {
super();
= light;
}
execute() {
();
}
}
class Light {
on() {
('Light is on');
}
off() {
('Light is off');
}
}
```
桥接模式
桥接模式将抽象部分与实现部分分离,使它们可以独立变化。它允许在不修改客户端代码的情况下扩展应用程序。示例代码如下:```javascript
class Abstraction {
implementation;
constructor(implementation) {
= implementation;
}
operation() {
();
}
}
class Implementation {
operation() {
throw new Error('Abstract method');
}
}
class ConcreteImplementationA extends Implementation {
operation() {
('Concrete Implementation A operation');
}
}
class ConcreteImplementationB extends Implementation {
operation() {
('Concrete Implementation B operation');
}
}
```
装饰器模式
装饰器模式允许动态地向对象添加新功能。它提供了比继承更灵活的方式来扩展对象的行为。示例代码如下:```javascript
class Shape {
draw() {
throw new Error('Abstract method');
}
}
class Circle extends Shape {
draw() {
('Drawing a circle');
}
}
class Decorator {
constructor(shape) {
= shape;
}
draw() {
();
}
}
class RedDecorator extends Decorator {
draw() {
();
('Making it red');
}
}
class BlueDecorator extends Decorator {
draw() {
();
('Making it blue');
}
}
```
组合模式
组合模式将对象组织成树形结构,其中每个对象可以是一个叶节点或一个包含其他对象的容器节点。它允许对复杂对象进行递归操作。示例代码如下:```javascript
class Component {
operation() {
throw new Error('Abstract method');
}
}
class Leaf extends Component {
operation() {
('Leaf operation');
}
}
class Composite extends Component {
children = [];
operation() {
(child => ());
}
}
```
中介者模式
中介者模式提供一个集中对象,用于协调对象之间的通信。它将对象与它们的直接通信分离,简化了系统设计。示例代码如下:```javascript
class Mediator {
colleagues = [];
register(colleague) {
(colleague);
}
send(message, colleague) {
(otherColleague => {
if (otherColleague !== colleague) {
(message);
}
});
}
}
class Colleague {
mediator;
constructor(mediator) {
= mediator;
(this);
}
send(message) {
(message, this);
}
receive(message) {
('Colleague received message: ', message);
}
}
```
其他常见模式
除了上述模式外,还有一些其他常见的设计模式用于JavaScript,包括:* 发布-订阅模式
* 命令查询职责分离模式(CQRS)
* 依赖注入模式
* 单元测试模式
* 异步编程模式
2025-02-08
![Windows 游戏脚本语言:深入浅出指南](https://cdn.shapao.cn/images/text.png)
Windows 游戏脚本语言:深入浅出指南
https://jb123.cn/jiaobenyuyan/34834.html
![Python 标准编程指南](https://cdn.shapao.cn/images/text.png)
Python 标准编程指南
https://jb123.cn/python/34833.html
![组态王编程脚本:自动化控制领域的利器](https://cdn.shapao.cn/images/text.png)
组态王编程脚本:自动化控制领域的利器
https://jb123.cn/jiaobenbiancheng/34832.html
![脚本编程判断数字大小](https://cdn.shapao.cn/images/text.png)
脚本编程判断数字大小
https://jb123.cn/jiaobenbiancheng/34831.html
![JavaScript find() 方法](https://cdn.shapao.cn/images/text.png)
JavaScript find() 方法
https://jb123.cn/javascript/34830.html
热门文章
![JavaScript (JS) 中的 JSF (JavaServer Faces)](https://cdn.shapao.cn/images/text.png)
JavaScript (JS) 中的 JSF (JavaServer Faces)
https://jb123.cn/javascript/25790.html
![JavaScript 枚举:全面指南](https://cdn.shapao.cn/images/text.png)
JavaScript 枚举:全面指南
https://jb123.cn/javascript/24141.html
![JavaScript 逻辑与:学习布尔表达式的基础](https://cdn.shapao.cn/images/text.png)
JavaScript 逻辑与:学习布尔表达式的基础
https://jb123.cn/javascript/20993.html
![JavaScript 中保留小数的技巧](https://cdn.shapao.cn/images/text.png)
JavaScript 中保留小数的技巧
https://jb123.cn/javascript/18603.html
![JavaScript 调试神器:步步掌握开发调试技巧](https://cdn.shapao.cn/images/text.png)
JavaScript 调试神器:步步掌握开发调试技巧
https://jb123.cn/javascript/4718.html