Prototype
プロトタイプデザインパターンが最善の解決法だと感じられたことは一度もなかった.
code:.cs
public enum HumanType {
Artist,
Musician,
}
public class HumanPrototype {
private List<HumanType, Human> humanList;
public HumanPrototype() {
humanList = new List<HumanType, Human>();
}
public void Register(HumanType t, Human h) => humanList.Add(t, h);
public Human Duplicate(HumanType t) => humanListt.Clone(); // HumanはClonableを実装する必要がある }
public class Main {
public void Main() {
var artist = new Human(Hair.Red, Clothes.White, Special.Art);
var music = new Human(Hair.Black, Clothes.Black, Special.Music);
var proto = new HumanPrototype();
proto.Register(HumanType.Artist, artist);
proto.Register(HumanType.Musician, music);
// たくさんの引数を指定しないでも登録したインスタンスを新規に生成できる.
var artistman = proto.Duplicate(HumanType.Artist);
var musicman = proto.Duplicate(HumanType.Musician);
}
}