-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
81 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Nest 起步 | ||
|
||
## 安装`nest cli` | ||
|
||
```bash | ||
pnpm i -g nest@cli | ||
|
||
nest new project-name | ||
``` | ||
|
||
## 目录结构 | ||
|
||
```text | ||
src | ||
├── app.controller.spec.ts | ||
├── app.controller.ts | ||
├── app.module.ts | ||
├── app.service.ts | ||
└── main.ts | ||
``` | ||
|
||
| 文件 | 描述 | | ||
| :--------------------- | :---------------------------------------------------------- | | ||
| app.controller.ts | 带有单个路由的基本控制器示例 | | ||
| app.controller.spec.ts | 对于基本控制器的单元测试样例 | | ||
| app.module.ts | 应用程序的根模块 | | ||
| app.service.ts | 带有单个方法的基本服务 | | ||
| main.ts | 应用程序入口文件。它使用`NestFactory`用来创建 Nest 应用实例 | |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1 @@ | ||
--- | ||
lang: zh-CN | ||
title: node.js | ||
--- | ||
|
||
# nodejs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# 工厂方法模式 | ||
|
||
工厂方法模式`Factory Method Pattern`又称为工厂模式,也叫虚拟构造器`Virtual Constructor`模式或者多态工厂`Polymorphic Factory`模式,它属于类创建型模式,在工厂方法模式中,工厂父类负责定义创建产品对象的公共接口,而工厂子类则负责生成具体的产品对象,这样做的目的是将产品类的实例化操作延迟到工厂子类中完成,即通过工厂子类来确定究竟应该实例化哪一个具体产品类。 | ||
|
||
## 描述 | ||
|
||
简单工厂模式有一个问题,类的创建依赖工厂类,也就是说如果想要拓展程序,必须对工厂类进行修改,而工厂方法模式就是将具体的创建过程交给专门的工厂子类去完成,即将产品类的实例化操作延迟到工厂子类中完成,由此可以不必再去修改工厂类,而只需要定义实现工厂的子类即可。 | ||
|
||
## 模式分析 | ||
|
||
工厂方法模式是简单工厂模式的进一步抽象和推广。由于使用了面向对象的多态性,工厂方法模式保持了简单工厂模式的优点,而且克服了它的缺点。在工厂方法模式中,核心的工厂类不再负责所有产品的创建,而是将具体创建工作交给子类去做。这个核心类仅仅负责给出具体工厂必须实现的接口,而不负责哪一个产品类被实例化这种细节,这使得工厂方法模式可以允许系统在不修改工厂角色的情况下引进新产品。 | ||
|
||
## 模式结构 | ||
|
||
- `Product`:抽象产品。 | ||
- `ConcreteProduct`:具体产品。 | ||
- `Factory`:抽象工厂。 | ||
- `ConcreteFactory`:具体工厂。 | ||
|
||
## 优点 | ||
|
||
- 在工厂方法模式中,工厂方法用来创建客户所需要的产品,同时还向客户隐藏了哪种具体产品类将被实例化这一细节,用户只需要关心所需产品对应的工厂,无须关心创建细节,甚至无须知道具体产品类的类名。 | ||
- 基于工厂角色和产品角色的多态性设计是工厂方法模式的关键。它能够使工厂可以自主确定创建何种产品对象,而如何创建这个对象的细节则完全封装在具体工厂内部。工厂方法模式之所以又被称为多态工厂模式,是因为所有的具体工厂类都具有同一抽象父类。 | ||
- 使用工厂方法模式的另一个优点是在系统中加入新产品时,无须修改抽象工厂和抽象产品提供的接口,无须修改客户端,也无须修改其他的具体工厂和具体产品,而只要添加一个具体工厂和具体产品就可以了。这样,系统的可扩展性也就变得非常好,完全符合“开闭原则”。 | ||
|
||
## 缺点 | ||
|
||
- 在添加新产品时,需要编写新的具体产品类,而且还要提供与之对应的具体工厂类,系统中类的个数将成对增加,在一定程度上增加了系统的复杂度,有更多的类需要编译和运行,会给系统带来一些额外的开销。 | ||
- 由于考虑到系统的可扩展性,需要引入抽象层,在客户端代码中均使用抽象层进行定义,增加了系统的抽象性和理解难度,且在实现时可能需要用到反射等技术,增加了系统的实现难度。 | ||
|
||
## 实现 | ||
|
||
```js | ||
|
||
``` |
File renamed without changes.