Of course we all know Java is renowned for lending itself well to
over-engineering and pattern abuse. This little clip is amazing!
Without comments it's still a little under 100 lines of code! To top it
off, it's not even thread safe they tell me.
If I got paid by the lines of code I'd switch to Java for sure. :)
/**
* This program is an elaborate joke about the strucuture of the Java
* programming language. Technically you'll have to put all the
* public interfaces and classes in their own file to get it to
* compile. The actual code came from a slashdot post, comments were
* later added by ookabooka.
*
* Originally Copyright 2002 MillionthMonkey.
*
* Ridiculously verbose and mostly useless comments (AKA good
* commenting) added by ookabooka Copyright 2007.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an "AS IS"
* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* TODO:
* Add some try/catches and a plethora of exceptions to further insult
* Java.
*
* @author ookabooka
* @version 2.41.54b_2-rc4
* @see http://ask.slashdot.org/article.pl?sid=07/07/14/2011208
*/
/**
* This interface simply houses one method (not to be confused with "function"
* from other languages like C++) without parameters and like everything in this
* program is definately not needed.
*/
public interface MessageStrategy {
/**
* Sends a message
*
* @return
*/
public void sendMessage();
}// MessageStrategy
/**
* The AbstractStrategyFactory will be used to as a template for
* StrategyFactories to take a MessageBody and create a useful Strategy whos
* parameters is defined by the MessageBody.
*
*/
public abstract class AbstractStrategyFactory {
/**
* Abstract method that creates the MessageStrategy. Any classes that
* inherit this abstract class should use this method to create their
* MessageStrategies.
*
* @param mb
* MessageBody object to initialize the MessageStrategy
*/
public abstract MessageStrategy createStrategy(MessageBody mb);
}// AbstractStrategyFactory
public class MessageBody {
Object payload;// This is the data that this class will be handling
/**
* Simple "getter" method because it's a lot cooler than accessing a
public
* variable.
*
* @return the data that this class is handling.
*/
public Object getPayload() {
return payload;
}// getPayload
/**
* Sets the payload for this TODO: Change to setPayload because that is
the
* common convention.
*
* @param obj
* the object that you want this MessageBody to handle.
*/
public void configure(Object obj) {
payload = obj;
}// configure
/**
* Runs the sendMessage method on the MessageStrategy. Why you don't
just
* call it directly instead of using this method I will never know.
*
* @param ms
* the MessageStrategy you want to call sendMessage() on.
*/
public void send(MessageStrategy ms) {
ms.sendMessage();
}// send
}// MessageBody
/**
* This class inherits from AbstractStrategyFactory and will be doing the bulk
* of the work to print the "Hello World" string.
*
*
*/
public class DefaultFactory extends AbstractStrategyFactory {
/**
* Default constructor, this is completely and totally not needed at
all as
* it does absolutely nothing and takes no parameters.
*
*/
private DefaultFactory() {
}// DefaultFactory
static DefaultFactory instance;// an instance of the factory
/**
* This method returns the instance of DefaultFactory, creating a new
one if
* there isn't one initialized already.
*
* @return DefaultFactory object ready for use.
*/
public static AbstractStrategyFactory getInstance() {
if (null == instance)// If we don't have an instance
instance = new DefaultFactory();// Initialize one
return instance;// return the instance to the DefaultFactory
}// getInstance()
/**
* This creates the relevant MessageStrategy defined by the passed
* MessageBody and defines it's sendMessage() method. I didn't even
know you
* could define functions like this. Obviously as a factory this is the
most
* useful and relevant function in this class.
*
* @param the
* MessageBody to you want the strategy to work with
* @return the MessageStrategy associated with the passed MessageBody
*/
public MessageStrategy createStrategy(final MessageBody mb) {
/*
* This is actually really neato here, the constructor is
actually
* defined right here as it's created.
*/
return new MessageStrategy() {
MessageBody body = mb;// set the body to the parameter
/**
* This simply gets MessageBody's payload and prints it
out
*/
public void sendMessage() {
Object obj = body.getPayload();// get the
payload
System.out.println(obj.toString());// finally a
println
// TODO: Change to
System.out.println(body.getPayload());
}// sendMessage()
};// MessageStrategy()
}// createStrategy
}// DefaultFactory
/**
* The main class of the program, this contains the main method and initializes
* the Factory and MessageBody and then proceeds indirectly print out the
* message by calling the MessageBody.send method with the "Hello World"
* MessageBody object.
*
*/
public class HelloWorld {
/**
* The main method, this is what gets run when the program is executed.
This
* is also all this class has in it.
*
* @param args
* completely ignored, here for compliance reasons
*/
public static void main(String[] args) {
MessageBody mb = new MessageBody();// initilize MessageBody
object
mb.configure("Hello World!");// set the body to "Hello World"
//get an instance of DefaultFactory
AbstractStrategyFactory asf = DefaultFactory.getInstance();
//initialize a strategy associated with the MessageBody
MessageStrategy strategy = asf.createStrategy(mb);
//Have the message body send using the passed strategy
mb.send(strategy);
}// main
}// HelloWorld
/*
PLUG: http://plug.org, #utah on irc.freenode.net
Unsubscribe: http://plug.org/mailman/options/plug
Don't fear the penguin.
*/