static void Main(string[] args) { //BuilderExample(); //FactoryExample(); //FactoryMethodExample(); //FactoryMethodExample2(); //ProtoTypeExample(); //SingletonExample(); //AdapterExample(); //BridgeExample(); //CompositeExample(); //DecoratorExample(); //FacadeExample(); //FlyweightExample();
ProxyExample();
//Console.WriteLine("Hello World!"); Console.ReadKey(); }
static void ProxyExample() { MathProxy proxy = new MathProxy();
Console.WriteLine("4 + 2 = {0}", proxy.Add(4, 2)); Console.WriteLine("4 - 2 = {0}", proxy.Sub(4, 2)); Console.WriteLine("4 * 2 = {0}", proxy.Mul(4, 2)); Console.WriteLine("4 / 2 = {0}", proxy.Div(4, 2)); }
main.cs |
namespace DesignPatterns.Structural_Patterns.Proxy { public interface IMath { double Add(double x, double y); double Sub(double x, double y); double Mul(double x, double y); double Div(double x, double y); } }
IMath.cs |
namespace DesignPatterns.Structural_Patterns.Proxy { public class Math : IMath { public double Add(double x, double y) { return x + y; }
public double Div(double x, double y) { return x - y; }
public double Mul(double x, double y) { return x * y; }
public double Sub(double x, double y) { return x / y; } } }
Math.cs |
namespace DesignPatterns.Structural_Patterns.Proxy { public class MathProxy : IMath { private Math math = new Math();
public double Add(double x, double y) { return math.Add(x, y); }
public double Div(double x, double y) { return math.Sub(x, y); }
public double Mul(double x, double y) { return math.Mul(x, y); }
public double Sub(double x, double y) { return math.Sub(x, y); } } }
MathProxy.cs |
참고 사이트
'C# 디자인패턴' 카테고리의 다른 글
15# ChainOfResponsibility 패턴 Behavioral Patterns (0) | 2019.06.19 |
---|---|
13# Flyweight패턴 Structural Patterns (0) | 2019.06.19 |
12# Facade패턴 Structural Patterns (0) | 2019.06.03 |
11# Decorator패턴 Structural Patterns (0) | 2019.06.03 |
10# Composite패턴 Structural Patterns (0) | 2019.06.03 |