当前位置:网站首页>06. Adapter mode
06. Adapter mode
2022-04-21 19:45:00 【zzyzxb】
namespace _06_Adapter
{
interface IStack // Customer desired interface
{
void Push(object item);
object Pop();
object Peek();
}
/// <summary>
/// Object adapter
/// </summary>
class AdapterObject : IStack // Fit object , Object adapter
{
ArrayList Adaptee; // An adapted object
public AdapterObject()
{
Adaptee = new ArrayList();
}
public void Push(object item)
{
Adaptee.Add(item);
}
public object Pop()
{
object obj = Adaptee[Adaptee.Count - 1];
Adaptee.RemoveAt(Adaptee.Count - 1);
return obj;
}
public object Peek()
{
return Adaptee[Adaptee.Count - 1];
}
}
/// <summary>
/// The class adapter
/// </summary>
class AdapterClass : ArrayList, IStack //ArrayList and IStack There is a public interface
{
public void Push(object item)
{
this.Add(item);
}
public object Pop()
{
object obj = this[this.Count - 1];
this.RemoveAt(this.Count - 1);
return obj;
}
public object Peek()
{
return this[this.Count - 1];
}
}
/// <summary>
/// Existing classes
/// </summary>
class ExistingClass
{
public void SpecificRequest1()
{
}
public void SpecificRequest2()
{
}
}
/// <summary>
/// The interface used by the new environment
/// </summary>
interface ITarget
{
void Request();
}
/// <summary>
/// Another system
/// </summary>
class MySystem
{
public void Process(ITarget target)
{
target.Request();
}
}
class Adapter : ITarget
{
ExistingClass adaptee;
public Adapter()
{
adaptee = new ExistingClass();
}
public void Request()
{
adaptee.SpecificRequest1();
adaptee.SpecificRequest2();
}
}
public class Employee
{
private int age = 0;
private string name;
public int Age
{
get {
return age; }
set {
age = value; }
}
}
class EmployeeSortAdapter : IComparer
{
//object obj1;
//object obj2;
//public EmployeeSortAdapter(object obj1, object obj2)
//{
// this.obj1 = obj1;
// this.obj2 = obj2;
//}
public int Compare(object obj1, object obj2)
{
if (obj1.GetType() != typeof(Employee) || obj2.GetType() != typeof(Employee))
{
throw new ArgumentException();
}
Employee e1 = (Employee)obj1;
Employee e2 = (Employee)obj2;
if (e1.Age == e2.Age)
{
return 0;
}
else if (e1.Age > e2.Age)
{
return 1;
}
else if (e1.Age < e2.Age)
{
return -1;
}
else
{
throw new Exception();
return -99;
}
}
}
internal class Program
{
static void Main(string[] args)
{
//MySystem ms = new MySystem();
//ms.Process(new Adapter());
Employee[] employees = new Employee[100];
int num = 1000;
for (int i = 0; i < 100; i++)
{
employees[i] = new Employee();
employees[i].Age = num;
num -= 1;
}
Array.Sort(employees, new EmployeeSortAdapter());
Console.ReadKey();
}
}
}
版权声明
本文为[zzyzxb]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211942043292.html
边栏推荐
- Today's sleep quality record is 83 points
- fiddler抓包PC微信小程序失败的解决方案
- Instructions of crystal Chem mouse glucose detection kit
- Overview of relational database and non relational database, introduction to redis, common commands and optimization
- 企业跨境电商平台服务解决方案,跨境电子商务贸易业务框架搭建运维
- js监测手机屏幕旋转(横屏还是竖屏)
- Linux Centos7 安装MySql8 (简单、实测可行)
- 80.(leaflet篇)leaflet调用geoserver发布的postgis数据图层
- Solutions informatiques pour les entreprises manufacturières haut de gamme, maintenance prédictive de l'équipement, des données et du système de la plate - forme de commerce électronique industriel
- 静态链接与动态链接
猜你喜欢
随机推荐
06.适配器模式
Learn MySQL performance tuning and make your database smooth
Building / Qt 5 and QT 6 compatibility using cmake
Force buckle - 53 Maximum subarray sum
若依集成actuator实现优雅关闭应用
Conan软件包管理器的Qt 6.2.4软件包
fiddler抓包PC微信小程序失败的解决方案
Frame rate, code rate, resolution and definition
Sword finger offer: [day 29 dynamic programming (difficult)] --- > n dice points
[Netty ]自己实现一个redis客户端难吗?
C# 将dll打包到程序中
Text to SQL learning and sorting (XVII) s2sql model
调试 ms 源代码
2023年南开大学税务专硕考研上岸前辈备考经验指导
2023年东南大学劳动卫生与环境卫生学考研上岸前辈备考经验指导
flink中checkpoint机制随笔
Leetcode0886. 可能的二分法(medium,二分图)
Rapid generation of latex from excel tables
nodejs笔记1
Solutions informatiques pour les entreprises manufacturières haut de gamme, maintenance prédictive de l'équipement, des données et du système de la plate - forme de commerce électronique industriel









