当前位置:网站首页>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 .
边栏推荐
- Wechat applet map annotation
- Pytorch yolov5 exécute la résolution de bogues à partir de 0:
- Which insurance company has a better product of anti-cancer insurance?
- 【毕业季·进击的技术er】年少有梦,何惧彷徨
- 【c语言】基础篇学习笔记
- Wechat applet JWT login issue token
- [C language] basic learning notes
- Pytoch --- use pytoch for image positioning
- Spring recruitment of Internet enterprises: Kwai meituan has expanded the most, and the annual salary of technical posts is up to nearly 400000
- Pytorch---使用Pytorch实现U-Net进行语义分割
猜你喜欢
Delete the code you wrote? Sentenced to 10 months!
First acquaintance with P4 language
树莓派GPIO引脚控制红绿灯与轰鸣器
云服务器的安全设置常识
Analysis of the overall design principle of Nacos configuration center (persistence, clustering, information synchronization)
Opencv learning example code 3.2.4 LUT
Use of go package
How to model noise data? Hong Kong Baptist University's latest review paper on "label noise representation learning" comprehensively expounds the data, objective function and optimization strategy of
10 minutes to understand CMS garbage collector in JVM
QT designer plug-in implementation of QT plug-in
随机推荐
JVM知识点
初识P4语言
Pytorch---使用Pytorch进行鸟类的预测
Fingertips life Chapter 4 modules and packages
Yolov5 network modification tutorial (modify the backbone to efficientnet, mobilenet3, regnet, etc.)
Cloud service selection of enterprises: comparative analysis of SaaS, PAAS and IAAs
There is no prompt for SQL in idea XML, and the dialect setting is useless.
How to model noise data? Hong Kong Baptist University's latest review paper on "label noise representation learning" comprehensively expounds the data, objective function and optimization strategy of
Installation and use of blue lake
【提高课】ST表解决区间最值问题【2】
微信小程序 - 实现获取手机验证码倒计时 60 秒(手机号+验证码登录功能)
Pytoch yolov5 runs bug solution from 0:
Homework of the 16th week
Where can I buy cancer insurance? Which product is better?
Spring recruitment of Internet enterprises: Kwai meituan has expanded the most, and the annual salary of technical posts is up to nearly 400000
PIP installation of third-party libraries
Monkey test
云服务器的安全设置常识
cookie、session、tooken
[improvement class] st table to solve the interval maximum value problem [2]