-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
// } |