Skip to content

Instantly share code, notes, and snippets.

@jiaming0708
Last active August 6, 2021 03:03
Show Gist options
  • Save jiaming0708/43901411a5164db71ace1d75d2d10cef to your computer and use it in GitHub Desktop.
Save jiaming0708/43901411a5164db71ace1d75d2d10cef to your computer and use it in GitHub Desktop.
design pattern sample
using System;
namespace DesignPattern.Facade
{ class Program
{
static void Main()
{
var employ = new 店員();
employ.買餅乾();
var coffee = employ.買咖啡();
var dinner = employ.買便當();
}
}
class 店員
{
結帳 pay = new();
煮咖啡 coffee = new();
加熱 hot = new();
public void 買餅乾()
{
Console.WriteLine("---買餅乾---");
pay.action();
}
public object 買咖啡()
{
Console.WriteLine("---買咖啡---");
pay.action();
coffee.action();
return new { coffee = true };
}
public object 買便當()
{
Console.WriteLine("---買便當---");
pay.action();
hot.action();
return new { food = true };
}
}
class 結帳
{
public void action()
{
Console.WriteLine("結帳囉");
}
}
class 加熱
{
public void action()
{
Console.WriteLine("加熱完成");
}
}
class 煮咖啡
{
public void action()
{
Console.WriteLine("咖啡好了");
}
}
}
using System;
using System.Collections.Generic;
namespace DesignPattern.Interpreter
{
class Program
{
static void Main()
{
var context = new Context();
context.Text = "二零二一";
var number = new ChineseExpression().Interpret(context);
Console.WriteLine($"{context.Text} to {number}");
context.Text = "2021";
number = new NumberExpression().Interpret(context);
Console.WriteLine($"{context.Text} to {number}");
}
}
class Context
{
public string Text { get; set; }
}
abstract class Expression
{
public string Interpret(Context context)
{
List<string> result = new();
foreach(var text in context.Text)
{
result.Add(Excute(text));
}
return String.Join("", result.ToArray());
}
public abstract string Excute(char text);
}
class ChineseExpression: Expression
{
public override string Excute(char text)
{
switch (text)
{
case '零':
return "0";
case '一':
return "1";
case '二':
return "2";
case '三':
return "3";
case '四':
return "4";
case '五':
return "5";
case '六':
return "6";
case '七':
return "7";
case '八':
return "8";
case '九':
return "9";
default:
return "";
}
}
}
class NumberExpression : Expression
{
public override string Excute(char text)
{
switch (text)
{
case '0':
return "零";
case '1':
return "一";
case '2':
return "二";
case '3':
return "三";
case '4':
return "四";
case '5':
return "五";
case '6':
return "六";
case '7':
return "七";
case '8':
return "八";
case '9':
return "九";
default:
return "";
}
}
}
}
using system;
namespace DesignPattern.Mediator
{
class Program
{
static void Main(string[] args)
{
var eng = new Engineer();
var pd = new Designer();
var mkt = new Marketing();
var pm = new PM(eng, pd, mkt);
eng.Update("login completed");
pd.Update("home page mockup ready");
Console.ReadKey();
}
}
abstract class Mediator
{
public abstract void UpdateProgress(string progress, Colleague colleague);
}
abstract class Colleague
{
protected Mediator Mediator;
public void SetMediator(Mediator mediator)
{
Mediator = mediator;
}
public abstract void Update(string progress);
public abstract void Receive(string progress, string who);
}
class PM : Mediator
{
Engineer ENG;
Designer PD;
Marketing MKT;
public PM(Engineer eng, Designer pd, Marketing mkt)
{
ENG = eng;
ENG.SetMediator(this);
PD = pd;
PD.SetMediator(this);
MKT = mkt;
MKT.SetMediator(this);
}
public override void UpdateProgress(string message, Colleague colleague)
{
switch (colleague)
{
case Engineer:
{
PD.Receive(message, "Engineer");
MKT.Receive(message, "Engineer");
break;
}
case Designer:
{
ENG.Receive(message, "Designer");
MKT.Receive(message, "Designer");
break;
}
case Marketing:
{
PD.Receive(message, "Marketing");
ENG.Receive(message, "Marketing");
break;
}
default:
{
ENG.Receive(message, "BOSS");
PD.Receive(message, "BOSS");
MKT.Receive(message, "BOSS");
break;
}
}
}
}
class Engineer : Colleague
{
public override void Update(string progress)
{
Mediator.UpdateProgress(progress, this);
}
public override void Receive(string progress, string who)
{
Console.WriteLine($"engineer receive {progress} from {who}");
}
}
class Designer : Colleague
{
public override void Update(string progress)
{
Mediator.UpdateProgress(progress, this);
}
public override void Receive(string progress, string who)
{
Console.WriteLine($"designer receive {progress} from {who}");
}
}
class Marketing : Colleague
{
public override void Update(string progress)
{
Mediator.UpdateProgress(progress, this);
}
public override void Receive(string progress, string who)
{
Console.WriteLine($"marketing receive {progress} from {who}");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment