Skip to content

Instantly share code, notes, and snippets.

@simpleprogrammer-shared
simpleprogrammer-shared / refactoring-switches-to-classes-1.cs
Last active April 25, 2018 02:38
Refactoring Switches to Classes 1
// In fighting code
switch(classType)
{
case WARRIOR:
swingSword();
break;
case MAGE:
castSpell();
break;
case THIEF:
public interface CharacterClass
{
void Attack();
ArmorResponse WearArmor(armor);
}
public class Warrior : CharacterClass
{
void Attack()
{
swingSword();
}
ArmorResponse WearArmor(armor)
{
return CAN_WEAR;
public Dictionary characterDictionary =
new Dictionary {
{ WARRIOR, new Warrior() },
{ MAGE, new Mage() },
{ THIEF, new Thief() }
};
// In fighting code
myCharacter.Attack();
// In wear armor code
var armorResponse = myCharacter.WearArmor(armor);
public class CurrencyConverter
{
CurrencyConverter(ICurrency destinationCurrency)
{ ... }
ICurrency Convert(ICurrency sourceCurrency, decimal amount);
{ ... }
}
public interface ICurrency
{
ConvertUsing(CurrencyConverter converter);
}
SELECT *
FROM
orders o
JOIN customers c
ON o.customerid = c.customerid
WHERE
c.companyname = 'Around the Horn'
SELECT *
FROM
orders o
CROSS APPLY (
SELECT * FROM
customers c
WHERE
o.customerid = c.customerid) AS c
WHERE
c.companyname = 'Around the Horn'
SELECT *
FROM
orders o
CROSS APPLY (SELECT *
FROM
customers c
WHERE
o.customerid = c.customerid) AS c
WHERE
c.companyname = 'Around the Horn'