Skip to content

Commit 2e8a771

Browse files
author
Patrick.Creutzburg
committed
add adapter pattern
1 parent 1b58a2e commit 2e8a771

File tree

4 files changed

+57
-3
lines changed

4 files changed

+57
-3
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { Toy } from './toy.model';
2+
import { Cat } from './cat.model';
3+
4+
export class CatAdapter implements Toy {
5+
private cat: Cat;
6+
7+
constructor(cat: Cat) {
8+
this.cat = cat;
9+
}
10+
11+
public squeak(): string {
12+
return this.cat.makeSound();
13+
}
14+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
export interface Cat {
2+
hunt(): string;
3+
makeSound(): string;
4+
}
5+
6+
// adaptee
7+
export class BritishShortHair implements Cat {
8+
public hunt(): string {
9+
return 'Hunting birds..';
10+
}
11+
12+
public makeSound(): string {
13+
return 'Miau.. Miau..';
14+
}
15+
}
16+
17+
export class NorwegianForest implements Cat {
18+
public hunt(): string {
19+
return 'Hunting fish..';
20+
}
21+
22+
public makeSound(): string {
23+
return 'Rrrroar.. Rrrroar..';
24+
}
25+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// target interface - toys dont hunt and only squeak
2+
export interface Toy {
3+
squeak(): string;
4+
}
5+
6+
export class ToyCat implements Toy {
7+
public squeak(): string {
8+
return 'squeaaaaaak...';
9+
}
10+
}

src/app/app-routing.module.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,16 @@ const routes: Routes = [
1414
path: 'prototype',
1515
loadChildren: () => import('./03. Prototype/prototype.module').then(m => m.PrototypeModule)
1616
},
17-
{ path: 'builder',
17+
{
18+
path: 'builder',
1819
loadChildren: () => import('./04. Builder/builder.module').then(m => m.BuilderModule)
1920
},
20-
{ path: 'factory-method',
21-
loadChildren: () => import('./05. Factory Method/factory-method.module').then(m => m.FactoryMethodModule) }
21+
{
22+
path: 'factory-method',
23+
loadChildren: () => import('./05. Factory Method/factory-method.module').then(m => m.FactoryMethodModule) },
24+
{
25+
path: 'adapter',
26+
loadChildren: () => import('./06. Adapter/adapter.module').then(m => m.AdapterModule) }
2227
];
2328

2429
@NgModule({

0 commit comments

Comments
 (0)