意圖
把一個類的接口變換成客戶端所期待的另一種接口
場景
假設網絡游戲的客戶端程序分兩部分
一開始的設計就是
此時可以考慮使用Adapter模式來適配這種接口的不匹配情況
using System
using System
namespace AdapterExample
{
class Program
{
static void Main(string[] args)
{
Lobby lobby = new Lobby();
lobby
lobby
}
}
interface IGame
{
void StartScene(string sceneName);
void EnterPlayer(string playerName);
}
class Lobby
{
private string sceneName;
public void CreateRoom(string sceneName)
{
this
}
public void StartGame()
{
IGame game = new GameAdapter();
game
game
}
}
class Game
{
public void LoadScene(string sceneName
{
if (token ==
Console
else
Console
}
public void EnterPlayer(int playerID)
{
Console
}
}
class GameAdapter : IGame
{
private Game game = new Game();
public void StartScene(string sceneName)
{
game
}
public void EnterPlayer(string playerName)
{
game
}
private int GetPlayerIDByPlayerName(string playerName)
{
return
}
}
}
[
From:http://tw.wingwit.com/Article/program/net/201311/15401.html