vendredi 18 novembre 2016

Bridge design pattern

I'm new with this of C# Bridge Pattern and today I do an exercise but I do it in a wrong way because I just was creating the message with MessageBox.Show and the values of my inputs, so now I want to know the correct way of doing this and I hope that you can help me, this is not for homework or anything, this is only to know the correct usage of this pattern. Here is my code:

Form1 Code:

private MessageController MessageController = new MessageController();

public Form1()
{
    InitializeComponent();
}

private void btnSendMessage_Click(object sender, EventArgs e)
{
    if (radSMS.Checked)
    {
        MessageController.SetMessageType(MessageType.SMS);
        //Here is where I do the MessageBox.Show(radSMS.text + txtTo.text + txtMessage.Text);
    }
    MessageController.SendMessage(txtTo.Text, txtMessage.Text);
}

MessageController Code:

public class MessageController
{
    private IMessage Message { get; set; }

    public void SetMessageType(MessageType messageType)
    {
        switch (messageType)
        {
            case MessageType.SMS:
                Message = new SMSMessage();
                break;
        }
    }

    public void SendMessage(string to, string message)
    {
        Message.SendMessage(to, message);
    }
}

IMessage Code

public interface IMessage
{
    void SendMessage(string to, string message);
}

MessageType Code:

public enum MessageType
{
    SMS
}

My question is how should this work? If you don't want to edit my code there is no problem, but I'm really interested on understand how this pattern works. Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire