抽象工厂模式 Posted on 2021-02-25 | In 设计模式 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253public abstract class Article { public abstract void produce();}public abstract class Video { public abstract void produce();}//实现类public class JavaArticle extends Article{ @Override public void produce() { System.out.println("生产文章"); }}public class JavaVideo extends Video{ @Override public void produce() { System.out.println("生成video"); }}课程public interface CourseFactory { Video getVideo(); Article getArtcle();}public class MyFactory implements CourseFactory{ @Override public Video getVideo() { return new JavaVideo(); } @Override public Article getArtcle() { return new JavaArticle(); }}//主类public class Client { public static void main(String[] args) { MyFactory myFactory = new MyFactory(); Article artcle = myFactory.getArtcle(); Video video = myFactory.getVideo(); artcle.produce(); video.produce(); }} Donate comment here Donate WeChat Pay