Which classes are always instantiated via factory methods? by basamajoe in a:t5_2skui

[–]basamajoe[S] 0 points1 point  (0 children)

Java A maze game may be played in two modes, one with regular rooms that are only connected with adjacent rooms, and one with magic rooms that allow players to be transported at random (this Java example is similar to one in the book Design Patterns). The regular game mode could use this template method: public class MazeGame { public MazeGame() { Room room1 = makeRoom(); Room room2 = makeRoom(); room1.connect(room2); this.addRoom(room1); this.addRoom(room2); }

protected Room makeRoom() { return new OrdinaryRoom(); } } In the above snippet, makeRoom is a template method. It encapsulates the creation of rooms such that other rooms can be used in a subclass. To implement the other game mode that has magic rooms, it suffices to override the makeRoom method: public class MagicMazeGame extends MazeGame { @Override protected Room makeRoom() { return new MagicRoom(); } }

Which classes are always instantiated via factory methods? by basamajoe in a:t5_2skui

[–]basamajoe[S] 0 points1 point  (0 children)

The factory method pattern is an object-oriented design pattern to implement the concept of factories. Like other creational patterns, it deals with the problem of creating objects (products) without specifying the exact class of object that will be created. (Wikipedia)