抽象工厂模式:是围绕一个超级工厂创建其他工厂。该超级工厂又称为其他工厂的工厂。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。在抽象工厂模式中,接口是负责创建一个相关对象的工厂,不需要显式指定它们的类。每个生成的工厂都能按照工厂模式提供对象。
使用情况:
1,系统不依赖于产品类实例如何被创建,组合和表达的细节
2,系统的产品有多于一个的产品族,而系统只消费其中某一族的产品
举例:微型计算机配件,这个系统所需要的产品族有两个,一个系列是PC系列,另一个系列是MAC系列。产品等级结构也有两个,一个是RAM,一个是CPU
public interface Cpu {
void run();
}
public class PcCpu implements Cpu {
public void run() {
System.out.println("pcCpu is running");
}
}
public class MacCpu implements Cpu{
public void run() {
System.out.println("MacCpu is running");
}
}
public interface Ram {
void run();
}
public class PcRam implements Ram {
public void run() {
System.out.println("pcRam is running");
}
}
public class MacRam implements Ram {
public void run() {
System.out.println("MacRam is running");
}
}
public interface AbstractFactory {
Cpu getCpu();
Ram getRam();
}
public class PcFactory implements AbstractFactory {
public Cpu getCpu() {
return new PcCpu();
}
public Ram getRam() {
return new PcRam();
}
}
public class MacFactory implements AbstractFactory {
public Cpu getCpu() {
return new MacCpu();
}
public Ram getRam() {
return new MacRam();
}
}
public class FactoryProducer {
public static AbstractFactory getFactory(String param){
if("pc".equalsIgnoreCase(param)){
return new PcFactory();
}else if("mac".equalsIgnoreCase(param)){
return new MacFactory();
}
System.out.println("没有这个系列");
return null;
}
}
public class FactoryDemo {
public static void main(String[] args) {
AbstractFactory pcFactory = FactoryProducer.getFactory("pc");
Cpu pcCpu = pcFactory.getCpu();
Ram pcRam = pcFactory.getRam();
pcCpu.run();
pcRam.run();
AbstractFactory macFactory = FactoryProducer.getFactory("mac");
Cpu macCpu = macFactory.getCpu();
Ram macRam = macFactory.getRam();
macCpu.run();
macRam.run();
}
}
验证输出
pcCpu is running
pcRam is running
MacCpu is running
MacRam is running
在抽象工厂模式中:
产品扩展有俩中,1增加产品族,比如增加服务器,2增加体系结构,比如增加一个显卡
增加产品族,在不改变产品等级结构的情况下,增加产品族就是意味着向每一个产品等级结构中增加一个或者多个新的具体产品角色,这时只需要向工厂等级结构中增加新的元素就可以了,具体的说,只需要增加新的具体工厂类就可以了,例如:
public class ServerFactory implements AbstractFactory {
public Cpu getCpu() {
return new ServerCpu();
}
public Ram getRam() {
return new ServerRam();
}
}
public class ServerCpu implements Cpu{
public void run() {
System.out.println("server cpu is running");
}
}
public class ServerRam implements Ram{
public void run() {
System.out.println("server ram is running");
}
}
public class FactoryProducer {
public static AbstractFactory getFactory(String param){
if("pc".equalsIgnoreCase(param)){
return new PcFactory();
}else if("mac".equalsIgnoreCase(param)){
return new MacFactory();
}else if("server".equalsIgnoreCase(param)){
return new ServerFactory();
}
System.out.println("没有这个系列");
return null;
}
}
public class FactoryDemo {
public static void main(String[] args) {
AbstractFactory pcFactory = FactoryProducer.getFactory("pc");
Cpu pcCpu = pcFactory.getCpu();
Ram pcRam = pcFactory.getRam();
pcCpu.run();
pcRam.run();
AbstractFactory macFactory = FactoryProducer.getFactory("mac");
Cpu macCpu = macFactory.getCpu();
Ram macRam = macFactory.getRam();
macCpu.run();
macRam.run();
AbstractFactory serverFactory = FactoryProducer.getFactory("server");
Cpu serverCpu = serverFactory.getCpu();
Ram serverRam = serverFactory.getRam();
serverCpu.run();
serverRam.run();
}
}
验证输出:
pcCpu is running
pcRam is running
MacCpu is running
MacRam is running
server cpu is running
server ram is running