Album Cover

Design Patterns Vol. 1 Java

Close Preview
Have you ever written a Hello World program?
Even if you have, you've likely never seen one like the patterns-happy version below.
It was written years ago by a programmer named Jason Tiscioni, who posted it to Slashdot.
Want to experience the dread of patterns? Take a look at Jason's fantastic caricature of what code can look like when patterns are overused.
Have I ever written a Hello World program?
Only about 2,366 times...

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);
interface MessageStrategy { public void sendMessage(); }
abstract class AbstractStrategyFactory { public abstract MessageStrategy createStrategy(MessageBody mb); }
class MessageBody { Object payload;
public Object getPayload() { return payload; }
public void configure(Object obj) { payload = obj; }
public void send(MessageStrategy ms) { ms.sendMessage(); } }
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(obj); } }; } }
| Tweet...
Page rating: | Rate this Page