1 代码讲解
1.1 抽象工厂优点
1.2 抽象工厂缺点
1.3 为何有产品族的业务场景宜用抽象工厂设计模式?而不是工厂设计模式?
2 代码演练
2.1 抽象工厂代码演练
1 代码讲解
1.1 抽象工厂优点
a 应用层代码不和具体的产品发生依赖,只和具体的产品族工厂发生依赖关系,低耦合,高内聚。
b 从具体的产品工厂取出来的肯定是同一产品族,开发的时候逻辑清晰。(ab其实可以归为1点)
c 对于产品族来说,符合开闭原则,增加新的产品族的时候,对扩展开放
1.2 抽象工厂缺点
a 增加新的产品时候,不符合开闭原则,工作量变大(解决方案:针对产品增加时间跨度比较大的业务场景,使用抽象方法比较好)
1.3 为何有产品族的业务场景宜用抽象工厂设计模式?而不是工厂设计模式?
如果使用工厂设计模式,可能会因为工行类太多而产生类爆炸的现象。
2 代码演练
2.1 抽象工厂代码演练
业务场景:
现在慕课网需要对不同的课程进行区分(产品族),java课程有java视频和java手记两种产品,net课程有net视频和net手机两种产品。
类图:
测试类:
package com.geely.design.pattern.creational.abstractfactory; public class TestAbstractFactory { /** * 制作java类的产品族,制作net类的产品族 * @param args */ public static void main(String [] args){ CourseFactory courseFactory1 = new JavaCourseFactory(); Video video1 = courseFactory1.getVideo(); Article article1 = courseFactory1.getArticle(); video1.produce(); article1.produce(); CourseFactory courseFactory2 = new NetCourseFactory(); Video video2 = courseFactory2.getVideo(); Article article2 = courseFactory2.getArticle(); video2.produce(); article2.produce(); } }
课程工厂(产品族工厂):
package com.geely.design.pattern.creational.abstractfactory; public interface CourseFactory { Video getVideo(); Article getArticle(); }
java课程工厂:
package com.geely.design.pattern.creational.abstractfactory; public class JavaCourseFactory implements CourseFactory{ @Override public Video getVideo() { return new JavaVideo(); } @Override public Article getArticle() { return new JavaArticle(); } }
net课程工厂:
package com.geely.design.pattern.creational.abstractfactory; public class NetVideo implements Video{ @Override public void produce() { System.out.println("开始只做点net视频!"); } }
video类:
package com.geely.design.pattern.creational.abstractfactory; public interface Video { void produce(); }
article类:
package com.geely.design.pattern.creational.abstractfactory; public interface Article { void produce(); }
javavideo类:
package com.geely.design.pattern.creational.abstractfactory; public class JavaVideo implements Video{ @Override public void produce() { System.out.println("开始制作java视频"); } }
javaarticle类:
package com.geely.design.pattern.creational.abstractfactory; public class JavaArticle implements Article{ @Override public void produce() { System.out.println("开始录入java笔记!"); } }
netvideo类:
package com.geely.design.pattern.creational.abstractfactory; public class NetCourseFactory implements CourseFactory{ @Override public Video getVideo() { return new NetVideo(); } @Override public Article getArticle() { return new NetArticle(); } }
netarticle类:
package com.geely.design.pattern.creational.abstractfactory; public class NetArticle implements Article{ @Override public void produce() { System.out.println("开始制做点net笔记!"); } }
打印日志:
"C:\Program Files\Java\jdk1.6.0_43\bin\java.exe" "-javaagent:D:\java\devolopKit\idea\anZh\IntelliJ IDEA Community Edition 2018.1.4\lib\idea_rt.jar=37199:D:\java\devolopKit\idea\anZh\IntelliJ IDEA Community Edition 2018.1.4\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.6.0_43\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.6.0_43\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.6.0_43\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.6.0_43\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.6.0_43\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.6.0_43\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.6.0_43\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.6.0_43\jre\lib\jce.jar;C:\Program Files\Java\jdk1.6.0_43\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.6.0_43\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.6.0_43\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.6.0_43\jre\lib\resources.jar;C:\Program Files\Java\jdk1.6.0_43\jre\lib\rt.jar;F:\xiangmu3\Xin\Idea\out\production\design_pattern" com.geely.design.pattern.creational.abstractfactory.TestAbstractFactory
开始制作java视频
开始录入java笔记!
开始只做点net视频!
开始制做点net笔记!
Process finished with exit code 0
诸葛