Type Script의 Abstract factory pattern
14903 단어 TypeScript
너무 한가해서.
써 보았다.그러나
인터페이스 관련 Type Script(0.8.3)
퍼블릭 static~속성을 지정할 수 없을 것 같아서
(new Slime).create();이런 느낌.
그리고 클라스의 속성으로 들기가 너무 게을러요.
module 썼어요.
ES5에 정의된 get 액세스기를 사용했기 때문에tsc hoge.tsc -target ES5
이렇게 대상을 지정하여 컴파일하세요...interface EnemyStatus {
name: string;
hitPoint: number;
magicPoint: number;
experiencePoint: number;
gold: number;
}
interface Enemy {
status: EnemyStatus;
name: string;
attaked: (attackPoint: number)=> void;
isAlive: ()=> boolean;
}
interface EnemyFactory {
create(): Enemy;
}
class BaseEnemy {
myStatus: EnemyStatus;
constructor(status: EnemyStatus) {
this.myStatus = status;
};
public get status() { return this.myStatus; }
public get name(){ return this.myStatus.name; }
public get hitPoint(){ return this.myStatus.hitPoint; }
public get magicPoint(){ return this.myStatus.magicPoint; }
public get experiencePoint(){ return this.myStatus.experiencePoint; }
public get gold(){ return this.myStatus.gold; }
public attaked(attackPoint: number): void {
this.myStatus.hitPoint -= attackPoint;
}
public isAlive(): boolean {
return this.myStatus.hitPoint > 0;
}
}
class SlimeFactory implements EnemyFactory {
create(): Slime {
return new Slime;
}
}
class SlimeKnightFactory implements EnemyFactory {
create(): SlimeKnight {
return new SlimeKnight;
}
}
class DrakeeFactory implements EnemyFactory {
create(): Drakee {
return new Drakee;
}
}
class Slime extends BaseEnemy implements Enemy {
constructor() {
super({
name: "Slime",
hitPoint: 6,
magicPoint: 2,
experiencePoint: 1,
gold: 1
});
}
}
class SlimeKnight extends BaseEnemy implements Enemy {
constructor() {
super({
name: "SlimeKnight",
hitPoint: 194,
magicPoint: 7,
experiencePoint: 119,
gold: 7
});
}
}
class Drakee extends BaseEnemy implements Enemy {
constructor() {
super({
name: "Drakee",
hitPoint: 13,
magicPoint: 4,
experiencePoint: 3,
gold: 2
});
}
}
module BuildEnemy {
var enemies: any[] = [
(new SlimeFactory).create(),
(new SlimeKnightFactory).create(),
(new DrakeeFactory).create()
];
export function singleSubstanceEnemy(): BaseEnemy {
return enemies[randIndex()];
}
function randIndex(): number {
return (parseInt(String(Math.random() * 10 % (enemies.length)), 10));
}
}
module Runner {
export function singleSubstanceEnemyScenario(): void {
var enemy: BaseEnemy = BuildEnemy.singleSubstanceEnemy();
console.log("Enemy name is: " + enemy.name);
console.log("Enemy hit point is: " + enemy.hitPoint);
enemy.attaked(1);
console.log("Attacked! Damage is: " + 1);
console.log("Enemy hit point is: " + enemy.hitPoint);
console.log("Enemy is alived?: " + enemy.isAlive());
}
}
Runner.singleSubstanceEnemyScenario();
실행 결과
이렇게 된 느낌.
괜찮죠?
Reference
이 문제에 관하여(Type Script의 Abstract factory pattern), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/futoase/items/7328f531202030297b1b
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
tsc hoge.tsc -target ES5
interface EnemyStatus {
name: string;
hitPoint: number;
magicPoint: number;
experiencePoint: number;
gold: number;
}
interface Enemy {
status: EnemyStatus;
name: string;
attaked: (attackPoint: number)=> void;
isAlive: ()=> boolean;
}
interface EnemyFactory {
create(): Enemy;
}
class BaseEnemy {
myStatus: EnemyStatus;
constructor(status: EnemyStatus) {
this.myStatus = status;
};
public get status() { return this.myStatus; }
public get name(){ return this.myStatus.name; }
public get hitPoint(){ return this.myStatus.hitPoint; }
public get magicPoint(){ return this.myStatus.magicPoint; }
public get experiencePoint(){ return this.myStatus.experiencePoint; }
public get gold(){ return this.myStatus.gold; }
public attaked(attackPoint: number): void {
this.myStatus.hitPoint -= attackPoint;
}
public isAlive(): boolean {
return this.myStatus.hitPoint > 0;
}
}
class SlimeFactory implements EnemyFactory {
create(): Slime {
return new Slime;
}
}
class SlimeKnightFactory implements EnemyFactory {
create(): SlimeKnight {
return new SlimeKnight;
}
}
class DrakeeFactory implements EnemyFactory {
create(): Drakee {
return new Drakee;
}
}
class Slime extends BaseEnemy implements Enemy {
constructor() {
super({
name: "Slime",
hitPoint: 6,
magicPoint: 2,
experiencePoint: 1,
gold: 1
});
}
}
class SlimeKnight extends BaseEnemy implements Enemy {
constructor() {
super({
name: "SlimeKnight",
hitPoint: 194,
magicPoint: 7,
experiencePoint: 119,
gold: 7
});
}
}
class Drakee extends BaseEnemy implements Enemy {
constructor() {
super({
name: "Drakee",
hitPoint: 13,
magicPoint: 4,
experiencePoint: 3,
gold: 2
});
}
}
module BuildEnemy {
var enemies: any[] = [
(new SlimeFactory).create(),
(new SlimeKnightFactory).create(),
(new DrakeeFactory).create()
];
export function singleSubstanceEnemy(): BaseEnemy {
return enemies[randIndex()];
}
function randIndex(): number {
return (parseInt(String(Math.random() * 10 % (enemies.length)), 10));
}
}
module Runner {
export function singleSubstanceEnemyScenario(): void {
var enemy: BaseEnemy = BuildEnemy.singleSubstanceEnemy();
console.log("Enemy name is: " + enemy.name);
console.log("Enemy hit point is: " + enemy.hitPoint);
enemy.attaked(1);
console.log("Attacked! Damage is: " + 1);
console.log("Enemy hit point is: " + enemy.hitPoint);
console.log("Enemy is alived?: " + enemy.isAlive());
}
}
Runner.singleSubstanceEnemyScenario();
이렇게 된 느낌.
괜찮죠?
Reference
이 문제에 관하여(Type Script의 Abstract factory pattern), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/futoase/items/7328f531202030297b1b텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)