java工厂模式和抽象工厂_Java中的抽象工厂设计模式

java工厂模式和抽象工厂

Welcome to Abstract Factory Design Pattern in java example. Abstract Factory design pattern is one of the Creational patterns. Abstract Factory pattern is almost similar to Factory Pattern except the fact that its more like factory of factories.

欢迎使用Java示例中的抽象工厂设计模式。 抽象工厂设计模式是创新模式之一。 抽象工厂模式几乎类似于工厂模式,除了它更像工厂工厂。

抽象工厂 (Abstract Factory)

If you are familiar with factory design pattern in java, you will notice that we have a single Factory class. This factory class returns different subclasses based on the input provided and factory class uses if-else or switch statement to achieve this.

如果您熟悉java中的工厂设计模式 ,您会注意到我们只有一个Factory类。 该工厂类根据提供的输入返回不同的子类,并且工厂类使用if-else或switch语句来实现此目的。

In the Abstract Factory pattern, we get rid of if-else block and have a factory class for each sub-class. Then an Abstract Factory class that will return the sub-class based on the input factory class. At first, it seems confusing but once you see the implementation, it’s really easy to grasp and understand the minor difference between Factory and Abstract Factory pattern.

在Abstract Factory模式中,我们摆脱了if-else块,并为每个子类都有一个工厂类。 然后是一个抽象工厂类,它将基于输入工厂类返回子类。 乍一看似乎令人困惑,但是一旦您看到了实现,就很容易理解和理解Factory模式与Abstract Factory模式之间的细微差别。

Like our factory pattern post, we will use the same superclass and sub-classes.

就像我们的工厂模式文章一样,我们将使用相同的超类和子类。

抽象工厂设计模式的超类和子类 (Abstract Factory Design Pattern Super Class and Subclasses)

Computer.java

Computer.java

package com.journaldev.design.model;
 
public abstract class Computer {
     
    public abstract String getRAM();
    public abstract String getHDD();
    public abstract String getCPU();
     
    @Override
    public String toString(){
        return "RAM= "+this.getRAM()+", HDD="+this.getHDD()+", CPU="+this.getCPU();
    }
}

PC.java

PC.java

package com.journaldev.design.model;
 
public class PC extends Computer {
 
    private String ram;
    private String hdd;
    private String cpu;
     
    public PC(String ram, String hdd, String cpu){
        this.ram=ram;
        this.hdd=hdd;
        this.cpu=cpu;
    }
    @Override
    public String getRAM() {
        return this.ram;
    }
 
    @Override
    public String getHDD() {
        return this.hdd;
    }
 
    @Override
    public String getCPU() {
        return this.cpu;
    }
 
}

Server.java

Server.java

package com.journaldev.design.model;
 
 
public class Server extends Computer {
 
    private String ram;
    private String hdd;
    private String cpu;
     
    public Server(String ram, String hdd, String cpu){
        this.ram=ram;
        this.hdd=hdd;
        this.cpu=cpu;
    }
    @Override
    public String getRAM() {
        return this.ram;
    }
 
    @Override
    public String getHDD() {
        return this.hdd;
    }
 
    @Override
    public String getCPU() {
        return this.cpu;
    }
 
}

每个子类的工厂类 (Factory Class for Each subclass)

First of all we need to create a Abstract Factory interface or abstract class.

首先,我们需要创建一个Abstract Factory接口或抽象类

ComputerAbstractFactory.java

ComputerAbstractFactory.java

package com.journaldev.design.abstractfactory;

import com.journaldev.design.model.Computer;

public interface ComputerAbstractFactory {

	public Computer createComputer();

}

Notice that createComputer() method is returning an instance of super class Computer. Now our factory classes will implement this interface and return their respective sub-class.

请注意, createComputer()方法将返回超类Computer的实例。 现在,我们的工厂类将实现此接口并返回其各自的子类。

PCFactory.java

PCFactory.java

package com.journaldev.design.abstractfactory;

import com.journaldev.design.model.Computer;
import com.journaldev.design.model.PC;

public class PCFactory implements ComputerAbstractFactory {

	private String ram;
	private String hdd;
	private String cpu;
	
	public PCFactory(String ram, String hdd, String cpu){
		this.ram=ram;
		this.hdd=hdd;
		this.cpu=cpu;
	}
	@Override
	public Computer createComputer() {
		return new PC(ram,hdd,cpu);
	}

}

Similarly we will have a factory class for Server subclass.

同样,我们将为Server子类提供一个工厂类。

ServerFactory.java

ServerFactory.java

package com.journaldev.design.abstractfactory;

