Skip to content

Commit f99e03b

Browse files
committed
add ts
1 parent 76eeb2b commit f99e03b

Some content is hidden

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

55 files changed

+779
-15
lines changed

abstract-factory/js/src/AbstractFactory.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export class AbstractFactory {
1010
return null
1111
}
1212

13-
getShop(name) {
13+
getShop(type) {
1414
return null
1515
}
1616
}

abstract-factory/js/src/FactoryCreator.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import { VehicleFactory } from './VehicleFactory.js'
22
import { ShopFactory } from './ShopFactory.js'
33

44
export class FactoryCreator {
5-
static getFactory(name, options) {
5+
static getFactory(name) {
66
switch (name) {
77
case 'vehicle':
8-
return new VehicleFactory(options)
8+
return new VehicleFactory()
99
case 'shop':
10-
return new ShopFactory(options)
10+
return new ShopFactory()
1111
default:
1212
return undefined
1313
}

abstract-factory/js/src/VehicleFactory.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@ export const VehicleType = {
77
BUS: 'bus',
88
CAR: 'car',
99
MOTORCYCLE: 'motorcycle',
10-
VAN: 'van',
10+
VAN: 'van'
1111
}
1212
export class VehicleFactory extends AbstractFactory {
13-
getVehicle(type, options) {
13+
getVehicle(type) {
1414
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)
15+
case VehicleType.BUS:
16+
return new Bus()
17+
case VehicleType.CAR:
18+
return new Car()
19+
case VehicleType.MOTORCYCLE:
20+
return new Motorcycle()
21+
case VehicleType.VAN:
22+
return new Van()
2323
default:
2424
return undefined
2525
}

abstract-factory/js/src/shop/DirectSaleShop.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Shop } from './Shop.js'
2-
32
export class DirectSaleShop extends Shop {
43
greetings() {
54
console.log('DirectSaleShop::greetings')

abstract-factory/ts/package.json

Lines changed: 9 additions & 0 deletions
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+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export class AbstractFactory {
2+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Vehicle } from './vehicle/Vehicle.js'
2+
import { Shop } from './shop/Shop.js'
3+
/*
4+
意图:提供一个创建一系列相互依赖的接口,而无需指定它们具体的类。
5+
主要作用:主要解决接口选择的问题。在一个对象家族系统里面里面,抽象定义多个产品对象。
6+
何时使用:产品很多,形成产品系列,而系统只需要处理其中某一类的产品。
7+
如何解决:在一个产品系列里面,定义多个产品对象。由抽象类来负责创建产品工厂。
8+
TS支持抽象类和抽象方法,这点与Java类似。
9+
*/
10+
export abstract class AbstractFactory {
11+
abstract getVehicle(type: string): Vehicle | void
12+
abstract getShop(type: string, options: Object): Shop | void
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { VehicleFactory } from './VehicleFactory.js';
2+
import { ShopFactory } from './ShopFactory.js';
3+
export class FactoryCreator {
4+
static getFactory(name) {
5+
switch (name) {
6+
case 'vehicle':
7+
return new VehicleFactory();
8+
case 'shop':
9+
return new ShopFactory();
10+
default:
11+
return;
12+
}
13+
}
14+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { VehicleFactory } from './VehicleFactory.js'
2+
import { ShopFactory } from './ShopFactory.js'
3+
import { AbstractFactory } from './AbstractFactory.js'
4+
5+
export type FactoryType = AbstractFactory | VehicleFactory | ShopFactory | void
6+
7+
export class FactoryCreator {
8+
static getFactory(name: string): FactoryType {
9+
switch (name) {
10+
case 'vehicle':
11+
return new VehicleFactory()
12+
case 'shop':
13+
return new ShopFactory()
14+
default:
15+
return
16+
}
17+
}
18+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { AgencyShop } from './shop/AgencyShop.js';
2+
import { DirectSaleShop } from './shop/DirectSaleShop.js';
3+
import { SupermarketShop } from './shop/SupermarketShop.js';
4+
import { AbstractFactory } from './AbstractFactory.js';
5+
export class ShopFactory extends AbstractFactory {
6+
getShop(type, options) {
7+
switch (type) {
8+
case 'AgencyShop':
9+
return new AgencyShop(options);
10+
case 'DirectSaleShop':
11+
return new DirectSaleShop(options);
12+
case 'SupermarketShop':
13+
return new SupermarketShop(options);
14+
default:
15+
return;
16+
}
17+
}
18+
getVehicle(type) {
19+
return;
20+
}
21+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { AgencyShop } from './shop/AgencyShop.js'
2+
import { DirectSaleShop } from './shop/DirectSaleShop.js'
3+
import { SupermarketShop } from './shop/SupermarketShop.js'
4+
import { AbstractFactory } from './AbstractFactory.js'
5+
import { Shop } from './shop/Shop.js'
6+
import { Vehicle } from './vehicle/Vehicle.js'
7+
8+
export class ShopFactory extends AbstractFactory {
9+
getShop(type: String, options: Object): Shop | void {
10+
switch (type) {
11+
case 'AgencyShop':
12+
return new AgencyShop(options)
13+
case 'DirectSaleShop':
14+
return new DirectSaleShop(options)
15+
case 'SupermarketShop':
16+
return new SupermarketShop(options)
17+
default:
18+
return
19+
}
20+
}
21+
getVehicle(type: string): Vehicle | void {
22+
return
23+
}
24+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { Bus } from './vehicle/Bus.js';
2+
import { Car } from './vehicle/Car.js';
3+
import { Motorcycle } from './vehicle/Motorcycle.js';
4+
import { Van } from './vehicle/Van.js';
5+
import { AbstractFactory } from './AbstractFactory.js';
6+
export var VehicleType;
7+
(function (VehicleType) {
8+
VehicleType["BUS"] = "bus";
9+
VehicleType["CAR"] = "car";
10+
VehicleType["MOTORCYCLE"] = "motorcycle";
11+
VehicleType["VAN"] = "van";
12+
})(VehicleType || (VehicleType = {}));
13+
export class VehicleFactory extends AbstractFactory {
14+
getVehicle(type) {
15+
switch (type) {
16+
case VehicleType.BUS:
17+
return new Bus();
18+
case VehicleType.CAR:
19+
return new Car();
20+
case VehicleType.MOTORCYCLE:
21+
return new Motorcycle();
22+
case VehicleType.VAN:
23+
return new Van();
24+
default:
25+
return;
26+
}
27+
}
28+
getShop(type) {
29+
return;
30+
}
31+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { Bus } from './vehicle/Bus.js'
2+
import { Car } from './vehicle/Car.js'
3+
import { Motorcycle } from './vehicle/Motorcycle.js'
4+
import { Van } from './vehicle/Van.js'
5+
import { AbstractFactory } from './AbstractFactory.js'
6+
import { Vehicle } from './vehicle/Vehicle.js'
7+
export enum VehicleType {
8+
BUS = 'bus',
9+
CAR = 'car',
10+
MOTORCYCLE = 'motorcycle',
11+
VAN = 'van'
12+
}
13+
export class VehicleFactory extends AbstractFactory {
14+
getVehicle(type: string): Vehicle | void {
15+
switch (type) {
16+
case VehicleType.BUS:
17+
return new Bus()
18+
case VehicleType.CAR:
19+
return new Car()
20+
case VehicleType.MOTORCYCLE:
21+
return new Motorcycle()
22+
case VehicleType.VAN:
23+
return new Van()
24+
default:
25+
return
26+
}
27+
}
28+
29+
getShop(type: string): void {
30+
return
31+
}
32+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export class AgencyShop {
2+
constructor(options) {
3+
}
4+
greetings() {
5+
console.log('AgencyShop::greetings');
6+
}
7+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Shop } from './Shop.js'
2+
3+
export class AgencyShop implements Shop {
4+
constructor(options: Object) {
5+
}
6+
greetings(): void {
7+
console.log('AgencyShop::greetings')
8+
}
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export class DirectSaleShop {
2+
constructor(options) {
3+
}
4+
greetings() {
5+
console.log('DirectSaleShop::greetings');
6+
}
7+
welcome() {
8+
console.log('Shop::Welcome');
9+
}
10+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Shop } from './Shop.js'
2+
3+
export class DirectSaleShop implements Shop {
4+
constructor(options: Object) {
5+
}
6+
greetings(): void {
7+
console.log('DirectSaleShop::greetings')
8+
}
9+
10+
welcome() {
11+
console.log('Shop::Welcome')
12+
}
13+
}

abstract-factory/ts/src/shop/Shop.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {};

abstract-factory/ts/src/shop/Shop.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export interface Shop {
2+
greetings(): void
3+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export class SupermarketShop {
2+
constructor(options) {
3+
}
4+
greetings() {
5+
console.log('SupermarketShop::greetings');
6+
}
7+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Shop } from './Shop.js'
2+
3+
export class SupermarketShop implements Shop {
4+
constructor(options: Object) {
5+
}
6+
greetings(): void {
7+
console.log('SupermarketShop::greetings')
8+
}
9+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export class Bus {
2+
run() {
3+
console.log('Bus::run().');
4+
}
5+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { Vehicle } from './Vehicle.js'
2+
export class Bus implements Vehicle {
3+
run() {
4+
console.log('Bus::run().')
5+
}
6+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export class Car {
2+
run() {
3+
console.log('Car::run().');
4+
}
5+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { Vehicle } from './Vehicle.js'
2+
export class Car implements Vehicle {
3+
run() {
4+
console.log('Car::run().')
5+
}
6+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export class Motorcycle {
2+
run() {
3+
console.log('Motorcycle::run().');
4+
}
5+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { Vehicle } from './Vehicle.js'
2+
export class Motorcycle implements Vehicle {
3+
run() {
4+
console.log('Motorcycle::run().')
5+
}
6+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export class Van {
2+
run() {
3+
console.log('Van::run().');
4+
}
5+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { Vehicle } from './Vehicle.js'
2+
export class Van implements Vehicle {
3+
run() {
4+
console.log('Van::run().')
5+
}
6+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export interface Vehicle {
2+
run(): void
3+
}

abstract-factory/ts/test/test.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<script type="module">
2+
// module need run in http server
3+
// like this: http://127.0.0.1/xxx/test.html
4+
import { test } from './test.js'
5+
alert('pls look console info.' + '\n\r' + test)
6+
</script>

0 commit comments

Comments
 (0)