Skip to content

Commit 4fe4356

Browse files
PatrickPatrick
authored andcommitted
feature(): add adapter pattern
1 parent 2e8a771 commit 4fe4356

File tree

6 files changed

+183
-0
lines changed

6 files changed

+183
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { NgModule } from '@angular/core';
2+
import { Routes, RouterModule } from '@angular/router';
3+
4+
import { AdapterComponent } from './adapter/adapter.component';
5+
6+
const routes: Routes = [{ path: '', component: AdapterComponent }];
7+
8+
@NgModule({
9+
imports: [RouterModule.forChild(routes)],
10+
exports: [RouterModule]
11+
})
12+
export class AdapterRoutingModule { }

src/app/06. Adapter/adapter.module.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { NgModule } from '@angular/core';
2+
import { CommonModule } from '@angular/common';
3+
4+
import { AdapterRoutingModule } from './adapter-routing.module';
5+
import { AdapterComponent } from './adapter/adapter.component';
6+
import {AngularMaterialModule} from "../modules/angular-material.module";
7+
8+
9+
@NgModule({
10+
declarations: [AdapterComponent],
11+
imports: [
12+
CommonModule,
13+
AdapterRoutingModule,
14+
AngularMaterialModule
15+
]
16+
})
17+
export class AdapterModule { }
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<div class="container">
2+
<h1>
3+
Adapter
4+
</h1>
5+
<div class="content">
6+
<mat-card>
7+
<mat-card-title>
8+
Cat
9+
</mat-card-title>
10+
<mat-card-subtitle>
11+
It's only a cat
12+
</mat-card-subtitle>
13+
<mat-card-actions>
14+
<button mat-raised-button (click)="btnCatSound()">Make sounds</button>
15+
</mat-card-actions>
16+
<mat-card-content>
17+
<div *ngFor="let catSound of catSounds">
18+
{{ catSound }}
19+
</div>
20+
</mat-card-content>
21+
22+
</mat-card>
23+
<mat-card>
24+
<mat-card-title>
25+
Toy
26+
</mat-card-title>
27+
<mat-card-subtitle>
28+
It's only a toy
29+
</mat-card-subtitle>
30+
<mat-card-actions>
31+
<button mat-raised-button (click)="btnToySound()">Make sounds</button>
32+
</mat-card-actions>
33+
<mat-card-content>
34+
<div *ngFor="let toySound of toySounds">
35+
{{ toySound }}
36+
</div>
37+
</mat-card-content>
38+
39+
</mat-card>
40+
<mat-card>
41+
<mat-card-title>
42+
ToyCat
43+
</mat-card-title>
44+
<mat-card-subtitle>
45+
It's a toy, but imitates a cat :O
46+
</mat-card-subtitle>
47+
<mat-card-actions>
48+
<button mat-raised-button (click)="btnCatAdapterSound()">Make sounds</button>
49+
</mat-card-actions>
50+
<mat-card-content>
51+
52+
<div *ngFor="let catAdapterSound of catAdapterSounds">
53+
{{ catAdapterSound }}
54+
</div>
55+
</mat-card-content>
56+
57+
</mat-card>
58+
</div>
59+
</div>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
div {
2+
text-align: center;
3+
}
4+
5+
div.container {
6+
display: flex;
7+
flex-direction: column;
8+
align-items: center;
9+
}
10+
11+
div.content {
12+
display: flex;
13+
flex-direction: row;
14+
align-items: flex-start;
15+
}
16+
17+
mat-card {
18+
margin: 15px;
19+
max-width: 300px;
20+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { ComponentFixture, TestBed } from '@angular/core/testing';
2+
3+
import { AdapterComponent } from './adapter.component';
4+
5+
describe('AdapterComponent', () => {
6+
let component: AdapterComponent;
7+
let fixture: ComponentFixture<AdapterComponent>;
8+
9+
beforeEach(async () => {
10+
await TestBed.configureTestingModule({
11+
declarations: [ AdapterComponent ]
12+
})
13+
.compileComponents();
14+
});
15+
16+
beforeEach(() => {
17+
fixture = TestBed.createComponent(AdapterComponent);
18+
component = fixture.componentInstance;
19+
fixture.detectChanges();
20+
});
21+
22+
it('should create', () => {
23+
expect(component).toBeTruthy();
24+
});
25+
});
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { Component, OnInit } from '@angular/core';
2+
import {Cat, NorwegianForest} from "../models/cat.model";
3+
import {Toy, ToyCat} from "../models/toy.model";
4+
import {CatAdapter} from "../models/cat.adapter";
5+
6+
@Component({
7+
selector: 'app-adapter',
8+
templateUrl: './adapter.component.html',
9+
styleUrls: ['./adapter.component.scss']
10+
})
11+
export class AdapterComponent {
12+
public catSounds: string[];
13+
public toySounds: string[];
14+
public catAdapterSounds: string[];
15+
16+
private cat: Cat; // adaptee
17+
private toy: Toy; // target interface
18+
private catAdapter: Toy; // adapter
19+
20+
constructor() {
21+
this.catSounds = [];
22+
this.toySounds = [];
23+
this.catAdapterSounds = [];
24+
25+
this.client();
26+
}
27+
28+
public btnCatSound() {
29+
this.catSounds.push(this.cat.makeSound());
30+
}
31+
32+
public btnToySound() {
33+
this.toySounds.push(this.toy.squeak());
34+
}
35+
36+
public btnCatAdapterSound() {
37+
this.catAdapterSounds.push(this.catAdapter.squeak());
38+
}
39+
40+
private client(): void {
41+
42+
// inspired by
43+
// https://www.geeksforgeeks.org/adapter-pattern/
44+
45+
this.cat = new NorwegianForest();
46+
this.toy = new ToyCat();
47+
this.catAdapter = new CatAdapter(this.cat);
48+
}
49+
50+
}

0 commit comments

Comments
 (0)