Algorithm Design pattern Java