当前位置:网站首页>Interviewer: what is the factory method mode?
Interviewer: what is the factory method mode?
2022-07-28 02:27:00 【Mr. Li, a genius】
A true master always has the heart of an apprentice
If you can't bear the pain of study, you have to bear the pain of life
Articles are constantly updated , You can search by wechat 【 Xiaoqi JAVA interview 】 First time reading , reply 【 Information 】 What's more, I have prepared welfare for you ! reply 【 project 】 I have some project source code for you . reply 【 The resume template 】 There is a resume template I prepared for you .

List of articles
Preface
Today's electronic sewing machine factory is still in full swing , The boys are full of energy to marry their wives , Step on the sewing machine pedal under your feet and smoke , Just when I concentrated on my work and reached a state of man-machine integration , Suddenly the door of the factory opened from the outside , Here comes a new colleague , In line with the principle of not having to watch your own excitement , Like everyone else, I slowly moved my eyes to the door .
Why ~, Why does this person look so familiar ? Isn't this wanghong battery brother , How can he come to work .

Take advantage of lunch , I found the battery brother .
I :“ Battery brother , You claim that you can't work , Why did you come here to work .”
Battery brother :“ I didn't come to work , I'm here to work .”
I :“ You made me speechless ... Who said you came to work , I'm curious .”
Battery brother :“ No one persuaded me , I saw the advertisement at the entrance of the village , I'm going to earn money to find a mother-in-law .”

I :“ The advertising effect is really good , Easy to understand and attractive .”
Just then my cell phone suddenly rang .
I :“ feed , Who is this !”
other party :“ I , I called you for an interview yesterday , Is it convenient for you now , Let's talk again .”
I :“ convenient , It's convenient for me to answer the phone as long as it's at work .”
One 、 interview
interviewer : Let's talk about the factory method mode today , Tell me about your understanding of the factory method pattern ?
I : The factory method pattern is an extension of the simple factory pattern , It inherits the advantages of the simple factory model , At the same time, it makes up for the defects of the simple factory model .
interviewer : Can you talk about the advantages of factory method mode over simple factory mode ?
I : In the factory method pattern, the logic in our factory class corresponds to the parameters passed in , If you want to buy apples , Then you have to have the judgment logic of generating Apple instances according to Apple parameters in the factory class , When you suddenly want to eat pineapple , If there is no logic of this block in the factory class , You have to add this piece of logic to the factory class , In this way, you will frequently modify the code in the factory class , This is not in line with the opening and closing principle .
The factory method pattern adds a specific factory between the factory class and the product , Our specific products are produced through specific factories , There is no need to modify the logic in the factory class .
For example, we were “ How sweet the orchard ”, If you want to eat apples, call our factory directly and we will mail you a box of apples . But now our business has expanded , We started raising poultry , If you want to eat chicken, you can also call us , But this time it's back “ How sweet the orchard ” It's a little inappropriate to call .
So I now set up a Chico Group , If you want something to eat, just call Qige Group , If you want to eat apples , Just call Qige Group , Then Chico group gave it to its subordinates “ How sweet the orchard ” Call and mail you a box of apples , If you want to eat chicken , You also call Chico Group , Then Chico gave it to his subordinates “ It's really delicious. Chicken coops ” Call you and mail a chicken .
interviewer : Let's talk about the roles of the factory method model !
1、Product( Abstract product ): It defines the interface of the product , Here he is equivalent to a box of fruit , Or a cage for chickens .
2、ConcreteProduct( Specific products ): He implements the abstract product interface , He is equivalent to Apple or chicken here .
3、Factory( Abstract factory ): He is equivalent to the Chico group here , It is used to receive calls from customers , Then give the customer a box of fruit or a cage of chicken .
4、ConcreteFactory( Specific factory ): Here he is equivalent to “ How sweet the orchard ” perhaps “ It's really delicious. Chicken coops ”, He is responsible for receiving calls from the group and then giving corresponding products to the Group .
interviewer : Can you write specific code to describe the factory method pattern ?
1、 First, define a box interface , This represents the abstract product role
public interface Box {
public void show();
}
2、 Then define an apple class , This represents specific products
public class Apple implements Box {
@Override
public void show() {
System.out.println(" Bought a box of apples ");
}
}
3、 Then define a Chico group class , This represents an abstract factory
public interface FactoryQG {
public void show(String name); // Abstract factory methods
}
4、 Then define a “ It's delicious ” class , This represents a specific factory
public class Zxy implements FactoryQG {
Box box = null;
@Override
public void show(String name) {
if(name.equals(" Apple ")){
box = new Apple();
box.show();
System.out.println(" It's really delicious. Orchard apples ,50 Yuan per box ");
}
}
}
5、 Final test
public class Test {
public static void main(String[] args) {
FactoryQG factoryQG = new Zxy();
factoryQG.show(" Apple ");
}
}

