search
HomeJavaJavaBaseWhat is factory pattern java
What is factory pattern javaNov 11, 2019 am 09:34 AM
javaFactory pattern

What is factory pattern java

What is factory pattern java

Factory Pattern is the most commonly used in Java One of the design patterns. This type of design pattern is a creational pattern, which provides an optimal way to create objects.

In the factory pattern, we do not expose the creation logic to the client when creating an object, and point to the newly created object by using a common interface.

To put it simply, the factory method is used instead of the new operation. In layman's terms, when you create a new object, you just call the factory method directly. When programming, you need to define a factory interface, which consists of different To implement it with a subclass, define a specific factory class and define a method to generate instances. We can use this method to obtain instances.

Advantages:

1. If a caller wants to create an object, he only needs to know its name.

2. High scalability. If you want to add a product, you only need to extend a factory class.

3. Shield the specific implementation of the product. The caller only cares about the product interface.

Disadvantages:

Every time you add a product, you need to add a specific class and object implementation factory, which doubles the number of classes in the system. To a certain extent, it increases the complexity of the system and also increases the dependence on specific classes of the system. This is not a good thing.

Application examples:

1. If you need a car, you can pick it up directly from the factory without worrying about how the car is made, and The specific implementation inside this car.

The simple implementation of the factory pattern is as follows:

//所有车的接口  有一个共同改的方法 开车
public interface Car {
    public void drive();
}
//奥迪类  实现car接口因为奥迪车也是车 肯定可以开的嘛
public class Audi implements Car {
    public Audi(){
        System.out.println("生产出一辆奥迪");
    }
    @Override
    public void drive() {
        System.out.println("开奥迪");
    }
}
//宝马类  实现car接口因为宝马车也是车 肯定也可以开
public class Bmw implements Car{
    public Bmw(){
        System.out.println("生产出一辆宝马");
    }
    @Override
    public void drive() {
        System.out.println("开宝马");
    }
}
//生产车的工厂  该工厂接收一个车名 你只需要告诉他你要生产什么车  就可以生产出对应的车出来
public class CarFactory {
    public static Car getCar(String caename){
        if(caename.equals("audi")){
            return new Audi();
        }else if(caename.equals("bmw")){
            return new Bmw();
        }
            return null;
    }
}
//测试
public class Test {
    public static void main(String[] args) {
        //告诉工厂类 你需要生产出一辆奥迪车  那么给昂他传一个参数audi进去
        Car audi = CarFactory.getCar("audi");
        //就可以生产出奥迪的车 调用开车的方法就可以开车啦
        audi.drive();
        //宝马类似
        Car bmw = CarFactory.getCar("bmw");
        bmw.drive();
    }
}
输出:
成产出一辆奥迪
开奥迪
生产出一辆宝马
开宝马

Recommended tutorial: java tutorial

The above is the detailed content of What is factory pattern java. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
带你搞懂Java结构化数据处理开源库SPL带你搞懂Java结构化数据处理开源库SPLMay 24, 2022 pm 01:34 PM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

java框架中工厂模式的应用场景有哪些?java框架中工厂模式的应用场景有哪些?Jun 01, 2024 pm 04:06 PM

工厂模式用于解耦对象的创建过程,将其封装在工厂类中,使之与具体类解耦。在Java框架中,工厂模式应用于:创建复杂对象(如Spring中的beans)提供对象隔离,增强可测试性和可维护性支持扩展,通过添加新工厂类增加对新对象类型的支持

java工厂模式有哪些好处java工厂模式有哪些好处Dec 25, 2023 pm 05:40 PM

java工厂模式的好处:1、降低系统的耦合度;2、提高代码的复用性;3、隐藏对象的创建过程;4、简化对象的创建过程;5、支持依赖注入;6、提供更好的性能;7、增强可测试性;8、支持国际化;9、促进开放封闭原则;10、提供更好的扩展性。详细介绍:1、降低系统的耦合度,工厂模式通过将对象的创建过程集中到一个工厂类中,降低了系统的耦合度;2、提高代码的复用性等等。

如何在Golang中应用工厂模式如何在Golang中应用工厂模式Apr 04, 2024 am 11:33 AM

工厂模式在Go中,工厂模式允许创建对象,无需指定具体类:定义一个表示对象的接口(例如Shape)。创建实现该接口的具体类型(例如Circle和Rectangle)。创建工厂类,根据给定的类型创建对象(例如ShapeFactory)。在客户端代码中使用工厂类创建对象。这种设计模式增强了代码的灵活性,无需直接耦合到具体类型。

一起聊聊Java多线程之线程安全问题一起聊聊Java多线程之线程安全问题Apr 21, 2022 pm 06:17 PM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

深入解析Java工厂模式:区分和应用简单工厂、工厂方法和抽象工厂的不同深入解析Java工厂模式:区分和应用简单工厂、工厂方法和抽象工厂的不同Dec 28, 2023 pm 03:09 PM

Java工厂模式详解:理解简单工厂、工厂方法和抽象工厂的区别与应用场景引言在软件开发过程中,面对复杂的对象创建和初始化过程,我们往往需要使用工厂模式来解决这一问题。Java作为一种常用的面向对象编程语言,提供了多种工厂模式的实现方式。本文将详细介绍Java工厂模式的三种常见实现方式:简单工厂、工厂方法和抽象工厂,并且对它们的区别以及应用场景进行深入分析。一、

详细解析Java的this和super关键字详细解析Java的this和super关键字Apr 30, 2022 am 09:00 AM

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

C++ 函数重载和重写中单例模式与工厂模式的运用C++ 函数重载和重写中单例模式与工厂模式的运用Apr 19, 2024 pm 05:06 PM

单例模式:通过函数重载提供不同参数的单例实例。工厂模式:通过函数重写创建不同类型的对象,实现创建过程与具体产品类的解耦。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool