1 Code Walkthrough
1.1 Unused simple factory pattern code
1.2 Simple factory pattern
1.3 bank using simple reflection mode
1 Code Walkthrough
1.1 Unused simple factory pattern code
Test categories:
Package com.geely.design.pattern.creational.simplefactory; public class the Test { / ** * NOTE: This is a counter-example, prior to the factory mode * no simple, requires new object. * @Param args * / public static void main (String [] args) { Connections Video Video = new new JavaVideo (); Video .printVideo (); } }
Parent Video:
package com.geely.design.pattern.creational.simplefactory; public abstract class Video { public abstract void printVideo(); }
Subclass javaVideo:
package com.geely.design.pattern.creational.simplefactory; public class JavaVideo extends Video{ @Override public void printVideo() { System.out.println("录制java视频"); } }
子 类 netVideo:
package com.geely.design.pattern.creational.simplefactory; public class NetVideo extends Video{ @Override public void printVideo() { System.out.println("录制.Net视频!"); } }
Print Results:
Video recording java
Process Finished with Exit code 0
1.2 Simple factory pattern
Test categories:
Package com.geely.design.pattern.creational.simplefactory; public class the Test { / ** * NOTE: This is a counter-example, prior to the factory mode * no simple, requires new object. * @Param args * / // public static void main (String [] args) { // Connections Video Video JavaVideo new new = (); // Video .printVideo (); // } / ** * Simple factory pattern 2 * here recalling the factory class parameter obtained directly factory subclass * @param args * / public static void main (String [] args) { VideoFactory videoFactory= new VideoFactory(); Video video = videoFactory.getVideo("net"); video .printVideo(); } }
Factory class:
package com.geely.design.pattern.creational.simplefactory; public class VideoFactory { private Video video = null; public Video getVideo(String param) { if("java".equals(param)){ return new JavaVideo(); }else if("net".equals(param)){ return new NetVideo(); }else{ return null; } } }
Parent (ibid.):
Subclass javaVideo :( ditto)
Subclass netVideo (ibid.):
Print log:
.Net video recording!
Finished with code Exit Process 0
1.3 bank using simple reflection mode