Skip to content

Commit 33b15cd

Browse files
committed
ts
1 parent a1f2776 commit 33b15cd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+1174
-37
lines changed

decorator-pattern/ts/test/test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export function test() {
3434
})()
3535
/**
3636
jarry@jarrys-MacBook-Pro ts % sudo npm install -g ts-node
37-
// direct use ts-node-esm or use tsc before
37+
// direct use `ts-node-esm` or use `tsc` and `node`
3838
jarry@jarrys-MacBook-Pro ts % ts-node-esm test/test.ts
3939
test start:
4040
Circle::draw()

facade-pattern/java/test/Test.java

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
public class Test {
66

77
public static void start() {
8+
/**
9+
* 外观模式就是在外层添加一个访问接口类,客户通过这统一访问接口来访问这些复杂子类。
10+
* 外观模式与适配器模式比较像,都是提供一个高层访问接口,隔绝外部客户与内部子类。
11+
* 所不同是外观模式后面的类无需实现相同接口,只是把各种调用整合简化,而适配器需要是同一系列类,为的是解决接口不兼容。
12+
* 这里声明外观类,外观类的方法里影藏了很多子类的调用。
13+
*/
814
AbstractFacade facade = new Facade();
915
facade.encoding(123456);
1016
facade.encrypt(999999);

facade-pattern/js/test/test.js

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { Facade } from '../src/Facade.js'
22

33
export function test() {
4+
/**
5+
* 外观模式就是在外层添加一个访问接口类,客户通过这统一访问接口来访问这些复杂子类。
6+
* 外观模式与适配器模式比较像,都是提供一个高层访问接口,隔绝外部客户与内部子类。
7+
* 所不同是外观模式后面的类无需实现相同接口,只是把各种调用整合简化,而适配器需要是同一系列类,为的是解决接口不兼容。
8+
* 这里声明外观类,外观类的方法里影藏了很多子类的调用。
9+
*/
410
const facade = new Facade()
511
facade.encoding(123456)
612
facade.encrypt(999999)

facade-pattern/python/test/test.py

+7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212
from src.Facade import Facade
1313

1414
def test():
15+
'''
16+
* 外观模式就是在外层添加一个访问接口类,客户通过这统一访问接口来访问这些复杂子类。
17+
* 外观模式与适配器模式比较像,都是提供一个高层访问接口,隔绝外部客户与内部子类。
18+
* 所不同是外观模式后面的类无需实现相同接口,只是把各种调用整合简化,而适配器需要是同一系列类,为的是解决接口不兼容。
19+
* 这里声明外观类,外观类的方法里影藏了很多子类的调用。
20+
'''
21+
1522
facade = Facade()
1623
facade.encoding(123456)
1724
facade.encrypt(999999)

facade-pattern/ts/package.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "esm-project",
3+
"version": "1.0.0",
4+
"main": "test/test.js",
5+
"scripts": {
6+
"test": "node test/test.js"
7+
},
8+
"type": "module"
9+
}
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// 外观模式的接口或抽象类,可选
2+
export interface AbstractFacade {
3+
encoding(id: number): void
4+
5+
encrypt(id: number): void
6+
}

facade-pattern/ts/src/EncodeModule.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// 编码模块
2+
export class EncodeModule {
3+
encoding() {
4+
console.log(this.constructor.name + '::encoding() 进行编码处理。')
5+
}
6+
}

facade-pattern/ts/src/Facade.ts

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { AbstractFacade } from './AbstractFacade.js'
2+
import { FileModule } from './FileModule.js'
3+
import { NetworkModule } from './NetworkModule.js'
4+
import { EncodeModule } from './EncodeModule.js'
5+
6+
// 外观模式实现类,是外部调用与内部子系统的衔接层
7+
export class Facade implements AbstractFacade {
8+
fileModule: FileModule
9+
networkModule: NetworkModule
10+
encodeModule: EncodeModule
11+
constructor() {
12+
this.fileModule = new FileModule()
13+
this.networkModule = new NetworkModule()
14+
this.encodeModule = new EncodeModule()
15+
}
16+
17+
encoding(id: number) {
18+
console.log(this.constructor.name + '::encoding() [id=' + id + ']')
19+
this.networkModule.request()
20+
this.fileModule.readFile()
21+
this.encodeModule.encoding()
22+
this.fileModule.saveFile()
23+
}
24+
25+
encrypt(id: number) {
26+
console.log(this.constructor.name + '::encrypt() [id=' + id + ']')
27+
this.fileModule.readFile()
28+
this.encodeModule.encoding()
29+
}
30+
31+
getFileModule() {
32+
return this.fileModule
33+
}
34+
35+
getNetworkModule() {
36+
return this.networkModule
37+
}
38+
39+
getEncodeModule() {
40+
return this.encodeModule
41+
}
42+
}

