Hello world!

The following demonstrates the use  of the OverkillPattern:

Hello World
  1. public interface MessageStrategy
  2. {
  3.    public void sendMessage();
  4. }
  5.  
  6. public abstract class AbstractStrategyFactory
  7. {
  8.    public abstract MessageStrategy createStrategy(MessageBody mb);
  9. }
  10.  
  11. public class MessageBody
  12. {
  13.    Object payload;
  14.  
  15.    public Object getPayload()
  16.    {
  17.       return payload;
  18.    }
  19.  
  20.    public void configure(Object obj)
  21.    {
  22.       payload = obj;
  23.    }
  24.  
  25.    public void send(MessageStrategy ms)
  26.    {
  27.       ms.sendMessage();
  28.    }
  29. }
  30.  
  31. public class DefaultFactory extends AbstractStrategyFactory
  32. {
  33.    private DefaultFactory()
  34.    {}
  35.  
  36.    static DefaultFactory instance;
  37.  
  38.    public static AbstractStrategyFactory getInstance()
  39.    {
  40.       if (null==instance) instance = new DefaultFactory();
  41.       return instance;
  42.    }
  43.  
  44.    public MessageStrategy createStrategy(final MessageBody mb)
  45.    {
  46.       return new MessageStrategy()
  47.       {
  48.          MessageBody body = mb;
  49.          public void sendMessage()
  50.          {
  51.             Object obj = body.getPayload();
  52.             System.out.println((String)obj);
  53.          }
  54.       };
  55.    }
  56. }
  57.  
  58. public class HelloWorld
  59. {
  60.    public static void main(String[] args)
  61.    {
  62.       MessageBody mb = new MessageBody();
  63.       mb.configure("Hello World!");
  64.  
  65.       AbstractStrategyFactory asf = DefaultFactory.getInstance();
  66.       MessageStrategy strategy = asf.createStrategy(mb);
  67.  
  68.       mb.send(strategy);
  69.    }
  70. }

About these ads

About Michael Brockman
SharePoint (2010, MOSS 2007) and .NET developer.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: