Skip to content

Commit 9f48996

Browse files
added Command
1 parent 01db04f commit 9f48996

File tree

4 files changed

+165
-0
lines changed

4 files changed

+165
-0
lines changed

behavioral/command/App.ts

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { Television } from "./Television";
2+
import { Remote } from "./Remote";
3+
import { VolumeUpCommand, VolumeDownCommand, ChannelUpCommand, ChannelDownCommand } from "./Commands";
4+
5+
export default class CommandEx {
6+
7+
tv: Television;
8+
remote: Remote;
9+
10+
constructor(){
11+
12+
this.tv = new Television();
13+
this.remote = this.configure();
14+
this.run();
15+
}
16+
17+
configure(): Remote {
18+
let command1 = new VolumeUpCommand(this.tv);
19+
let command2 = new VolumeDownCommand(this.tv);
20+
let command3 = new ChannelUpCommand(this.tv);
21+
let command4 = new ChannelDownCommand(this.tv);
22+
23+
let remote = new Remote();
24+
25+
remote.add(command1);
26+
remote.add(command2);
27+
remote.add(command3);
28+
remote.add(command4);
29+
30+
return remote;
31+
}
32+
33+
run(): void {
34+
35+
this.remote.pushButtons();
36+
}
37+
}

behavioral/command/Commands.ts

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { Television } from "./Television";
2+
3+
export interface Command {
4+
5+
execute(): void;
6+
}
7+
8+
export class VolumeUpCommand implements Command {
9+
10+
tv: Television;
11+
12+
constructor(_tv: Television){
13+
14+
this.tv = _tv;
15+
}
16+
17+
execute(): void {
18+
19+
console.log("Turning volume up");
20+
this.tv.operation("volup");
21+
console.log(`Volume is now ${this.tv.getVolume()}`)
22+
}
23+
}
24+
25+
export class VolumeDownCommand implements Command {
26+
27+
tv: Television;
28+
29+
constructor(_tv: Television){
30+
31+
this.tv = _tv;
32+
}
33+
34+
execute(): void {
35+
36+
console.log("Turning volume down");
37+
this.tv.operation("voldown");
38+
console.log(`Volume is now ${this.tv.getVolume()}`)
39+
}
40+
}
41+
42+
export class ChannelUpCommand implements Command {
43+
44+
tv: Television;
45+
46+
constructor(_tv: Television){
47+
48+
this.tv = _tv;
49+
}
50+
51+
execute(): void {
52+
53+
console.log("Turning channel up");
54+
this.tv.operation("chup");
55+
console.log(`Channel is now ${this.tv.getChannel()}`)
56+
}
57+
}
58+
59+
export class ChannelDownCommand implements Command {
60+
61+
tv: Television;
62+
63+
constructor(_tv: Television){
64+
65+
this.tv = _tv;
66+
}
67+
68+
execute(): void {
69+
70+
console.log("Turning channel down");
71+
this.tv.operation("chdown");
72+
console.log(`Channel is now ${this.tv.getChannel()}`)
73+
}
74+
}

behavioral/command/Remote.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Command } from "./Commands";
2+
3+
export class Remote {
4+
5+
commands: Command[] = [];
6+
7+
add(command: Command): void {
8+
this.commands.push(command);
9+
}
10+
11+
pushButtons(): void {
12+
this.commands.forEach((command: Command) => command.execute());
13+
this.commands = [];
14+
}
15+
}

behavioral/command/Television.ts

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
export class Television {
2+
3+
private volume: number = 20;
4+
private channel: number = 1;
5+
6+
operation(opcode: string): void{
7+
8+
switch(opcode){
9+
10+
case "volup":
11+
12+
if(this.volume < 100) this.volume += 10;
13+
break;
14+
15+
case "voldown":
16+
17+
if(this.volume > 0) this.volume -= 10;
18+
break;
19+
20+
case "chup":
21+
22+
if(this.channel < 999) this.channel++;
23+
break;
24+
25+
case "chdown":
26+
27+
if(this.channel > 1) this.channel--;
28+
break;
29+
}
30+
}
31+
32+
getVolume(): number {
33+
return this.volume;
34+
}
35+
36+
getChannel(): number {
37+
return this.channel;
38+
}
39+
}

0 commit comments

Comments
 (0)