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();@Overridepublic 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;}@Overridepublic String getRAM() {return this.ram;}@Overridepublic String getHDD() {return this.hdd;}@Overridepublic 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;}@Overridepublic String getRAM() {return this.ram;}@Overridepublic String getHDD() {return this.hdd;}@Overridepublic 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;}@Overridepublic 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;}@Overridepublic 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工厂模式和抽象工厂

java工厂模式和抽象工厂_Java中的抽象工厂设计模式相关推荐

  1. java 抽象接口_JAVA中的“抽象接口”

    在程序设计过程中,读者很可能遇到这样一种困境:设计了一个接口,但实现这个接口的子类并不需要实现接口中的全部方法,也就是说,接口中的方法过多,对于某些子类是多余的,我们不得不浪费的写上一个空的实现. 今 ...

  2. java网页统计访客量_Java中的访客设计模式

    java网页统计访客量 Visitor Design Pattern is one of the behavioral design pattern. 访客设计模式是行为设计​​模式之一. 访客设计模 ...

  3. Java设计模式——工厂模式讲解以及在JDK中源码分析

    需求:便于手机种类的扩展 手机的种类很多(比如HuaWeiPhone.XiaoMiPhone等) 手机的制作有prepare,production, assemble, box 完成手机店订购功能. ...

  4. 抽象工厂模式java_面试官:说一下静态工厂模式,工厂方法模式,抽象工厂的区别吧...

    静态工厂模式 用生活中的场景类比一下这三种模式,假如你想学习Java视频,你得自己到处去找资料,找资料是一个繁琐的过程,过一段时间你又想学Python视频了,你还得去找资料.现在你在学校上学,你想学J ...

  5. 将简单工厂模式改造应用到项目中,而不是纸上谈兵

    10月26日晚补充:经过掘友的提醒,我才发现之前我这篇所写的策略模式,其本身更偏向于工厂模式,我起初以为是掘友分不清工厂模式和策略模式,实际上是我自己把自己绕进去,看不清工厂模式和策略模式的区别. 因 ...

  6. java与模式pdf 闫宏_Java设计模式及实践.pdf下载

    Java设计模式及实践.pdf下载 资料简介:本书向读者展示Java语言中更加智能化的编码实例.书中首先介绍面向对象编程(OOP)和函数式编程(FP)范式,然后描述常用设计模式的经典使用方法,并解释如 ...

  7. java后台常用设计模式_Java中几个常用设计模式

    1.单例模式(有的书上说叫单态模式其实都一样) 该模式主要目的是使内存中保持1个对象.看下面的例子: package org.sp.singleton; //方法一 public class Sing ...

  8. java斐波那切数列_Java中的递归方法

    递归算法 1.递归算法 递归在计算机科学中也称为递归算法.一些问题在分解的时候会有这样的现象:解决该问题的过程是由重复的相似子过程组成,类似这样的问题我们都可以通过递归算法进行解决.在计算机语言中,递 ...

  9. lg显示器工厂模式怎么进入_一目了然 揭秘品牌LCD工厂模式进入方法

    [IT168 导购]关于显示器的工厂模式,它是厂家在设计电路时预留的一些功能,这些功能并不对普通用户开放的.通过特殊的方式进入,通过修改存储器数据或其他方式对显示器进行维护. 那么工厂模式都能做些什么 ...

最新文章

  1. 这个Python知识点,90%初学者没太整明白
  2. SQL SERVER 2000 安装问题
  3. Python 技巧篇-官方网站打不开的情况下通过官方获取最新python安装包方法
  4. mysql 表与表之间的条件比对_Mysql分库分表面试题(mysql高可用方案解析)
  5. Windwos中system、System32、SysWOW64区别
  6. AI+RPA,让你的工作模式开启“新方式”
  7. Ubuntu系统运行darknet出OSError: /libdarknet.so: cannot open shared object file: No such file or directory
  8. python min函数时间复杂度_作为Python程序员,你真的会用max()和min()函数吗?
  9. WinForm 实例教程 通讯录 视频教程 入门教程
  10. 股市华为鸿蒙是什么意思,4月华为鸿蒙概念股市回顾数据(干货满满)
  11. 猫哥教你写爬虫 040--存储数据-作业
  12. ORACLE ORA错误码大全 (备忘)
  13. 安装卸载Oracle
  14. 2021-2027全球与中国草坪和花园耗材市场现状及未来发展趋势
  15. SaaS,iass 和pass,你知道吗?
  16. Axure导入元件库的两种方式-附完整元件库
  17. 运用Python+Pygame开发坦克大战游戏_版本V1.01
  18. SCI:SCI论文写作技巧的详细攻略
  19. android 按钮3d效果图,android.graphics.Camera 实现简单的3D效果
  20. VB基础版版务处理_20050827

热门文章

  1. 转:LoadRunner检查点使用小结
  2. 批处理mysql命令
  3. [转载] python tuple类型如何索引_Python基础数据类型——tuple浅析
  4. [转载] python多重继承初始化_关于python多重继承初始化问题
  5. [转载] 用python 获取当前时间(年-月-日 时:分:秒),并且返回当前时间的下一秒
  6. vue路由传参丢失问题
  7. npm install 错误 安装 chromedriver 失败的解决办法
  8. 在.net core 2.0中生成exe文件
  9. vue.js基础知识篇(1):简介、数据绑定
  10. 算法5-7:区间检索