工厂设计模式,顾名思义,就是用来生产对象的,在java中,万物皆对象,这些对象都需要创建,如果创建的时候直接new该对象,就会对该对象耦合严重,假如我们要更换对象,所有new对象的地方都需要修改一遍,这显然违背了软件设计的开闭原则,如果我们使用工厂来生产对象,我们就只和工厂打交道就可以了,彻底和对象解耦,如果要更换对象,直接在工厂里更换该对象即可,达到了与对象解耦的目的;所以说,工厂模式最大的优点就是:解耦
抽象工厂的实现抽象工厂可以用来生产一组相同,有相对关系的产品;重点在于一组,一批,一系列;举个例子,假如生产小米手机,小米手机有很多系列,小米note、红米note等;假如小米note生产需要的配件有825的处理器,6英寸屏
幕,而红米只需要650的处理器和5寸的屏幕就可以了;用抽象工厂来实现:
public interface Cpu { void run(); }
public class Cpu650 implements Cpu{ @Override public void run() { System.out.println("650cpu"); } }
public class Cpu865 implements Cpu{ @Override public void run() { System.out.println("650cpu"); } }手机屏幕尺寸组件
public interface Screen { void size(); }
public class Screen5 implements Screen{ @Override public void size() { System.out.println("5寸的屏幕"); } }
public class Screen6 implements Screen{ @Override public void size() { System.out.println("6寸的屏幕"); } }生产手机抽象工厂
public interface PhoneFactory { Cpu getCpu(); Screen getScreen(); }生产手机的具体工厂
public class HongMiFactory implements PhoneFactory{ @Override public Cpu getCpu() { return new Cpu650(); } @Override public Screen getScreen() { return new Screen5(); } }
public class XiaoMiFactory implements PhoneFactory{ @Override public Cpu getCpu() { return new Cpu865(); } @Override public Screen getScreen() { return new Screen6(); } }生产手机测试
public class ProductPhone { public static void main(String[] args) { HongMiFactory hongMiFactory = new HongMiFactory(); Cpu cpu = hongMiFactory.getCpu(); Screen screen = hongMiFactory.getScreen(); cpu.run(); screen.size(); } }结果
/Library/Java/JavaVirtualMachines/zulu-11.jdk/Contents/Home/bin/java -javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=51074:/Applications/IntelliJ IDEA.app/Contents/bin -Dfile.encoding=UTF-8 -classpath /Users/tao/IdeaProjects/stadu_all/pattern/target/classes:/Users/tao/maven/repository/org/apache/camel/camel-core/3.13.0/camel-core-3.13.0.jar:/Users/tao/maven/repository/org/apache/camel/camel-core-engine/3.13.0/camel-core-engine-3.13.0.jar:/Users/tao/maven/repository/org/apache/camel/camel-base-engine/3.13.0/camel-base-engine-3.13.0.jar:/Users/tao/maven/repository/org/apache/camel/camel-core-reifier/3.13.0/camel-core-reifier-3.13.0.jar:/Users/tao/maven/repository/org/apache/camel/camel-core-processor/3.13.0/camel-core-processor-3.13.0.jar:/Users/tao/maven/repository/org/apache/camel/camel-core-languages/3.13.0/camel-core-languages-3.13.0.jar:/Users/tao/maven/repository/org/apache/camel/camel-core-model/3.13.0/camel-core-model-3.13.0.jar:/Users/tao/maven/repository/org/apache/camel/camel-bean/3.13.0/camel-bean-3.13.0.jar:/Users/tao/maven/repository/org/apache/camel/camel-browse/3.13.0/camel-browse-3.13.0.jar:/Users/tao/maven/repository/org/apache/camel/camel-cluster/3.13.0/camel-cluster-3.13.0.jar:/Users/tao/maven/repository/org/apache/camel/camel-controlbus/3.13.0/camel-controlbus-3.13.0.jar:/Users/tao/maven/repository/org/apache/camel/camel-dataformat/3.13.0/camel-dataformat-3.13.0.jar:/Users/tao/maven/repository/org/apache/camel/camel-dataset/3.13.0/camel-dataset-3.13.0.jar:/Users/tao/maven/repository/org/apache/camel/camel-direct/3.13.0/camel-direct-3.13.0.jar:/Users/tao/maven/repository/org/apache/camel/camel-directvm/3.13.0/camel-directvm-3.13.0.jar:/Users/tao/maven/repository/org/apache/camel/camel-file/3.13.0/camel-file-3.13.0.jar:/Users/tao/maven/repository/org/apache/camel/camel-health/3.13.0/camel-health-3.13.0.jar:/Users/tao/maven/repository/org/apache/camel/camel-language/3.13.0/camel-language-3.13.0.jar:/Users/tao/maven/repository/org/apache/camel/camel-log/3.13.0/camel-log-3.13.0.jar:/Users/tao/maven/repository/org/apache/camel/camel-mock/3.13.0/camel-mock-3.13.0.jar:/Users/tao/maven/repository/org/apache/camel/camel-ref/3.13.0/camel-ref-3.13.0.jar:/Users/tao/maven/repository/org/apache/camel/camel-rest/3.13.0/camel-rest-3.13.0.jar:/Users/tao/maven/repository/org/apache/camel/camel-tooling-model/3.13.0/camel-tooling-model-3.13.0.jar:/Users/tao/maven/repository/org/apache/camel/camel-util-json/3.13.0/camel-util-json-3.13.0.jar:/Users/tao/maven/repository/org/apache/camel/camel-saga/3.13.0/camel-saga-3.13.0.jar:/Users/tao/maven/repository/org/apache/camel/camel-scheduler/3.13.0/camel-scheduler-3.13.0.jar:/Users/tao/maven/repository/org/apache/camel/camel-seda/3.13.0/camel-seda-3.13.0.jar:/Users/tao/maven/repository/org/apache/camel/camel-stub/3.13.0/camel-stub-3.13.0.jar:/Users/tao/maven/repository/org/apache/camel/camel-timer/3.13.0/camel-timer-3.13.0.jar:/Users/tao/maven/repository/org/apache/camel/camel-validator/3.13.0/camel-validator-3.13.0.jar:/Users/tao/maven/repository/org/apache/camel/camel-vm/3.13.0/camel-vm-3.13.0.jar:/Users/tao/maven/repository/org/apache/camel/camel-xpath/3.13.0/camel-xpath-3.13.0.jar:/Users/tao/maven/repository/org/apache/camel/camel-xslt/3.13.0/camel-xslt-3.13.0.jar:/Users/tao/maven/repository/org/apache/camel/camel-xml-jaxb/3.13.0/camel-xml-jaxb-3.13.0.jar:/Users/tao/maven/repository/jakarta/xml/bind/jakarta.xml.bind-api/2.3.3/jakarta.xml.bind-api-2.3.3.jar:/Users/tao/maven/repository/jakarta/activation/jakarta.activation-api/1.2.2/jakarta.activation-api-1.2.2.jar:/Users/tao/maven/repository/com/sun/xml/bind/jaxb-core/2.3.0/jaxb-core-2.3.0.jar:/Users/tao/maven/repository/com/sun/xml/bind/jaxb-impl/2.3.3/jaxb-impl-2.3.3.jar:/Users/tao/maven/repository/com/sun/activation/jakarta.activation/1.2.2/jakarta.activation-1.2.2.jar:/Users/tao/maven/repository/org/apache/camel/camel-xml-jaxp/3.13.0/camel-xml-jaxp-3.13.0.jar:/Users/tao/maven/repository/org/apache/camel/camel-xml-io-util/3.13.0/camel-xml-io-util-3.13.0.jar:/Users/tao/maven/repository/org/slf4j/slf4j-api/1.7.30/slf4j-api-1.7.30.jar:/Users/tao/maven/repository/org/apache/camel/camel-main/3.13.0/camel-main-3.13.0.jar:/Users/tao/maven/repository/org/apache/camel/camel-api/3.13.0/camel-api-3.13.0.jar:/Users/tao/maven/repository/org/apache/camel/camel-base/3.13.0/camel-base-3.13.0.jar:/Users/tao/maven/repository/org/apache/camel/camel-management-api/3.13.0/camel-management-api-3.13.0.jar:/Users/tao/maven/repository/org/apache/camel/camel-support/3.13.0/camel-support-3.13.0.jar:/Users/tao/maven/repository/org/apache/camel/camel-util/3.13.0/camel-util-3.13.0.jar:/Users/tao/maven/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.13.3/log4j-slf4j-impl-2.13.3.jar:/Users/tao/maven/repository/org/apache/logging/log4j/log4j-api/2.13.3/log4j-api-2.13.3.jar:/Users/tao/maven/repository/org/apache/logging/log4j/log4j-core/2.13.3/log4j-core-2.13.3.jar com.example.factory.abstrcactfactory.ProductPhone 650cpu 5寸的屏幕 Process finished with exit code 0
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)