更新 TypeScript 示例,添加抽象工厂模式实现,完善相关文档和配置文件
This commit is contained in:
parent
539182fded
commit
29e554b670
|
|
@ -1,2 +1,4 @@
|
||||||
go/build/*
|
go/build/*
|
||||||
python/.venv
|
python/.venv
|
||||||
|
ts/node_modules
|
||||||
|
.DS_Store
|
||||||
|
|
@ -1,10 +1,14 @@
|
||||||
# how to
|
# how to
|
||||||
|
|
||||||
To build the Go examples in this directory, navigate to the specific example folder and run the following command:
|
To build the Go examples in this directory, run the following command:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
make
|
make
|
||||||
# 或者依次运行所有命令
|
```
|
||||||
|
|
||||||
|
or run each command sequentially:
|
||||||
|
|
||||||
|
```bash
|
||||||
make build ## 构建所有示例
|
make build ## 构建所有示例
|
||||||
make test ## 运行所有示例
|
make test ## 运行所有示例
|
||||||
make clean ## 清理构建产物
|
make clean ## 清理构建产物
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
# how to
|
# how to
|
||||||
|
|
||||||
To run the Python examples in this directory, navigate to the specific example folder and execute the following command:
|
To run the Python examples in this directory, execute the following command:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
make -s test ## 运行所有示例
|
make ## 运行所有示例
|
||||||
```
|
```
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,13 @@
|
||||||
|
DIRS := $(shell ls -d src/*)
|
||||||
|
|
||||||
|
ifndef VERBOSE
|
||||||
|
.SILENT:
|
||||||
|
endif
|
||||||
|
|
||||||
|
all:
|
||||||
|
for dir in $(DIRS); do \
|
||||||
|
echo "\n"; \
|
||||||
|
pnpm tsx $$dir/index.ts; \
|
||||||
|
echo "\n"; \
|
||||||
|
done
|
||||||
|
echo "All Typescripts examples executed.\n"
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
# how to
|
||||||
|
|
||||||
|
To run the typescript examples in this directory, execute the following command:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
make
|
||||||
|
```
|
||||||
|
|
@ -0,0 +1,16 @@
|
||||||
|
{
|
||||||
|
"name": "ts",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "",
|
||||||
|
"main": "index.js",
|
||||||
|
"scripts": {
|
||||||
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
|
},
|
||||||
|
"keywords": [],
|
||||||
|
"author": "",
|
||||||
|
"license": "ISC",
|
||||||
|
"packageManager": "pnpm@10.22.0",
|
||||||
|
"dependencies": {
|
||||||
|
"tsx": "^4.21.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,317 @@
|
||||||
|
lockfileVersion: '9.0'
|
||||||
|
|
||||||
|
settings:
|
||||||
|
autoInstallPeers: true
|
||||||
|
excludeLinksFromLockfile: false
|
||||||
|
|
||||||
|
importers:
|
||||||
|
|
||||||
|
.:
|
||||||
|
dependencies:
|
||||||
|
tsx:
|
||||||
|
specifier: ^4.21.0
|
||||||
|
version: 4.21.0
|
||||||
|
|
||||||
|
packages:
|
||||||
|
|
||||||
|
'@esbuild/aix-ppc64@0.27.1':
|
||||||
|
resolution: {integrity: sha512-HHB50pdsBX6k47S4u5g/CaLjqS3qwaOVE5ILsq64jyzgMhLuCuZ8rGzM9yhsAjfjkbgUPMzZEPa7DAp7yz6vuA==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
cpu: [ppc64]
|
||||||
|
os: [aix]
|
||||||
|
|
||||||
|
'@esbuild/android-arm64@0.27.1':
|
||||||
|
resolution: {integrity: sha512-45fuKmAJpxnQWixOGCrS+ro4Uvb4Re9+UTieUY2f8AEc+t7d4AaZ6eUJ3Hva7dtrxAAWHtlEFsXFMAgNnGU9uQ==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
cpu: [arm64]
|
||||||
|
os: [android]
|
||||||
|
|
||||||
|
'@esbuild/android-arm@0.27.1':
|
||||||
|
resolution: {integrity: sha512-kFqa6/UcaTbGm/NncN9kzVOODjhZW8e+FRdSeypWe6j33gzclHtwlANs26JrupOntlcWmB0u8+8HZo8s7thHvg==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
cpu: [arm]
|
||||||
|
os: [android]
|
||||||
|
|
||||||
|
'@esbuild/android-x64@0.27.1':
|
||||||
|
resolution: {integrity: sha512-LBEpOz0BsgMEeHgenf5aqmn/lLNTFXVfoWMUox8CtWWYK9X4jmQzWjoGoNb8lmAYml/tQ/Ysvm8q7szu7BoxRQ==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
cpu: [x64]
|
||||||
|
os: [android]
|
||||||
|
|
||||||
|
'@esbuild/darwin-arm64@0.27.1':
|
||||||
|
resolution: {integrity: sha512-veg7fL8eMSCVKL7IW4pxb54QERtedFDfY/ASrumK/SbFsXnRazxY4YykN/THYqFnFwJ0aVjiUrVG2PwcdAEqQQ==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
cpu: [arm64]
|
||||||
|
os: [darwin]
|
||||||
|
|
||||||
|
'@esbuild/darwin-x64@0.27.1':
|
||||||
|
resolution: {integrity: sha512-+3ELd+nTzhfWb07Vol7EZ+5PTbJ/u74nC6iv4/lwIU99Ip5uuY6QoIf0Hn4m2HoV0qcnRivN3KSqc+FyCHjoVQ==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
cpu: [x64]
|
||||||
|
os: [darwin]
|
||||||
|
|
||||||
|
'@esbuild/freebsd-arm64@0.27.1':
|
||||||
|
resolution: {integrity: sha512-/8Rfgns4XD9XOSXlzUDepG8PX+AVWHliYlUkFI3K3GB6tqbdjYqdhcb4BKRd7C0BhZSoaCxhv8kTcBrcZWP+xg==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
cpu: [arm64]
|
||||||
|
os: [freebsd]
|
||||||
|
|
||||||
|
'@esbuild/freebsd-x64@0.27.1':
|
||||||
|
resolution: {integrity: sha512-GITpD8dK9C+r+5yRT/UKVT36h/DQLOHdwGVwwoHidlnA168oD3uxA878XloXebK4Ul3gDBBIvEdL7go9gCUFzQ==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
cpu: [x64]
|
||||||
|
os: [freebsd]
|
||||||
|
|
||||||
|
'@esbuild/linux-arm64@0.27.1':
|
||||||
|
resolution: {integrity: sha512-W9//kCrh/6in9rWIBdKaMtuTTzNj6jSeG/haWBADqLLa9P8O5YSRDzgD5y9QBok4AYlzS6ARHifAb75V6G670Q==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
cpu: [arm64]
|
||||||
|
os: [linux]
|
||||||
|
|
||||||
|
'@esbuild/linux-arm@0.27.1':
|
||||||
|
resolution: {integrity: sha512-ieMID0JRZY/ZeCrsFQ3Y3NlHNCqIhTprJfDgSB3/lv5jJZ8FX3hqPyXWhe+gvS5ARMBJ242PM+VNz/ctNj//eA==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
cpu: [arm]
|
||||||
|
os: [linux]
|
||||||
|
|
||||||
|
'@esbuild/linux-ia32@0.27.1':
|
||||||
|
resolution: {integrity: sha512-VIUV4z8GD8rtSVMfAj1aXFahsi/+tcoXXNYmXgzISL+KB381vbSTNdeZHHHIYqFyXcoEhu9n5cT+05tRv13rlw==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
cpu: [ia32]
|
||||||
|
os: [linux]
|
||||||
|
|
||||||
|
'@esbuild/linux-loong64@0.27.1':
|
||||||
|
resolution: {integrity: sha512-l4rfiiJRN7sTNI//ff65zJ9z8U+k6zcCg0LALU5iEWzY+a1mVZ8iWC1k5EsNKThZ7XCQ6YWtsZ8EWYm7r1UEsg==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
cpu: [loong64]
|
||||||
|
os: [linux]
|
||||||
|
|
||||||
|
'@esbuild/linux-mips64el@0.27.1':
|
||||||
|
resolution: {integrity: sha512-U0bEuAOLvO/DWFdygTHWY8C067FXz+UbzKgxYhXC0fDieFa0kDIra1FAhsAARRJbvEyso8aAqvPdNxzWuStBnA==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
cpu: [mips64el]
|
||||||
|
os: [linux]
|
||||||
|
|
||||||
|
'@esbuild/linux-ppc64@0.27.1':
|
||||||
|
resolution: {integrity: sha512-NzdQ/Xwu6vPSf/GkdmRNsOfIeSGnh7muundsWItmBsVpMoNPVpM61qNzAVY3pZ1glzzAxLR40UyYM23eaDDbYQ==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
cpu: [ppc64]
|
||||||
|
os: [linux]
|
||||||
|
|
||||||
|
'@esbuild/linux-riscv64@0.27.1':
|
||||||
|
resolution: {integrity: sha512-7zlw8p3IApcsN7mFw0O1Z1PyEk6PlKMu18roImfl3iQHTnr/yAfYv6s4hXPidbDoI2Q0pW+5xeoM4eTCC0UdrQ==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
cpu: [riscv64]
|
||||||
|
os: [linux]
|
||||||
|
|
||||||
|
'@esbuild/linux-s390x@0.27.1':
|
||||||
|
resolution: {integrity: sha512-cGj5wli+G+nkVQdZo3+7FDKC25Uh4ZVwOAK6A06Hsvgr8WqBBuOy/1s+PUEd/6Je+vjfm6stX0kmib5b/O2Ykw==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
cpu: [s390x]
|
||||||
|
os: [linux]
|
||||||
|
|
||||||
|
'@esbuild/linux-x64@0.27.1':
|
||||||
|
resolution: {integrity: sha512-z3H/HYI9MM0HTv3hQZ81f+AKb+yEoCRlUby1F80vbQ5XdzEMyY/9iNlAmhqiBKw4MJXwfgsh7ERGEOhrM1niMA==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
cpu: [x64]
|
||||||
|
os: [linux]
|
||||||
|
|
||||||
|
'@esbuild/netbsd-arm64@0.27.1':
|
||||||
|
resolution: {integrity: sha512-wzC24DxAvk8Em01YmVXyjl96Mr+ecTPyOuADAvjGg+fyBpGmxmcr2E5ttf7Im8D0sXZihpxzO1isus8MdjMCXQ==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
cpu: [arm64]
|
||||||
|
os: [netbsd]
|
||||||
|
|
||||||
|
'@esbuild/netbsd-x64@0.27.1':
|
||||||
|
resolution: {integrity: sha512-1YQ8ybGi2yIXswu6eNzJsrYIGFpnlzEWRl6iR5gMgmsrR0FcNoV1m9k9sc3PuP5rUBLshOZylc9nqSgymI+TYg==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
cpu: [x64]
|
||||||
|
os: [netbsd]
|
||||||
|
|
||||||
|
'@esbuild/openbsd-arm64@0.27.1':
|
||||||
|
resolution: {integrity: sha512-5Z+DzLCrq5wmU7RDaMDe2DVXMRm2tTDvX2KU14JJVBN2CT/qov7XVix85QoJqHltpvAOZUAc3ndU56HSMWrv8g==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
cpu: [arm64]
|
||||||
|
os: [openbsd]
|
||||||
|
|
||||||
|
'@esbuild/openbsd-x64@0.27.1':
|
||||||
|
resolution: {integrity: sha512-Q73ENzIdPF5jap4wqLtsfh8YbYSZ8Q0wnxplOlZUOyZy7B4ZKW8DXGWgTCZmF8VWD7Tciwv5F4NsRf6vYlZtqg==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
cpu: [x64]
|
||||||
|
os: [openbsd]
|
||||||
|
|
||||||
|
'@esbuild/openharmony-arm64@0.27.1':
|
||||||
|
resolution: {integrity: sha512-ajbHrGM/XiK+sXM0JzEbJAen+0E+JMQZ2l4RR4VFwvV9JEERx+oxtgkpoKv1SevhjavK2z2ReHk32pjzktWbGg==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
cpu: [arm64]
|
||||||
|
os: [openharmony]
|
||||||
|
|
||||||
|
'@esbuild/sunos-x64@0.27.1':
|
||||||
|
resolution: {integrity: sha512-IPUW+y4VIjuDVn+OMzHc5FV4GubIwPnsz6ubkvN8cuhEqH81NovB53IUlrlBkPMEPxvNnf79MGBoz8rZ2iW8HA==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
cpu: [x64]
|
||||||
|
os: [sunos]
|
||||||
|
|
||||||
|
'@esbuild/win32-arm64@0.27.1':
|
||||||
|
resolution: {integrity: sha512-RIVRWiljWA6CdVu8zkWcRmGP7iRRIIwvhDKem8UMBjPql2TXM5PkDVvvrzMtj1V+WFPB4K7zkIGM7VzRtFkjdg==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
cpu: [arm64]
|
||||||
|
os: [win32]
|
||||||
|
|
||||||
|
'@esbuild/win32-ia32@0.27.1':
|
||||||
|
resolution: {integrity: sha512-2BR5M8CPbptC1AK5JbJT1fWrHLvejwZidKx3UMSF0ecHMa+smhi16drIrCEggkgviBwLYd5nwrFLSl5Kho96RQ==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
cpu: [ia32]
|
||||||
|
os: [win32]
|
||||||
|
|
||||||
|
'@esbuild/win32-x64@0.27.1':
|
||||||
|
resolution: {integrity: sha512-d5X6RMYv6taIymSk8JBP+nxv8DQAMY6A51GPgusqLdK9wBz5wWIXy1KjTck6HnjE9hqJzJRdk+1p/t5soSbCtw==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
cpu: [x64]
|
||||||
|
os: [win32]
|
||||||
|
|
||||||
|
esbuild@0.27.1:
|
||||||
|
resolution: {integrity: sha512-yY35KZckJJuVVPXpvjgxiCuVEJT67F6zDeVTv4rizyPrfGBUpZQsvmxnN+C371c2esD/hNMjj4tpBhuueLN7aA==}
|
||||||
|
engines: {node: '>=18'}
|
||||||
|
hasBin: true
|
||||||
|
|
||||||
|
fsevents@2.3.3:
|
||||||
|
resolution: {integrity: sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==}
|
||||||
|
engines: {node: ^8.16.0 || ^10.6.0 || >=11.0.0}
|
||||||
|
os: [darwin]
|
||||||
|
|
||||||
|
get-tsconfig@4.13.0:
|
||||||
|
resolution: {integrity: sha512-1VKTZJCwBrvbd+Wn3AOgQP/2Av+TfTCOlE4AcRJE72W1ksZXbAx8PPBR9RzgTeSPzlPMHrbANMH3LbltH73wxQ==}
|
||||||
|
|
||||||
|
resolve-pkg-maps@1.0.0:
|
||||||
|
resolution: {integrity: sha512-seS2Tj26TBVOC2NIc2rOe2y2ZO7efxITtLZcGSOnHHNOQ7CkiUBfw0Iw2ck6xkIhPwLhKNLS8BO+hEpngQlqzw==}
|
||||||
|
|
||||||
|
tsx@4.21.0:
|
||||||
|
resolution: {integrity: sha512-5C1sg4USs1lfG0GFb2RLXsdpXqBSEhAaA/0kPL01wxzpMqLILNxIxIOKiILz+cdg/pLnOUxFYOR5yhHU666wbw==}
|
||||||
|
engines: {node: '>=18.0.0'}
|
||||||
|
hasBin: true
|
||||||
|
|
||||||
|
snapshots:
|
||||||
|
|
||||||
|
'@esbuild/aix-ppc64@0.27.1':
|
||||||
|
optional: true
|
||||||
|
|
||||||
|
'@esbuild/android-arm64@0.27.1':
|
||||||
|
optional: true
|
||||||
|
|
||||||
|
'@esbuild/android-arm@0.27.1':
|
||||||
|
optional: true
|
||||||
|
|
||||||
|
'@esbuild/android-x64@0.27.1':
|
||||||
|
optional: true
|
||||||
|
|
||||||
|
'@esbuild/darwin-arm64@0.27.1':
|
||||||
|
optional: true
|
||||||
|
|
||||||
|
'@esbuild/darwin-x64@0.27.1':
|
||||||
|
optional: true
|
||||||
|
|
||||||
|
'@esbuild/freebsd-arm64@0.27.1':
|
||||||
|
optional: true
|
||||||
|
|
||||||
|
'@esbuild/freebsd-x64@0.27.1':
|
||||||
|
optional: true
|
||||||
|
|
||||||
|
'@esbuild/linux-arm64@0.27.1':
|
||||||
|
optional: true
|
||||||
|
|
||||||
|
'@esbuild/linux-arm@0.27.1':
|
||||||
|
optional: true
|
||||||
|
|
||||||
|
'@esbuild/linux-ia32@0.27.1':
|
||||||
|
optional: true
|
||||||
|
|
||||||
|
'@esbuild/linux-loong64@0.27.1':
|
||||||
|
optional: true
|
||||||
|
|
||||||
|
'@esbuild/linux-mips64el@0.27.1':
|
||||||
|
optional: true
|
||||||
|
|
||||||
|
'@esbuild/linux-ppc64@0.27.1':
|
||||||
|
optional: true
|
||||||
|
|
||||||
|
'@esbuild/linux-riscv64@0.27.1':
|
||||||
|
optional: true
|
||||||
|
|
||||||
|
'@esbuild/linux-s390x@0.27.1':
|
||||||
|
optional: true
|
||||||
|
|
||||||
|
'@esbuild/linux-x64@0.27.1':
|
||||||
|
optional: true
|
||||||
|
|
||||||
|
'@esbuild/netbsd-arm64@0.27.1':
|
||||||
|
optional: true
|
||||||
|
|
||||||
|
'@esbuild/netbsd-x64@0.27.1':
|
||||||
|
optional: true
|
||||||
|
|
||||||
|
'@esbuild/openbsd-arm64@0.27.1':
|
||||||
|
optional: true
|
||||||
|
|
||||||
|
'@esbuild/openbsd-x64@0.27.1':
|
||||||
|
optional: true
|
||||||
|
|
||||||
|
'@esbuild/openharmony-arm64@0.27.1':
|
||||||
|
optional: true
|
||||||
|
|
||||||
|
'@esbuild/sunos-x64@0.27.1':
|
||||||
|
optional: true
|
||||||
|
|
||||||
|
'@esbuild/win32-arm64@0.27.1':
|
||||||
|
optional: true
|
||||||
|
|
||||||
|
'@esbuild/win32-ia32@0.27.1':
|
||||||
|
optional: true
|
||||||
|
|
||||||
|
'@esbuild/win32-x64@0.27.1':
|
||||||
|
optional: true
|
||||||
|
|
||||||
|
esbuild@0.27.1:
|
||||||
|
optionalDependencies:
|
||||||
|
'@esbuild/aix-ppc64': 0.27.1
|
||||||
|
'@esbuild/android-arm': 0.27.1
|
||||||
|
'@esbuild/android-arm64': 0.27.1
|
||||||
|
'@esbuild/android-x64': 0.27.1
|
||||||
|
'@esbuild/darwin-arm64': 0.27.1
|
||||||
|
'@esbuild/darwin-x64': 0.27.1
|
||||||
|
'@esbuild/freebsd-arm64': 0.27.1
|
||||||
|
'@esbuild/freebsd-x64': 0.27.1
|
||||||
|
'@esbuild/linux-arm': 0.27.1
|
||||||
|
'@esbuild/linux-arm64': 0.27.1
|
||||||
|
'@esbuild/linux-ia32': 0.27.1
|
||||||
|
'@esbuild/linux-loong64': 0.27.1
|
||||||
|
'@esbuild/linux-mips64el': 0.27.1
|
||||||
|
'@esbuild/linux-ppc64': 0.27.1
|
||||||
|
'@esbuild/linux-riscv64': 0.27.1
|
||||||
|
'@esbuild/linux-s390x': 0.27.1
|
||||||
|
'@esbuild/linux-x64': 0.27.1
|
||||||
|
'@esbuild/netbsd-arm64': 0.27.1
|
||||||
|
'@esbuild/netbsd-x64': 0.27.1
|
||||||
|
'@esbuild/openbsd-arm64': 0.27.1
|
||||||
|
'@esbuild/openbsd-x64': 0.27.1
|
||||||
|
'@esbuild/openharmony-arm64': 0.27.1
|
||||||
|
'@esbuild/sunos-x64': 0.27.1
|
||||||
|
'@esbuild/win32-arm64': 0.27.1
|
||||||
|
'@esbuild/win32-ia32': 0.27.1
|
||||||
|
'@esbuild/win32-x64': 0.27.1
|
||||||
|
|
||||||
|
fsevents@2.3.3:
|
||||||
|
optional: true
|
||||||
|
|
||||||
|
get-tsconfig@4.13.0:
|
||||||
|
dependencies:
|
||||||
|
resolve-pkg-maps: 1.0.0
|
||||||
|
|
||||||
|
resolve-pkg-maps@1.0.0: {}
|
||||||
|
|
||||||
|
tsx@4.21.0:
|
||||||
|
dependencies:
|
||||||
|
esbuild: 0.27.1
|
||||||
|
get-tsconfig: 4.13.0
|
||||||
|
optionalDependencies:
|
||||||
|
fsevents: 2.3.3
|
||||||
|
|
@ -0,0 +1,106 @@
|
||||||
|
/*
|
||||||
|
实例
|
||||||
|
今天伟哥约自己在富士康工作的老乡出来喝酒,没想到老乡最近一直在加班。听说接了个大活,要同时为小米公司和苹果公司代工他们的笔记本电脑和手机生产业务。
|
||||||
|
*/
|
||||||
|
|
||||||
|
// 抽象产品 - 手机
|
||||||
|
abstract class Phone {
|
||||||
|
abstract brand: string;
|
||||||
|
abstract setOS(): void;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 具体产品 - 苹果手机
|
||||||
|
class Iphone extends Phone {
|
||||||
|
readonly brand = "Apple";
|
||||||
|
public setOS(): void {
|
||||||
|
console.log(" 为该手机安装IOS系统");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 具体产品 - 小米手机
|
||||||
|
class MiPhone extends Phone {
|
||||||
|
readonly brand = "Xiaomi";
|
||||||
|
public setOS(): void {
|
||||||
|
console.log(" 为该手机安装安卓系统");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 抽象产品 - 笔记本电脑
|
||||||
|
abstract class Laptop {
|
||||||
|
abstract brand: string;
|
||||||
|
abstract setOS(): void;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 具体产品 - 苹果笔记本电脑
|
||||||
|
class MacBook extends Laptop {
|
||||||
|
readonly brand = "Apple";
|
||||||
|
public setOS(): void {
|
||||||
|
console.log(" 为该笔记本电脑安装MacOS系统");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 具体产品 - 小米笔记本电脑
|
||||||
|
class MiLaptop extends Laptop {
|
||||||
|
readonly brand = "Xiaomi";
|
||||||
|
public setOS(): void {
|
||||||
|
console.log(" 为该笔记本电脑安装Windows系统");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 抽象工厂
|
||||||
|
abstract class AbstractFactory {
|
||||||
|
abstract name: string;
|
||||||
|
abstract createPhone(): Phone;
|
||||||
|
abstract createLaptop(): Laptop;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 具体工厂 - 苹果工厂
|
||||||
|
class AppleFactory extends AbstractFactory {
|
||||||
|
name = "苹果工厂";
|
||||||
|
public createPhone(): Phone {
|
||||||
|
return new Iphone();
|
||||||
|
}
|
||||||
|
public createLaptop(): Laptop {
|
||||||
|
return new MacBook();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 具体工厂 - 小米工厂
|
||||||
|
class XiaomiFactory extends AbstractFactory {
|
||||||
|
name = "小米工厂";
|
||||||
|
public createPhone(): Phone {
|
||||||
|
return new MiPhone();
|
||||||
|
}
|
||||||
|
public createLaptop(): Laptop {
|
||||||
|
return new MiLaptop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const client = (factory: AbstractFactory) => {
|
||||||
|
const phone = factory.createPhone();
|
||||||
|
const laptop = factory.createLaptop();
|
||||||
|
console.log(` 生产了一台${phone.brand}手机`);
|
||||||
|
phone.setOS();
|
||||||
|
console.log(` 生产了一台${laptop.brand}笔记本电脑`);
|
||||||
|
laptop.setOS();
|
||||||
|
};
|
||||||
|
|
||||||
|
function main() {
|
||||||
|
console.log("======== 抽象工厂模式 ========");
|
||||||
|
|
||||||
|
// 伟哥先去苹果工厂生产产品
|
||||||
|
let factory: AbstractFactory = new AppleFactory();
|
||||||
|
console.log(`伟哥选择了${factory.name}生产产品:`);
|
||||||
|
client(factory);
|
||||||
|
|
||||||
|
console.log("\n切换工厂生产其他品牌的产品...\n");
|
||||||
|
|
||||||
|
// 伟哥又去小米工厂生产产品
|
||||||
|
factory = new XiaomiFactory();
|
||||||
|
console.log(`伟哥选择了${factory.name}生产产品:`);
|
||||||
|
client(factory);
|
||||||
|
|
||||||
|
console.log("====== 抽象工厂模式结束 ======");
|
||||||
|
}
|
||||||
|
|
||||||
|
main();
|
||||||
|
|
@ -0,0 +1,13 @@
|
||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"target": "esnext",
|
||||||
|
"module": "esnext",
|
||||||
|
"lib": ["ESNext", "DOM"],
|
||||||
|
"moduleResolution": "node",
|
||||||
|
"esModuleInterop": true,
|
||||||
|
"rootDir": "./src",
|
||||||
|
"outDir": "./dist",
|
||||||
|
"forceConsistentCasingInFileNames": true,
|
||||||
|
"strict": true
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue