惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Blog — PlanetScale
Blog — PlanetScale
J
Java Code Geeks
月光博客
月光博客
Engineering at Meta
Engineering at Meta
WordPress大学
WordPress大学
Jina AI
Jina AI
小众软件
小众软件
U
Unit 42
云风的 BLOG
云风的 BLOG
Stack Overflow Blog
Stack Overflow Blog
雷峰网
雷峰网
博客园 - Franky
Microsoft Security Blog
Microsoft Security Blog
罗磊的独立博客
宝玉的分享
宝玉的分享
B
Blog
C
Check Point Blog
爱范儿
爱范儿
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
量子位
阮一峰的网络日志
阮一峰的网络日志
Vercel News
Vercel News
酷 壳 – CoolShell
酷 壳 – CoolShell

博客园 - AlanPaoPao

23)Visitor 22)Template 21)Strategy 20)State 19)Observer 18)Memento 17)Mediator 16)Iterator 15)Interpreter 14)Command 13)Chain Of Responsibility 12)Proxy 11)Flyweight 10)Facade 09)Decorator 08)Composite 07)Bridge 05)Prototype 04)Factory Method
06)Adapter
AlanPaoPao · 2007-09-19 · via 博客园 - AlanPaoPao

    适配器模式的目的是: 将一个类的接口转换成客户希望的另外一个接口
    实例代码:

class EmployeeDatabank
{
  
public Int32 GetAge(String name)
  
{
    Int32 age 
= 0;
    
switch (name.ToLower())
    
{
      
case "paopao": age = 25break;
      
case "xiongxiong": age = 25break;
      
case "xiaoshu": age = 29break;
    }

    
return age;
  }

  
public String GetSequence(String name)
  
{
    String Sequence 
= "";
    
switch (name.ToLower())
    
{
      
case "paopao": Sequence = "FD28**"break;
      
case "xiongxiong": Sequence = "FD29**"break;
      
case "xiaoshu": Sequence = "FD17**"break;
    }

    
return Sequence;
  }

  
public Double GetStature(String name)
  
{
    Double stature 
= 0.0;
    
switch (name.ToLower())
    
{
      
case "paopao": stature = 178.5break;
      
case "xiongxiong": stature = 170.1break;
      
case "xiaoshu": stature = 179.5break;
    }

    
return stature;
  }

}

class Person
{
  
protected String name;
  
protected String sequence;
  
protected Double stature;
  
protected Int32 age;
  
public Person(String name)
  
{
    
this.name = name;
  }

  
public virtual void Show()
  
{
    Console.WriteLine(
"Person: {0} ------ ", name);
  }

}

class RichPerson : Person  //适配器
{
  
private EmployeeDatabank bank;
  
public RichPerson(String name)
    : 
base(name)
  
{
  }

  
public override void Show()
  
{
    bank 
= new EmployeeDatabank();
    sequence 
= bank.GetSequence(name);
    stature 
= bank.GetStature(name);
    age 
= bank.GetAge(name);
    
base.Show();
    Console.WriteLine(
" 工号: {0}", sequence);
    Console.WriteLine(
" 身高: {0}", stature);
    Console.WriteLine(
" 年龄: {0}", age);
  }

}

//测试
class MainApp
{
  
static void Main()
  
{
    Person person 
= new Person("person");
    person.Show();
    Person shu 
= new RichPerson("XiaoShu");
    shu.Show();
    Person alan 
= new RichPerson("PaoPao");
    alan.Show();
    Person xiong 
= new RichPerson("XiongXiong");
    xiong.Show();
    Console.Read();
  }

}