public class Ring : Decorator
{
public override void Show()
{
Console.WriteLine("鈴聲功能 ");
base.Show();
}
}
public class Office : Decorator
{
public override void Show()
{
Console.WriteLine("Office拓展功能 ");
base.Show();
}
}
public class Video_Call : Decorator
{
public override void Show()
{
Console.WriteLine("視頻電話功能 ");
base.Show();
}
}
public class Handwriting : Decorator
{
public override void Show()
{
Console.WriteLine("手寫功能 ");
base.Show();
}
} public class Surfing : Decorator
{
public override void Show()
{
Console.WriteLine("上網功能 ");
base.Show();
}
}
public class QQ : Decorator
{
public override void Show()
{
Console.WriteLine("QQ功能 ");
base.Show();
}
}
public class Fetion : Decorator
{
public override void Show()
{
Console.WriteLine("飛信功能 ");
base.Show();
}
}
public class Wolf : Decorator
{
public override void Show()
{
Console.WriteLine("狼牙功能 ");
base.Show();
}
}
客戶端代碼如下:
static void Main(string[] args)
{
Mobile mobile = new Mobile("Hoogle");
Console.WriteLine("\n第一款全能裝飾方式:");
Game game = new Game();
Touch touch = new Touch();
Photo photo = new Photo();
Memory memory = new Memory();
MP3 mp3 = new MP3();
Recording recording = new Recording();
Ring ring = new Ring();
Office office = new Office();
Video_Call video_call = new Video_Call();
Handwriting handwriting = new Handwriting();
Surfing surfing = new Surfing();
QQ qq = new QQ();
Fetion fetion = new Fetion();
Wolf wolf = new Wolf();
game.expand(mobile);
touch.expand(game);
photo.expand(touch);
memory.expand(photo);
mp3.expand(memory);
recording.expand(mp3);
ring.expand(recording);
office.expand(ring);
video_call.expand(office);
handwriting.expand(video_call);
surfing.expand(handwriting);
qq.expand(surfing);
fetion.expand(qq);
wolf.expand(fetion);
wolf.Show();
Console.WriteLine("\n另一種裝飾方式:");
Ring rings = new Ring();
Office offices = new Office();
Video_Call video_calls = new Video_Call();
Handwriting handwritings = new Handwriting();
Surfing surfings = new Surfing();
QQ qqs = new QQ();
Fetion fetions = new Fetion();
Wolf wolfs = new Wolf();
rings.expand(recording);
offices.expand(ring);
video_calls.expand(office);
handwritings.expand(video_call);
surfings.expand(handwriting);
qqs.expand(surfing);
fetions.expand(qq);
wolfs.expand(fetion);
wolfs.Show();
Console.Read();
}
顯示結果:
第一款全能裝飾方式:
遊戲功能 觸屏功能 照相功能 內存卡擴展功能 MP3功能 錄音功能 鈴聲功能 Office拓展功能 視頻電話功能 手寫功能 上網功能 QQ功能 飛信功能 狼牙功能
另一種裝飾方式:
錄音功能 鈴聲功能 Office拓展功能 視頻電話功能 手寫功能 上網功能 QQ功能 飛信功能 狼牙功能
以上就是我用裝飾模式設計手機的全過程,其中難免存在不足或漏洞,歡迎大家提出自己的意見和建議,謝謝!
曾祥瑚
2010.06.28 15:06