コード例 #1
0
        public void StrategyTest()
        {
            // given
            var          goby       = "bus";
            IGotStrategy goStrategy = null;

            switch (goby)
            {
            case "bus":
                goStrategy = new GoByBus();
                break;

            case "train":
            default:
                goStrategy = new GoByTrain();
                break;
            }
            var traveler = new Traveler("小明", goStrategy);

            // when
            var result = traveler.Travel("台北");

            // then
            Assert.AreEqual("小明搭巴士去台北", result);
        }
コード例 #2
0
        public string Travel(string place, string goby)
        {
            IGotStrategy goStrategy = null;

            switch (goby)
            {
            case "bus":
                goStrategy = new GoByBus();
                break;

            case "train":
            default:
                goStrategy = new GoByTrain();
                break;
            }
            return(_name + goStrategy.Go(place));
        }
コード例 #3
0
 public Traveler(string name, IGotStrategy goStrategy)
 {
     _name       = name;
     _goStrategy = goStrategy;
 }