Through the above process, we can see , If you need to add one at this time “ Chicken coop ”, Just add a specific factory class , There is no need to modify the code of the abstract factory class , This is in line with the opening to expansion , Turn off for changes .
interviewer : Boy... Yes , When will you have time to join the company ?
I : To say again , I'll finish sewing the autumn pants in my hand first ...
Two 、 summary
The relevant contents here have not been sorted out yet , The article continues to be updated later , Recommended collection .
The commands involved in the article must be typed several times each like me , Only in the process of knocking can you find out whether you really master the command .
If you think my article is good, just like it , In addition, you can search through wechat 【 Xiaoqi JAVA interview 】 First time reading , reply 【 Information 】 What's more, I have prepared welfare for you ! reply 【 project 】 I have some project source code for you . reply 【 The resume template 】 There is a resume template I prepared for you .
边栏推荐
- Understand the "next big trend" in the encryption industry - ventures Dao
- Implementation of mongodb/mongotemplate.upsert batch inserting update data
- 【愚公系列】2022年07月 Go教学课程 019-循环结构之for
- Promise从入门到精通(第3章 自定义(手写)Promise)
- 11-Django-基础篇-数据库操作
- 并发编程的三大核心问题(荣耀典藏版)
- Leetcode hot topic Hot 100 - > 1. Sum of two numbers
- 小米网站主页面大模块——小模块+导航(浮动案例)
- A lock faster than read-write lock. Don't get to know it quickly
- Unity saves pictures to albums and rights management
猜你喜欢

Under the new retail format, retail e-commerce RPA helps reshape growth

CeresDAO:Ventures DAO的“新代言”

The level "trap" of test / development programmers is not a measure of one-dimensional ability

54: Chapter 5: develop admin management services: 7: face warehousing process; Face login process; The browser turns on the video debugging mode (so that the camera can also be turned on in the case o
![[Yugong series] July 2022 go teaching course 019 - for circular structure](/img/40/b4e673de0462c3dd6ca8b8fb513914.png)
[Yugong series] July 2022 go teaching course 019 - for circular structure

Wechat campus bathroom reservation applet graduation design finished product (2) applet function

Clear the cause of floating and six methods (solve the problem that floating affects the parent element and the global)
![This operation may not be worth money, but it is worth learning | [batch cutting of pictures]](/img/e8/a34e471b0089f8085b140c74b5c01f.jpg)
This operation may not be worth money, but it is worth learning | [batch cutting of pictures]

Flex布局—固定定位+流式布局—主轴对齐—侧轴对齐—伸缩比

【ROS进阶篇】第九讲 基于Rviz和Arbotix控制的机器人模型运动
随机推荐
Flume (5 demos easy to get started)
Vxe table/grid cell grouping and merging
剑指offer专项突击版第12天
Implementation of mongodb/mongotemplate.upsert batch inserting update data
借助Elephant Swap打造的ePLATO,背后的高溢价解析
获取两个集合相差数据
[database data recovery] data recovery case of insufficient disk space of SQL Server database
Promise从入门到精通(第4章 async 和 await)
Flex布局—固定定位+流式布局—主轴对齐—侧轴对齐—伸缩比
Codeworks round 807 (Div. 2) a-c problem solution
Principle and implementation of cross entropy
Appium click operation sorting
结构伪类选择器—查找单个—查找多个—nth-of-type和伪元素
Understand the "next big trend" in the encryption industry - ventures Dao
Aike AI frontier promotion (7.14)
Appium 点击操作梳理
【愚公系列】2022年07月 Go教学课程 019-循环结构之for
【愚公系列】2022年07月 Tabby集成终端的使用
Promise from introduction to mastery (Chapter 1 Introduction and basic use of promise)
[advanced ROS chapter] Lecture 10 gadf integrated simulation process and examples based on gazebo