and only one, clear, well-defined job. • Break down your code into smaller, reusable functions. • Keep functions focused on a single task. • Use comments wisely (JavaDoc, multi-line, single-line) • DRY • KIS 5
public class Driver { public static void main(String[] arg) { Player player = new Player(); GameEngine game = new GameEngine(); game.ready(); do { player.move(); game.move(); } while (!game.isOver()); game.bye(); } } GameEngine GameBoard Main Or Driver
relies on) are provided externally rather than created internally by the object itself. • Constructor injection: Dependencies are passed via the class constructor. • Setter injection: Dependencies are provided through setter methods. • Make components more accessible to swap or extend without modifying the dependent class. 10
main(String[] arg) { View view = new View (); Player player = new Player(view); Game game = new Game(view); game.ready(); do { player.move(); game.move(); } while (!game.isOver()); game.bye(); } }
main(String[] arg) { View view = new View (); Player player = new Player(view); Game game = new Game(view); game.ready(); do { player.move(); game.move(); } while (!game.isOver()); game.bye(); } } public class View { public void print(String s) { System.out.println(s); } } public class Game { View myView; public Game(View v) { myView = v; } public void ready() { myView.print (“Welcome!”); } // more code … }
[email protected] Winter 2025 Copyright. These slides can only be used as study material for the class CSC305 at Cal Poly. They cannot be distributed or used for another purpose.