I am study factory method pattern and after reader in some places i did the following implementation. If in the future there is a need to add new types of boards, every time I have to change the BoardFactory class and the enum? Is this the correct approach to implement factory method?
public interface Board { }
public class MotherBoard implements Board { }
public class VideoBoard implements Board { }
public class AudioBoard implements Board { }
public enum BoardType {
VIDEO,
AUDIO,
MOTHER_BOARD
}
public class BoardFactory {
private BoardFactory() { }
public static Board createBoard(BoardType boardType) {
switch (boardType) {
case VIDEO:
return new VideoBoard();
case AUDIO:
return new AudioBoard();
default:
return new MotherBoard();
}
}
}
public class Main {
public static void main(String[] args) {
Board b1 = BoardFactory.createBoard(BoardType.VIDEO);
Board b2 = BoardFactory.createBoard(BoardType.AUDIO);
Board b3 = BoardFactory.createBoard(BoardType.MOTHER_BOARD);
}
}