facade-pattern/ts/src/FileModule.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// 文件模块
2+
export class FileModule {
3+
readFile() {
4+
console.log(this.constructor.name + '::readFile() 读取文件。')
5+
}
6+
7+
saveFile() {
8+
console.log(this.constructor.name + '::saveFile() 保存文件。')
9+
}
10+
}
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// 网络模块
2+
export class NetworkModule {
3+
request() {
4+
console.log(this.constructor.name + '::request() 远程读取。')
5+
}
6+
}

facade-pattern/ts/test/test.html

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script type="module">
2+
// module need run in http server
3+
import { test } from './test.js'
4+
alert('pls look console info.' + '\n\r' + test)
5+
</script>

facade-pattern/ts/test/test.ts

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { Facade } from '../src/Facade.js'
2+
3+
export function test() {
4+
/**
5+
* 外观模式就是在外层添加一个访问接口类,客户通过这统一访问接口来访问这些复杂子类。
6+
* 外观模式与适配器模式比较像,都是提供一个高层访问接口,隔绝外部客户与内部子类。
7+
* 所不同是外观模式后面的类无需实现相同接口,只是把各种调用整合简化,而适配器需要是同一系列类,为的是解决接口不兼容。
8+
* 这里声明外观类,外观类的方法里影藏了很多子类的调用。
9+
*/
10+
const facade = new Facade()
11+
facade.encoding(123456)
12+
facade.encrypt(999999)
13+
}
14+
15+
// 执行测试
16+
;(function () {
17+
console.log('test start:')
18+
test()
19+
})()
20+
21+
/**
22+
jarry@jarrys-MacBook-Pro ts % sudo npm install -g ts-node
23+
// direct use `ts-node-esm` or use `tsc` and `node`
24+
jarry@jarrys-MacBook-Pro ts % ts-node-esm test/test.ts
25+
test start:
26+
Facade::encoding() [id=123456]
27+
NetworkModule::request() 远程读取。
28+
FileModule::readFile() 读取文件。
29+
EncodeModule::encoding() 进行编码处理。
30+
FileModule::saveFile() 保存文件。
31+
Facade::encrypt() [id=999999]
32+
FileModule::readFile() 读取文件。
33+
EncodeModule::encoding() 进行编码处理。
34+
*/

facade-pattern/ts/tsconfig.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"compilerOptions": {
3+
"target": "esnext",
4+
"lib": ["esnext", "dom"],
5+
"module": "esnext",
6+
"strict": true,
7+
"removeComments": true,
8+
"forceConsistentCasingInFileNames": true,
9+
"strictPropertyInitialization": false,
10+
"esModuleInterop": true,
11+
"skipLibCheck": true,
12+
},
13+
"include": [
14+
"src/**/*.ts",
15+
"test/*.ts",
16+
]
17+
}

factory-pattern/java/test/Test.java

