Created
March 15, 2013 01:25
-
-
Save shishi/5166813 to your computer and use it in GitHub Desktop.
pattern happy?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface MessageStrategy { | |
public void sendMessage(); | |
} | |
public abstract class AbstractStrategyFactory { | |
public abstract MessageStrategy createStrategy(MessageBody mb); | |
} | |
public class MessageBody { | |
Object payload; | |
public Object getPayload() { | |
return payload; | |
} | |
public void configure(Object obj) { | |
payload = obj; | |
} | |
public void send(MessageStrategy ms) { | |
ms.sendMessage(); | |
} | |
} | |
public class DefaultFactory extends AbstractStrategyFactory { | |
private DefaultFactory() {;} | |
static DefaultFactory instance; | |
public static AbstractStrategyFactory getInstance() { | |
if (instance==null) instance = new DefaultFactory(); | |
return instance; | |
} | |
public MessageStrategy createStrategy(final MessageBody mb) { | |
return new MessageStrategy() { | |
MessageBody body = mb; | |
public void sendMessage() { | |
Object obj = body.getPayload(); | |
System.out.println((String)obj); | |
} | |
}; | |
} | |
} | |
public class HelloWorld { | |
public static void main(String[] args) { | |
MessageBody mb = new MessageBody(); | |
mb.configure("Hello World!"); | |
AbstractStrategyFactory asf = DefaultFactory.getInstance(); | |
MessageStrategy strategy = asf.createStrategy(mb); | |
mb.send(strategy); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment