代理模式,先占位,由于文章太耗时,后面补上。
using System;
using System.Collections.Generic;
using System.Linq;
namespace SimpleFactory
{
class Program
{
static void Main(string[] args)
{
现金支付 cash = new 现金支付();
支付代理 proxy1 = new 支付代理(cash);
proxy1.支付();
proxy1.获取免单人数();
传单支付 leflet2 = new 传单支付();
支付代理 proxy2 = new 支付代理(leflet2);
proxy2.支付();
proxy2.获取免单人数();
传单支付 leflet3 = new 传单支付();
支付代理 proxy3 = new 支付代理(leflet3);
proxy3.支付();
proxy3.获取免单人数();
}
}
public interface 支付方式
{
void 支付();
}
public class 现金支付 : 支付方式
{
public void 支付()
{
Console.WriteLine("用现金支付了9.9元");
}
}
public class 传单支付 : 支付方式
{
public void 支付()
{
Console.WriteLine("拿着新客宣传单,本次免单,支付0元");
}
}
public class 支付代理 : 支付方式
{
private static int _免单人数;
private 支付方式 _service;
public 支付代理(支付方式 service)
{
this._service = service;
}
public void 支付()
{
this._service?.支付();
if (this._service is 传单支付)
{
_免单人数++;
}
}
public void 获取免单人数()
{
Console.WriteLine("免单人数共计:" + _免单人数);
}
}
}