How to use the Strategy Pattern with C#?

前端 未结 7 786
暗喜
暗喜 2021-01-01 18:29

Here\'s what I have so far:

namespace Strategy
{
    interface IWeaponBehavior
    {
        void UseWeapon();
    }
}

namespace Strategy
{
    class Knife          


        
7条回答
  •  情话喂你
    2021-01-01 18:48

      class Character
      {
        private IWeaponBehavior _weapon;
    
        // Constructor
        public Character(IWeaponBehavior strategy)
        {
          this._weapon = strategy;
        }
    
        public void WeaponInterface()
        {
          _weapon.UseWeapon();
        }
      }
    

提交回复
热议问题