当前位置:网站首页>What methods should service define?
What methods should service define?
2022-07-02 04:17:00 【Mingmingruyue senior】
One 、 background
Recently during code review , I will find that some new students in school will take some steps as Service Methods , stay Facade Layer arranges these steps to complete a complete function . This is obviously not normal .
Facade Layer is usually used as the entry of front-end or other service calls , Mainly responsible for parameter verification 、 Parameters required to construct the underlying service 、 Result conversion, etc .
A single business function should not be split into Service in , stay Facade These methods are arranged in , Finally complete a business function .
Facade As a facade , There should be no business logic in itself , Business logic should be placed in Service layer .Service Each method of should be able to complete a relatively complete business intention independently , Instead of providing multiple steps of a function , Let the outside assemble .
Two 、 Definition
Service The service in should have “ Business atomicity ”, This atomicity is not exactly the same as that of a database .
The so-called business atomicity refers to the ability to completely realize a certain business , Not a sub step of a business .
3、 ... and 、 analogy
This is like what many places offer “ Run once at most ” The policy of , Complete a business , Just run once ; There is no need to complete only one step of a certain intention at a time .
It's like buying a cup of coffee in the self-service coffee machine , You just need to choose the right type , Just pay . The production of coffee may include heating water 、 Add coffee powder 、 Add milk 、 Mixing and other steps , You should not know or participate in every process . Coffee service should be complete , Not every step requires you to press a button to arrange .
Just like the currency exchange service , You go to the exchange window to exchange RMB into baht , The general service is complete , Generally, they give RMB to them , They can directly give you the corresponding amount of baht .
You don't need to check the exchange rate yourself , Calculate the amount you can exchange , Go to their counter and take out the money by yourself , Count by yourself , Then say to yourself “ looking forward to your next visit ”.
The facade is equivalent to the public comment , Users can see the location of the target store here 、 Switching time and other information .

You can even comment on the public 、 Meituan orders , However, the target store is still required to provide corresponding catering services .
Reference resources 《 Clean code 》 Chapter six A case in the object and data structure section :
encapsulation 1:
public interface Vehicle{
double getFuelTankCapacityInGallons();
double getGallonsOfGasoline();
}
encapsulation 2:
public interface Vehicle{
double getPercentFuelRemaining();
}
The second interface definition method is better , We should not expose data details , Data should be expressed in an abstract way , Think a little about the defined interface .
This mainly includes several aspects :
1 Design interface principles try to hide complexity ( The principle of least knowledge )
2 Service The method provided by the layer should express the complete business intention ( If I want to check the percentage of fuel tank left ; Instead of providing two interfaces , One is to query the total quantity , Then check the current quantity and calculate it by yourself ), Not a step .
边栏推荐
- 【c语言】基础篇学习笔记
- 千亿市场规模医疗美容行业的水究竟有多浑?
- powershell_ View PowerShell function source code (environment variable / alias) / take function as parameter
- Actual combat | use composite material 3 in application
- pip 安装第三方库
- Qt插件之Qt Designer插件实现
- Yolov5 network modification tutorial (modify the backbone to efficientnet, mobilenet3, regnet, etc.)
- 【毕业季·进击的技术er】年少有梦,何惧彷徨
- Raspberry pie GPIO pin controls traffic light and buzzer
- Go function
猜你喜欢

Demonstration description of integrated base scheme

Lei Jun wrote a blog when he was a programmer. It's awesome

Raspberry pie GPIO pin controls traffic light and buzzer

树莓派GPIO引脚控制红绿灯与轰鸣器

Spring recruitment of Internet enterprises: Kwai meituan has expanded the most, and the annual salary of technical posts is up to nearly 400000

BGP experiment the next day

Yolov5 network modification tutorial (modify the backbone to efficientnet, mobilenet3, regnet, etc.)

Dare to go out for an interview without learning some distributed technology?
![[C language] Dynamic Planning --- from entry to standing up](/img/7e/29482c8f3970bb1a40240e975ef97f.png)
[C language] Dynamic Planning --- from entry to standing up

Play with concurrency: what's the use of interruptedexception?
随机推荐
Realizing deep learning framework from zero -- Introduction to neural network
Yyds dry goods inventory kubernetes introduction foundation pod concept and related operations
Go function
Play with concurrency: draw a thread state transition diagram
【提高课】ST表解决区间最值问题【2】
LCM of Spreadtrum platform rotates 180 °
How much is the tuition fee of SCM training class? How long is the study time?
Fluent icon demo
Is the product of cancer prevention medical insurance safe?
SQL: common SQL commands
SQL:常用的 SQL 命令
"No war on the Western Front" we just began to love life, but we had to shoot at everything
WPViewPDF Delphi 和 .NET 的 PDF 查看组件
Lei Jun wrote a blog when he was a programmer. It's awesome
如何解决在editor模式下 无法删除物体的问题
cookie、session、tooken
【leetcode】81. Search rotation sort array II
What is 5g industrial wireless gateway? What functions can 5g industrial wireless gateway achieve?
[untitled]
手撕——排序