代码改变世界

适配器模式

2011-05-18 20:49  littlelion  阅读(243)  评论(0编辑  收藏  举报
刚刚接触适配器模式时,对于类适配和对象适配理解的很不好,之前写了一个对象适配的程序也没有运行成功,昨天又尝试写了一下,这回运行成功了
View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication104
{
public class MobileStorageEquipment
{
public void connect()
{
Console.WriteLine(
"The equipment can connect with the computer");
}
}

public interface IEquipmentInterface
{
void read();
void write();
}


//now there is a new equipment which contains the method :newread() and newwrite()
//it must introduce a adapter
public interface Inewfunction
{
void newread();
void newwrite();
}
//the adapter
public class oneadapter
{
private Inewfunction _newUSBDriver;
public Inewfunction newUSBDriver
{
get
{
return this._newUSBDriver;

}
set
{
this._newUSBDriver = value;
}
}
public oneadapter()
{
}
public oneadapter(Inewfunction newusbdriver)
{
this._newUSBDriver = newusbdriver ;
}
public void newreaddate()
{
this._newUSBDriver.newread();
}
public void newwritedata()
{
this._newUSBDriver.newwrite();
}



}

//mp3player
public class mp3player : MobileStorageEquipment,IEquipmentInterface
{

public void read()
{
Console.WriteLine(
"reading from the computer");
Console.WriteLine(
"reading finished");
}
public void write()
{
Console.WriteLine(
"writing to the mp3player");
Console.WriteLine(
"writing finished");
}
}

//flashdisk
public class flashdisk : MobileStorageEquipment,IEquipmentInterface
{

public void read()
{
Console.WriteLine(
"reading from the computer");
Console.WriteLine(
"reading finished");
}
public void write()
{
Console.WriteLine(
"writing to the flashdisk");
Console.WriteLine(
"writing finished");
}
}

//Mobileharddisk
public class Mobileharddisk : MobileStorageEquipment,IEquipmentInterface
{

public void read()
{
Console.WriteLine(
"reading from the computer");
Console.WriteLine(
"reading finished");
}
public void write()
{
Console.WriteLine(
"writing to the Mobileharddisk");
Console.WriteLine(
"writing finished");
}
}


//the new equipment
public class newequipment : MobileStorageEquipment,Inewfunction
{
public void newread()
{
Console.WriteLine(
"the new equipment : reading from the computer ");
}
public void newwrite()
{
Console.WriteLine(
"the new equipment : writing to the new equioment");
}

}

public class computer
{
private IEquipmentInterface _USBDriver;
public IEquipmentInterface USBDriver
{
get
{
return this._USBDriver;
}
set
{
this._USBDriver = value;
}
}

public computer()
{
}
public computer(IEquipmentInterface usbdriver)
{
this.USBDriver = usbdriver;
}

public void readdata()
{
this._USBDriver.read();
}
public void writedata()
{
this._USBDriver.write();
}
}
class Program
{
static void Main(string[] args)
{
computer mycomputer
= new computer();
mp3player mymp3player
= new mp3player();
mycomputer.USBDriver
= mymp3player;
Console.WriteLine(
"test the mp3player");
mymp3player.connect();
Console.WriteLine();
Console.WriteLine(
"now insert the mp3player into the computer to copy some music from it\n");
mycomputer.writedata();
Console.WriteLine();
Console.WriteLine(
"now insert the mp3player into the computer\ncopy some files to the computer\n");
mycomputer.readdata();
Console.WriteLine();
Console.WriteLine(
"now test the new equipment\n");
newequipment mynewequipment
= new newequipment();
oneadapter myadapter
= new oneadapter();
myadapter.newUSBDriver
= mynewequipment;
myadapter.newreaddate();
myadapter.newwritedata();

}
}
}
对象适配的模式符合了面向对象的基本原则:多聚合,少继承,低耦合,高内聚。