Back to list
Views:   8.5K
Replies:  1
Archived

Is the Factory Pattern correct for my message system?

Hi, 

Just started to read the amazing books in the Design Pattern Framework 4.5. However, I am a bit confused.

Say I have a messaging system which can send each message using different channel.
Is it a mistake to define Factory class -> Create function based on Enum which due to value return the right channel?
Since the creation is dynamic at run-time as I understand a Factory pattern doesn't match here...

Any suggestions?

Here is some code: 
public class Channel
{
  public abstract void SendMessage();
}


public class EmailChannel : Channel
{
   public void SendMessage()
   {

   }
}


public class SmsChannel : Channel
{
   public void SendMessage()
   {

   }
}



public class MessageSenderfactory
{
    public static Channel CreateChannel(ChannelType ct)
    {
       switch(ct)
       {
         case Sms: 
            return new ChannelSms();
         case Email:
            return new EmailChannel();
       }
    }
}


public Enum ChannelType
{
  Sms = 1,
  Email
}

 
Amit Malina, Sep 13, 2013
Reply 1
Hello Amit: 

This seems like a fine implementation of the Factory Method design pattern.
It allows each message channel to implement its own SendMessage implementation.
Furthermore, you can easily add a third message channel with little effort. 

Hope this helps.
Jack

Jack Poorte, Sep 13, 2013


Stay Inspired!
Join other developers and designers who have already signed up for our mailing list.
Terms     Privacy     Licensing       EULA       Sitemap      
© Data & Object Factory, LLC.
Made with    in Austin, Texas.      Vsn 1.2.2