Flyweightパターン
概要
共通部分を切り出して、同じものを使い回すことで、メモリ使用量を減らすパターン(正規化と似ている)
例
bad
type Tree = {
position: { x: number; y: number };
color: string;
length: number;
}
// 大量の木を生成
const tree1 = {
position: { x: 1, y: 2 },
color: 'green',
length: 10,
};
const tree2 = {
position: { x: 1, y: 2 },
color: 'green',
length: 10,
};
//...
colorやlengthが同じ値を持っているが、それぞれの木で同じ値を持っているため、メモリの無駄遣いになっている
good
type TreeType = {
color: string;
length: number;
}
type Tree = {
position: { x: number; y: number };
type: TreeType;
}
const oakType = {
color: 'green',
length: 10,
}
const tree1 = {
position: { x: 1, y: 2 },
type: oakType,
};
const tree2 = {
position: { x: 1, y: 2 },
type: oakType,
};
//...
使い所
- メモリ使用量を減らしたいとき
- 繰り返しの多いデータが大きいほど効果が大きい(当たり前)
注意点
- 共有部分を切り出すと、コードが複雑になる可能性がある
- データが追いづらくなったり、変更が難しくなったりする可能性がある