parent
24bb24cb25
commit
d9167b653b
@ -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