適用場景:前端
優勢:ide
缺點:spa
public abstract class Video { public abstract void produce(); }
public class JavaVideo extends Video { @Override public void produce() { System.out.println("錄製Java課程視頻"); } }
public class PythonVideo extends Video { @Override public void produce() { System.out.println("錄製Python課程視頻"); } }
public abstract class VideoFactory { public abstract Video getVideo(); }
public class JavaVideoFactory extends VideoFactory { @Override public Video getVideo() { return new JavaVideo(); } }
public class PythonVideoFactory extends VideoFactory { @Override public Video getVideo() { return new PythonVideo(); } }
public class Test { public static void main(String[] args) { VideoFactory videoFactory = new PythonVideoFactory(); Video video = videoFactory.getVideo(); video.produce(); } }
可擴展性:
增長一個前端視頻:code
public class FEVideo extends Video{ @Override public void produce() { System.out.println("錄製FE課程視頻"); } }
public class FEVideoFactory extends VideoFactory{ @Override public Video getVideo() { return new FEVideo(); } }
重點:videoFactory把建立具體某個視頻的職責交由子類來實現。這裏Video是產品,FEVideo、PythonVideo、JavaVideo是實際的產品,VideoFactory是建立者,而FEVideoFactory、PythonVideoFactory、JavaVideoFactory是實際的建立者視頻