当前位置:网站首页>Thorough and thorough analysis of factory method mode
Thorough and thorough analysis of factory method mode
2022-06-24 01:48:00 【Tom bomb architecture】
This article is excerpted from 《 This is how design patterns should be learned 》
1 Application scenarios of the factory method pattern
The factory method mode is mainly applicable to the following application scenarios .
(1) Creating objects requires a lot of repetitive code .
(2) client ( application layer ) Does not depend on how product class instances are created 、 Implementation details .
(3) A class uses its subclasses to specify which object to create .
2 Factory method mode UML Class diagram
Factory method mode UML The class diagram is shown in the figure below .
As can be seen from the picture above , Abstract factory pattern mainly includes 4 A character .
(1) Abstract factory (Factory): It's the core of the factory approach pattern , It's not about the application . Any factory class of an object created in a pattern must implement this interface .
(2) Specific factory (Concrete Factory): Is a concrete factory class that implements the abstract factory interface , Contains logic that is closely related to the application , And is called by the application to create the product object .
(3) Abstract product (Product): Is the supertype of the object created by the factory method pattern , That is, the common parent class or the common interface of the product object .
(4) Specific products (Concrete Product): This role implements the interface defined by the abstract product role . A specific product has a specific factory , They often correspond to each other .
3 A common way to write factory method patterns
The following is a general description of the factory method pattern .
public class Client {
public static void main(String[] args) {
IFactory factory = new FactoryA();
factory.makeProduct().doSomething();
factory = new FactoryB();
factory.makeProduct().doSomething();
factory = new FactoryC();
factory.makeProduct().doSomething();
}
// Abstract factory
public interface IFactory {
IProduct makeProduct();
}
// production ProductA Specific factory class
static class FactoryA implements IFactory {
public IProduct makeProduct() {
return new ProductA();
}
}
// production ProductB Specific factory class
static class FactoryB implements IFactory {
public IProduct makeProduct() {
return new ProductB();
}
}
// production ProductC Specific factory class
static class FactoryC implements IFactory {
public IProduct makeProduct() {
return new ProductC();
}
}
// Abstract product
public interface IProduct {
void doSomething();
}
// Specific products :ProductA
static class ProductA implements IProduct {
public void doSomething() {
System.out.println("I am Product A");
}
}
// Specific products :ProductB
static class ProductB extends FactoryB implements IProduct {
public void doSomething() {
System.out.println("I am Product B");
}
}
// Specific products :ProductC
static class ProductC implements IProduct {
public void doSomething() {
System.out.println("I am Product C");
}
}
}4 Implement product extension using factory method pattern
The factory method mode mainly solves the problem of product expansion , In a simple factory , With the enrichment of product chain , If the creation logic of each course is different , Then the responsibilities of the factory will become more and more , It's kind of like a universal factory , Not easy to maintain . According to the single responsibility principle , We will continue to split our functions , Someone to do special work .Java The course consists of Java The factory to create ,Python The course consists of Python The factory to create , Abstract the factory itself . First create ICourseFactory Interface .
public interface ICourseFactory {
ICourse create();
}Then create sub factories ,JavaCourseFactory The code for the class is as follows .
import com.tom.pattern.factory.ICourse;
import com.tom.pattern.factory.JavaCourse;
public class JavaCourseFactory implements ICourseFactory {
public ICourse create() {
return new JavaCourse();
}
}PythonCourseFactory The code for the class is as follows .
import com.tom.pattern.factory.ICourse;
import com.tom.pattern.factory.PythonCourse;
public class PythonCourseFactory implements ICourseFactory {
public ICourse create() {
return new PythonCourse();
}
}The client test code is as follows .
public static void main(String[] args) {
ICourseFactory factory = new PythonCourseFactory();
ICourse course = factory.create();
course.record();
factory = new JavaCourseFactory();
course = factory.create();
course.record();
}Finally, look at the class diagram shown in the figure below .
5 The factory approach pattern is in Logback Application in source code
Look at Logback The application of factory method pattern in , The class diagram is shown in the following figure .
As can be seen from the picture above , Different factories have been separated to create different logging frameworks , Such as Substitute Log framework 、NOP Log framework 、Log4J Log framework , So the corresponding Logger So is the product system , As shown in the figure below .
Pay attention to WeChat public number 『 Tom Bomb architecture 』 reply “ Design patterns ” The complete source code is available .
This paper is about “Tom Bomb architecture ” original , Reprint please indicate the source . Technology is about sharing , I share my happiness !undefined If this article helps you , Welcome to pay attention and like ; If you have any suggestions, you can also leave comments or private letters , Your support is the driving force for me to adhere to my creation . Pay attention to WeChat public number 『 Tom Bomb architecture 』 More technical dry goods are available !
边栏推荐
- 5、 Array base
- Tencent host security (cloud mirror) arsenal: a sword to kill mining Trojans binaryai engine
- What does fortress server mean? What are the benefits of Fortress server installation
- It's too difficult for me. Ali has had 7 rounds of interviews (5 years of experience and won the offer of P7 post)
- November 17, 2021: the longest path of the same value. Given a binary tree, find the longest path
- How to use the speech synthesis tool? Does the speech synthesis tool cost money?
- [technical grass planting] the cloud driver takes you straight to the clouds
- Security | how does Tencent virtual machine realize tamper proof of web pages?
- Tcapulusdb Jun · industry news collection
- Tcapulusdb Jun · industry news collection
猜你喜欢

It's too difficult for me. Ali has had 7 rounds of interviews (5 years of experience and won the offer of P7 post)

I, a 27 year old female programmer, feel that life is meaningless, not counting the accumulation fund deposit of 430000
![[SQL injection 13] referer injection foundation and Practice (based on burpseuite tool and sqli labs less19 target platform)](/img/b5/a8c4bbaf868dd20b7dc9449d2a4378.jpg)
[SQL injection 13] referer injection foundation and Practice (based on burpseuite tool and sqli labs less19 target platform)
![[SQL injection 12] user agent injection foundation and Practice (based on burpsuite tool and sqli labs LESS18 target machine platform)](/img/c8/f6c2a62b8ab8fa88bd2b3d8f35f592.jpg)
[SQL injection 12] user agent injection foundation and Practice (based on burpsuite tool and sqli labs LESS18 target machine platform)
随机推荐
Go language core 36 lectures (go language practice and application VII) -- learning notes
2、 Shell position variable
Build and enable all plug-ins of kubesphere high availability cluster
Why promote steam education?
Typescript is a weak type
Flink weapon: introduction to the open source platform streamx
How does SAP retail view which Po the allocation table is created with reference to?
Why use CDN content distribution network?
Tcapulusdb Jun · industry news collection
Gin framework: implementing distributed log tracing
How to make a ECS into a fortress machine how long does it take to build a fortress machine
8、 Pipeline pipeline construction project
Tcapulusdb Jun · industry news collection (November 22)
Tencent Conference - black screen analysis
Build a smart pole visual intercom system through an intelligent gateway
Railway patrol system - Railway Intelligent Patrol communication system
From idea to finished product, the necessary process of APP product development
[technical grass planting] cdn+ lightweight server +hugo= let the blog "cloud native"
Tcapulusdb Jun · industry news collection
How to use voice synthesis? Can voice synthesis modify the voice?