+19-14
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
public class Test {
66

77
public static void start() {
8+
/**
9+
* 工厂模式由工厂类提供统一的创建对象的方法,省去直接new,而是通过统一方法来创建。
10+
* 这样的好处就是让对象创建和调用解耦,便于在创建对象时统一干预。
11+
*/
812
VehicleFactory vehicleFactory = new VehicleFactory();
913

1014
// 获取Bus对象,并调用它的 run 方法
@@ -14,19 +18,20 @@ public static void start() {
1418
// 获取Car对象,并调用它的 run 方法
1519
Vehicle car = vehicleFactory.getVehicle(VehicleType.CAR);
1620
// 类型转为Car
17-
Car car1 = (Car)car;
21+
Car car1 = (Car) car;
1822
car1.run();
1923

2024
// 获取Motorcycle对象,并调用它的 run 方法
2125
// 类型直接转为Motorcycle
22-
Motorcycle motorcycle = (Motorcycle)vehicleFactory.getVehicle(VehicleType.MOTORCYCLE);
26+
Motorcycle motorcycle = (Motorcycle) vehicleFactory.getVehicle(VehicleType.MOTORCYCLE);
2327
motorcycle.run();
2428

2529
// 获取Motorcycle对象,并调用它的 run 方法
2630
Vehicle van = vehicleFactory.getVehicle(VehicleType.VAN);
2731
van.run();
2832

2933
}
34+
3035
public static void main(String[] args) {
3136
System.out.println("test start:");
3237
start();
@@ -36,16 +41,16 @@ public static void main(String[] args) {
3641

3742
/**
3843
* 测试
39-
jarry@jarrys-MacBook-Pro java % java --version
40-
java 14.0.1 2020-04-14
41-
Java(TM) SE Runtime Environment (build 14.0.1+7)
42-
Java HotSpot(TM) 64-Bit Server VM (build 14.0.1+7, mixed mode, sharing)
43-
44-
jarry@jarrys-MacBook-Pro java % javac test/Test.java
45-
jarry@jarrys-MacBook-Pro java % java test/Test
46-
test start:
47-
Bus::run().
48-
Car::run().
49-
Motorcycle::run().
50-
Van::run().
44+
* jarry@jarrys-MacBook-Pro java % java --version
45+
* java 14.0.1 2020-04-14
46+
* Java(TM) SE Runtime Environment (build 14.0.1+7)
47+
* Java HotSpot(TM) 64-Bit Server VM (build 14.0.1+7, mixed mode, sharing)
48+
*
49+
* jarry@jarrys-MacBook-Pro java % javac test/Test.java
50+
* jarry@jarrys-MacBook-Pro java % java test/Test
51+
* test start:
52+
* Bus::run().
53+
* Car::run().
54+
* Motorcycle::run().
55+
* Van::run().
5156
*/

factory-pattern/js/test/test.js

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { VehicleFactory, VehicleType } from '../src/VehicleFactory.js'
22

33
export function test() {
4+
/**
5+
* 工厂模式由工厂类提供统一的创建对象的方法,省去直接new,而是通过统一方法来创建。
6+
* 这样的好处就是让对象创建和调用解耦,便于在创建对象时统一干预。
7+
*/
48
const vehicleFactory = new VehicleFactory()
59

610
// 获取Bus对象,并调用它的 run 方法

factory-pattern/python/test/test.py

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
from src.VehicleFactory import VehicleType
1212

1313
def test():
14+
'''
15+
* 工厂模式由工厂类提供统一的创建对象的方法,省去直接new,而是通过统一方法来创建。
16+
* 这样的好处就是让对象创建和调用解耦,便于在创建对象时统一干预。
17+
'''
1418
vehicle_factory = VehicleFactory()
1519

1620
# 获取Bus对象,并调用它的 run 方法

factory-pattern/ts/package.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "esm-project",
3+
"version": "1.0.0",
4+
"main": "test/test.js",
5+
"scripts": {
6+
"test": "node test/test.js"
7+
},
8+
"type": "module"
9+
}

factory-pattern/ts/src/Bus.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Vehicle } from './Vehicle.js'
2+
export class Bus implements Vehicle {
3+
constructor(options: object) {}
4+
run() {
5+
console.log('Bus::run().')
6+
}
7+
}

factory-pattern/ts/src/Car.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Vehicle } from './Vehicle.js'
2+
export class Car implements Vehicle {
3+
constructor(options: object) {}
4+
run() {
5+
console.log('Car::run().')
6+
}
7+
}

factory-pattern/ts/src/Motorcycle.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Vehicle } from './Vehicle.js'
2+
export class Motorcycle implements Vehicle {
3+
constructor(options: object) {}
4+
run() {
5+
console.log('Motorcycle::run().')
6+
}
7+
}

factory-pattern/ts/src/Van.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Vehicle } from './Vehicle.js'
2+
export class Van implements Vehicle {
3+
constructor(options: object) {}
4+
run() {
5+
console.log('Van::run().')
6+
}
7+
}

factory-pattern/ts/src/Vehicle.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export interface Vehicle {
2+
run(): void
3+
}
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { Bus } from './Bus.js'
2+
import { Car } from './Car.js'
3+
import { Motorcycle } from './Motorcycle.js'
4+
import { Van } from './Van.js'
5+
import { Vehicle } from './Vehicle.js'
6+
export const VehicleType = {
7+
BUS: 'bus',
8+
CAR: 'car',
9+
MOTORCYCLE: 'motorcycle',
10+
VAN: 'van'
11+
}
12+
export class VehicleFactory {
13+
getVehicle(type: string, options: object = {}): Vehicle {
14+
switch (type) {
15+
case 'bus':
16+
return new Bus(options)
17+
case 'car':
18+
return new Car(options)
19+
case 'motorcycle':
20+
return new Motorcycle(options)
21+
case 'van':
22+
return new Van(options)
23+
default:
24+
return new Bus(options)
25+
}
26+
}
27+
}

factory-pattern/ts/test/test.html

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script type="module">
2+
// module need run in http server
3+
import { test } from './test.js'
4+
alert('pls look console info.' + '\n\r' + test)
5+
</script>

0 commit comments

Comments
 (0)