parent
24bb24cb25
commit
3d8d1058ed
@ -0,0 +1,84 @@ |
||||
namespace DesignPattern.Patterns |
||||
{ |
||||
interface ICommand |
||||
{ |
||||
void Execute(); |
||||
} |
||||
|
||||
class User |
||||
{ |
||||
private List<ICommand> _commands = new List<ICommand>(); |
||||
|
||||
public void AddCommand(ICommand command) |
||||
{ |
||||
_commands.Add(command); |
||||
} |
||||
|
||||
public void AddCommand(StockBroker broker, string symbol, TxType txType, int qty) |
||||
{ |
||||
ICommand cmd = new StockCommand(broker, symbol, txType, qty); |
||||
_commands.Add(cmd); |
||||
} |
||||
|
||||
public void ExecuteAll() |
||||
{ |
||||
foreach (var cmd in _commands) |
||||
{ |
||||
cmd.Execute(); |
||||
} |
||||
} |
||||
} |
||||
|
||||
class StockCommand : ICommand |
||||
{ |
||||
private StockBroker _broker; |
||||
private string _symbol; |
||||
private TxType _txType; |
||||
private int _qty; |
||||
|
||||
public StockCommand(StockBroker broker, string symbol, TxType txType, int qty) |
||||
{ |
||||
_broker = broker; |
||||
_symbol = symbol; |
||||
_txType = txType; |
||||
_qty = qty; |
||||
} |
||||
|
||||
public void Execute() |
||||
{ |
||||
_broker.Process(_symbol, _txType, _qty); |
||||
} |
||||
} |
||||
|
||||
enum TxType |
||||
{ |
||||
Buy, |
||||
Sell |
||||
} |
||||
|
||||
class StockBroker |
||||
{ |
||||
public void Process(string stockSymbol, TxType txType, int qty) |
||||
{ |
||||
Console.WriteLine($"{txType}, {stockSymbol}, {qty}"); |
||||
} |
||||
} |
||||
|
||||
class CommandClient |
||||
{ |
||||
public static void Work() |
||||
{ |
||||
var broker = new StockBroker(); |
||||
|
||||
User user = new User(); |
||||
|
||||
ICommand cmd = new StockCommand(broker, "MSFT", TxType.Buy, 150); |
||||
user.AddCommand(cmd); |
||||
user.AddCommand(broker, "AMZN", TxType.Sell, 2000); |
||||
user.AddCommand(broker, "BRK.B", TxType.Buy, 5000); |
||||
user.AddCommand(broker, "APPL", TxType.Sell, 1000); |
||||
|
||||
user.ExecuteAll(); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,75 @@ |
||||
using System.Runtime.CompilerServices; |
||||
|
||||
namespace DesignPattern.Patterns |
||||
{ |
||||
public interface IProduct |
||||
{ |
||||
string Operation(); |
||||
} |
||||
|
||||
abstract class Creator |
||||
{ |
||||
public abstract IProduct FactoryMethod(); |
||||
|
||||
public string SomeOperation() |
||||
{ |
||||
var product = FactoryMethod(); |
||||
|
||||
var result = $"Creator: The same creator's code has just worked with {product.Operation()}"; |
||||
|
||||
return result; |
||||
} |
||||
} |
||||
|
||||
class ContreteProduct1 : IProduct |
||||
{ |
||||
public string Operation() |
||||
{ |
||||
return $"[Result of {this.GetType().Name}]"; |
||||
} |
||||
} |
||||
|
||||
class ContreteProduct2 : IProduct |
||||
{ |
||||
public string Operation() |
||||
{ |
||||
return $"[Result of {this.GetType().Name}]"; |
||||
} |
||||
} |
||||
|
||||
class ConcreteCreator1 : Creator |
||||
{ |
||||
public override IProduct FactoryMethod() |
||||
{ |
||||
return new ContreteProduct1(); |
||||
} |
||||
} |
||||
|
||||
class ConcreteCreator2 : Creator |
||||
{ |
||||
public override IProduct FactoryMethod() |
||||
{ |
||||
return new ContreteProduct2(); |
||||
} |
||||
} |
||||
|
||||
class FactoryClient |
||||
{ |
||||
public static void Work() |
||||
{ |
||||
Console.WriteLine("App: Launch with the Creator1"); |
||||
ClientCode(new ConcreteCreator1()); |
||||
|
||||
Console.WriteLine(); |
||||
|
||||
Console.WriteLine("App: Launch with the Creator2"); |
||||
ClientCode(new ConcreteCreator2()); |
||||
} |
||||
|
||||
public static void ClientCode(Creator creator) |
||||
{ |
||||
Console.WriteLine($"Client: I'm not aware of the creator's class, but it still works."); |
||||
Console.WriteLine(creator.SomeOperation()); |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue