Last active
October 17, 2021 04:23
-
-
Save cch12313/f7e1829b09017f1d4ea7f19dffb1ad04 to your computer and use it in GitHub Desktop.
Strategy Pattern
This file contains hidden or 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
using System; | |
namespace design_pattern | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Toyote car1 = new Toyote(); | |
Bens car2 = new Bens(); | |
Tezla car3 = new Tezla(); | |
car1.Skill(); | |
car2.Skill(); | |
car3.Skill(); | |
} | |
} | |
public class Toyote | |
{ | |
private ISkill _skill; | |
public Toyote() | |
{ | |
_skill = new Turbo(); | |
} | |
public void Skill() | |
{ | |
_skill.Cast(); | |
} | |
} | |
public class Bens | |
{ | |
private ISkill _skill; | |
public Bens() | |
{ | |
_skill = new Turbo(); | |
} | |
public void Skill() | |
{ | |
_skill.Cast(); | |
} | |
} | |
public class Tezla | |
{ | |
private ISkill _skill; | |
public Tezla() | |
{ | |
_skill = new ElectromagneticPulseSkill(); | |
} | |
public void Skill() | |
{ | |
_skill.Cast(); | |
} | |
} | |
public interface ISkill | |
{ | |
public void Cast(); | |
} | |
public class Turbo: ISkill | |
{ | |
public void Cast() | |
{ | |
Console.WriteLine("噴射加速~"); | |
} | |
} | |
public class ElectromagneticPulseSkill: ISkill | |
{ | |
public void Cast() | |
{ | |
Console.WriteLine("電磁脈衝加速~"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment