r/GoodSoftware • • Aug 25 '19

In the Luan/Java webserver code, what is going on in the code used in the class Example.java in the method fancy() on lines 54-58? Is the instance of Handler being used like a pointer? Ive never seen an Object incremented like that. (code in comments)

public static void fancy() throws IOException {
	Map<String,Handler> map = new HashMap<String,Handler>();
	map.put( "/hello", new Example() );
	map.put( "/headers", new Headers() );
	map.put( "/params", new Params() );
	map.put( "/cookies", new Cookies() );
	Handler mapHandler = new MapHandler(map);
	FileHandler fileHandler = new FileHandler();
	Handler dirHandler = new DirHandler(fileHandler);
	Handler handler = new ListHandler( mapHandler, fileHandler, dirHandler );
	handler = new ContentTypeHandler(handler);
	handler = new SafeHandler(handler);
	handler = new LogHandler(handler);
	new Server(8080,handler).start();
}

I had no idea you could do that. Its about a 1000 times simpler than using an over-engineered factory or builder pattern or using some shit dependency injection library.

2 Upvotes

2 comments sorted by

1

u/A_Plagiarize_Zest Aug 25 '19

"I had no idea you could do that in java. Its about a 1000 times..."

1

u/fschmidt Aug 25 '19

Yes an object variable contains a pointer to an object. So I am just updating the pointer.

And yes, my coding style is a complete rejection of modern programming, so it is 1000 times simpler.