using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Adaptor
{
///
/// 适配器模式是在不改动原来的功能的基础上增加新的功能。
/// 主要是新增接口,新增类继承新增接口,实现新增类方法
/// 的时候,也要实现原有类中的方法。
///
interface Iface
{
void Old();
}
class MyClass : Iface
{
public void Old()
{
Console.WriteLine(“这是一个旧方法”);
}
}
interface INewFace
{
void New();
}
class NewClass : INewFace
{
MyClass mc = new MyClass();
public void New()
{
mc.Old();
Console.WriteLine(“这是一个新方法”);
}
}
class Program
{
static void Main(string[] args)
{
NewClass nc = new NewClass();
nc.New();
Console.ReadKey();
}
}
}
本文深入探讨了适配器模式的实现方式,通过新增接口和类来在不改变原有功能的基础上增加新功能。示例代码展示了如何使用C#创建适配器,使现有类能够与新接口一起工作。
1153

被折叠的 条评论
为什么被折叠?



