Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
theodesp committed Apr 22, 2021
1 parent af37a3d commit fbee225
Showing 1 changed file with 91 additions and 0 deletions.
91 changes: 91 additions & 0 deletions chapters/chapter-4_Structural_Design_Patterns/Bridge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
interface Box {
id: string;
value: string;
}
interface BoxArranger {
arrangeItem(item: Box, items: Box[]): Box[];
}

abstract class BoxContainer {
constructor(public items: Box[] = [], protected boxArranger: BoxArranger) {}
arrangeItem(item: Box) {}
}

class StraightBoxContainer extends BoxContainer {
arrangeItem(item: Box) {
this.items = this.boxArranger.arrangeItem(item, this.items);
}
}

class ReversingBoxContainer extends BoxContainer {
arrangeItem(item: Box) {
this.items = this.boxArranger.arrangeItem(item, this.items).reverse();
}
}

class PutLastBoxArranger implements BoxArranger {
arrangeItem(item: Box, items: Box[]): Box[] {
items = items.concat(item);
return items;
}
}

class PutFirstBoxArranger implements BoxArranger {
arrangeItem(item: Box, items: Box[]): Box[] {
let result = items.slice();
result.unshift(item);
return result;
}
}
const items: Box[] = [
{
id: "1",
value: "abc",
},
];
const pfa = new PutFirstBoxArranger();
const pla = new PutLastBoxArranger();
const rv = new StraightBoxContainer(items, pla);
rv.arrangeItem({
id: "3",
value: "dfa",
}); // [ { id: '3', value: 'dfa' }, { id: '1', value: 'abc' } ]
console.log(rv.items);
const sc = new StraightBoxContainer(items, pfa);
sc.arrangeItem({
id: "3",
value: "dfa",
});
console.log(sc.items);
[
{ id: "3", value: "dfa" },
{ id: "1", value: "abc" },
];

// // Implementor type
// interface StorageItem {
// id: string;
// value: string;
// }
// type ExtendedStorageItem = StorageItem & {
// createdAt: Date;
// };
// // Abstraction type
// interface List<T> {
// push(item: T);
// pop(): T;
// shift(): T;
// unshift(): T;
// }
// interface Node {
// value: number;
// next: Node;
// }
// class ArrayList<T> implements List<T> {
// constructor(private items: StorageItem) {}
// // implements methods of List
// }
// class LinkedList<T> implements List<T> {
// constructor(private root: Node, private items: StorageItem) {}
// // implements methods of List
// }

0 comments on commit fbee225

Please sign in to comment.