import com.journaldev.design.model.Computer;
import com.journaldev.design.model.Server;

public class ServerFactory implements ComputerAbstractFactory {

	private String ram;
	private String hdd;
	private String cpu;
	
	public ServerFactory(String ram, String hdd, String cpu){
		this.ram=ram;
		this.hdd=hdd;
		this.cpu=cpu;
	}
	
	@Override
	public Computer createComputer() {
		return new Server(ram,hdd,cpu);
	}

}

Now we will create a consumer class that will provide the entry point for the client classes to create sub-classes.

现在,我们将创建一个消费者类,该类将为客户端类创建子类提供入口。

ComputerFactory.java

ComputerFactory.java

package com.journaldev.design.abstractfactory;

import com.journaldev.design.model.Computer;

public class ComputerFactory {

	public static Computer getComputer(ComputerAbstractFactory factory){
		return factory.createComputer();
	}
}

Notice that its a simple class and getComputer method is accepting ComputerAbstractFactory argument and returning Computer object. At this point the implementation must be getting clear.

注意,它的一个简单类和getComputer方法是接受ComputerAbstractFactory参数并返回Computer对象。 此时,实现必须变得清晰。

Let’s write a simple test method and see how to use the abstract factory to get the instance of sub-classes.

让我们编写一个简单的测试方法,看看如何使用抽象工厂来获取子类的实例。

TestDesignPatterns.java

TestDesignPatterns.java

package com.journaldev.design.test;

import com.journaldev.design.abstractfactory.PCFactory;
import com.journaldev.design.abstractfactory.ServerFactory;
import com.journaldev.design.factory.ComputerFactory;
import com.journaldev.design.model.Computer;

public class TestDesignPatterns {

	public static void main(String[] args) {
		testAbstractFactory();
	}

	private static void testAbstractFactory() {
		Computer pc = com.journaldev.design.abstractfactory.ComputerFactory.getComputer(new PCFactory("2 GB","500 GB","2.4 GHz"));
		Computer server = com.journaldev.design.abstractfactory.ComputerFactory.getComputer(new ServerFactory("16 GB","1 TB","2.9 GHz"));
		System.out.println("AbstractFactory PC Config::"+pc);
		System.out.println("AbstractFactory Server Config::"+server);
	}
}

Output of the above program will be:

上面程序的输出将是:

AbstractFactory PC Config::RAM= 2 GB, HDD=500 GB, CPU=2.4 GHz
AbstractFactory Server Config::RAM= 16 GB, HDD=1 TB, CPU=2.9 GHz

Here is the class diagram of abstract factory design pattern implementation.

这是抽象工厂设计模式实现的类图。

抽象工厂设计模式的好处 (Abstract Factory Design Pattern Benefits)

  • Abstract Factory design pattern provides approach to code for interface rather than implementation.

    抽象工厂设计模式提供了接口代码而不是实现代码的方法。
  • Abstract Factory pattern is “factory of factories” and can be easily extended to accommodate more products, for example we can add another sub-class Laptop and a factory LaptopFactory.

    摘要工厂模式是“工厂工厂”,可以轻松扩展以容纳更多产品,例如,我们可以添加另一个子类Laptop和一个Factory LaptopFactory。
  • Abstract Factory pattern is robust and avoid conditional logic of Factory pattern.

    摘要工厂模式具有鲁棒性,避免了工厂模式的条件逻辑。

JDK中的抽象工厂设计模式示例 (Abstract Factory Design Pattern Examples in JDK)

  • javax.xml.parsers.DocumentBuilderFactory#newInstance()

    javax.xml.parsers.DocumentBuilderFactory#newInstance()
  • javax.xml.transform.TransformerFactory#newInstance()

    javax.xml.transform.TransformerFactory#newInstance()
  • javax.xml.xpath.XPathFactory#newInstance()

    javax.xml.xpath.XPathFactory#newInstance()

抽象工厂设计模式视频教程 (Abstract Factory Design Pattern Video Tutorial)

I recently uploaded a video on YouTube for abstract factory design pattern. In the video, I discuss when and how to implement an abstract factory pattern. I have also discussed what is the difference between the factory pattern and abstract factory design pattern.

我最近在YouTube上上传了一段视频,以了解抽象的工厂设计模式。 在视频中,我讨论了何时以及如何实现抽象工厂模式。 我还讨论了工厂模式和抽象工厂设计模式之间的区别。

演示地址

GitHub Project. GitHub Project下载示例代码。

翻译自: https://www.journaldev.com/1418/abstract-factory-design-pattern-in-java

java工厂模式和抽象工厂

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值