This article mainly introduces the simple factory pattern of java design pattern learning in detail, which has certain reference value. Interested friends can refer to it
Simple Factory Pattern
The simple factory pattern is a creational pattern, also called the Static Factory Method pattern, but it is not one of the 23 GOF design patterns. The simple factory pattern uses a factory object to decide which product class instance to create. The simple factory pattern is the simplest and most practical pattern in the factory pattern family and can be understood as a special implementation of different factory patterns.
Design a calculator using the simple factory pattern.
1: Create an operation class
package EasyFactoryModel;
abstract class Operation {
public double num1=0;
public double num2=0;
public double getNum1() {
return num1;
}
public void setNum1(double num1) {
this.num1 = num1;
}
public double getNum2() {
return num2;
}
public void setNum2(double num2) {
this.num2 = num2;
}
public abstract double getResult();
}2: Create an addition class.
class OpeartionAdd extends Operation {
public double getResult(){
double result=0;
result=num1+num2;
return result;
}
}3: Create a subtraction class.
public class OpeartionSub extends Operation {
public double getResult(){
double result=0;
result=num1-num2;
return result;
}
}4: Create a multiplication class.
public class OperationMul extends Operation {
public double getResult(){
double result=0;
result=num1*num2;
return result;
}
}5: Create a division class.
public class Operationp extends Operation {
public double getResult(){
double result=0;
result=num1/num2;
return result;
}
}6: After completing the basic operation class, you will find that you need to instantiate different classes according to the required addition, subtraction, multiplication and division. For this matter, you need a factory class.
public class OperationFactory {
public static Operation creatOpera(String operation){
Operation oper=null;
switch(operation){
case "+":oper=new OpeartionAdd();break;
case "-":oper=new OpeartionSub();break;
case "*":oper=new OperationMul();break;
case "/":oper=new Operationp();break;
}
return oper;
}
}The factory class creates different objects according to different operations. Here is the use of polymorphism.
7: Test class
public class Calculator {
public static void main(String[] args) {
Operation oper;
String operation="+";
oper=OperationFactory.creatOpera(operation);
oper.num1=1;
oper.num2=2;
double result=0;
result=oper.getResult();
System.out.println("result="+result);
}
}8: Console output:
result=3.0
Advantages: Simple The factory class is the key to the entire pattern, which contains the necessary logical judgment to determine which specific class object should be created based on external information.
By implementing the simple factory pattern, users do not need to know how the object is created, as long as they pass in Just the necessary information.
Disadvantages: The factory class concentrates the creation logic of all instances, violating the principle of high cohesion allocation.
As the specific product categories in the system continue to increase, it is necessary to continuously modify the factory categories, which is difficult to maintain and expand. At the same time, it also violates the principle of openness and closure.
The above is the detailed content of Learning the simple factory pattern in Java. For more information, please follow other related articles on the PHP Chinese website!
How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PMThe article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.
How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PMThe article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.
How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PMThe article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra
How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PMThe article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]
How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PMJava's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1
Powerful PHP integrated development environment

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

WebStorm Mac version
Useful JavaScript development tools






