public abstract class Room {
abstract void connect(Room room);
}
public class MagicRoom extends Room {
public void connect(Room room) { /* ... */ }
}
public class OrdinaryRoom extends Room {
public void connect(Room room) { /* ... */ }
}
public abstract class MazeGame {
final List<Room> rooms = new ArrayList<>();
public MazeGame() {
var room1 = makeRoom();
var room2 = makeRoom();
room1.connect(room2);
rooms.add(room1);
rooms.add(room2);
}
abstract protected Room makeRoom();
}
public class MagicMazeGame extends MazeGame {
@Override
protected Room makeRoom() {
return new MagicRoom();
}
}
public class OrdinaryMazeGame extends MazeGame {
@Override
protected Room makeRoom() {
return new OrdinaryRoom();
}
}
// main
class Example {
public static void main(String[] arguments) {
var ordinaryGame = new OrdinaryMazeGame();
var magicGame = new MagicMazeGame();
}
}
interface Room {
connect: (Room) => void;
}
interface MazeGame {
rooms: Room[];
}
function makeMagicRoom(): Room {
return { connect: () => { /* ... */ } };
}
function makeOrdinaryRoom(): Room {
return { connect: () => { /* ... */ } };
}
function makeMazeGame(roomMaker: () => Room): MazeGame {
const room1 = roomMaker();
const room2 = roomMaker();
return {
rooms: [room1, room2]
};
}
// main
const ordinaryGame = makeMazeGame(makeOrdinaryRoom);
const magicGame = makeMazeGame(makeMagicRoom);