橋接模式(bridge結構模式)c#

來源:互聯網
上載者:User

標籤:blog   io   ar   使用   sp   for   on   2014   art   

橋接模式(bridge結構模式)c#簡單例子

在前面的玩家中每增加一個行為,就必須在每個玩家中都增加,通過橋接模式將行為提取出來了,減少變化

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace adapterpattern{    public partial class bridge : Form    {        public bridge()        {            InitializeComponent();        }        private void btnDisplay_Click(object sender, EventArgs e)        {            play p1 = new play1();            p1.setPlayAction(new move());            p1.run();            this.listBox1.Items.Add(p1.playstring);            play p2 = new play2();            p2.setPlayAction(new jump());            p2.run();            this.listBox1.Items.Add(p2.playstring);        }    }    //意圖(Intent)將抽象部分與實現部分分離,使它們都可以獨立地變化。    public abstract class play//抽象部分    {        public string playstring { get; set; }        protected playAction pa;        public void setPlayAction(playAction pa)//使用組合        {            this.pa = pa;        }        public abstract void action();//抽象部分變化        public void run()        {            pa.action();//執行實現部分            action();        }    }    public class play1 : play    {        public override void action()        {            playstring = "play1" + pa.actionstring;        }    }    public class play2 : play    {        public override void action()        {            playstring = "play2" + pa.actionstring;        }    }    public abstract class playAction//對實現部分進行抽象    {        public string actionstring;        public abstract void action();    }    public class move : playAction//實現玩家移動行為    {        public override void action()        {            actionstring = "move";        }    }    public class jump : playAction//實現玩家跳躍行為    {        public override void action()        {            actionstring = "jump";        }    }}


橋接模式(bridge結構模式)c#

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.