适配器模式,先占位,后面补充
using System;
using System.Collections.Generic;
namespace SimpleFactory
{
class Program
{
static void Main(string[] args)
{
原味汤粉 product = new 原味汤粉();
//使用奶茶包装方式来打包。原本奶茶杯是无法打包原味汤粉的,但经过适配器一配,居然就可以打了。
奶茶打包器 milkTea = new 奶茶打包器();
适配器 adapter1 = new 适配器(milkTea);
product.需要打包(adapter1);
//使用原味汤粉的打包方式来打包
原味汤粉打包器 soup = new 原味汤粉打包器();
product.需要打包(soup);
}
}
/// <summary>
/// 本店产品
/// </summary>
public class 原味汤粉
{
public void 需要打包(打包器 pack)
{
pack.打包();
}
}
public interface 打包器
{
void 打包();
}
/// <summary>
/// 本店产品
/// </summary>
public class 原味汤粉打包器 : 打包器
{
public void 打包()
{
Console.WriteLine("使用1000ml塑料碗打包原味汤粉");
}
}
/// <summary>
/// 其它店提供的奶茶服务,也有包装功能,但是这个第三方店,提供的方法名不叫打包,而是叫“包装”
/// </summary>
public class 奶茶打包器
{
public void 包装()
{
Console.WriteLine("使用700ml塑料杯打包奶茶");
}
}
/// <summary>
/// 现在要适配本店产品和其它店的打包服务
/// </summary>
public class 适配器 : 打包器
{
private 奶茶打包器 _adaptee;
public 适配器(奶茶打包器 milkTea)
{
this._adaptee = milkTea;
}
public void 打包()
{
this._adaptee.包装();
}
}
}