当前位置:网站首页>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 .
边栏推荐
- The original author is out! Faker. JS has been controlled by the community..
- 向数据库中存入数组数据,代码出错怎么解决
- 【leetcode】74. Search 2D matrix
- 阿里云polkit pkexec 本地提权漏洞
- 微信小程序 - 实现获取手机验证码倒计时 60 秒(手机号+验证码登录功能)
- Today's plan: February 15, 2022
- Exposure X8标准版图片后期滤镜PS、LR等软件的插件
- Fingertips life Chapter 4 modules and packages
- [live broadcast review] the first 8 live broadcasts of battle code Pioneer have come to a perfect end. Please look forward to the next one!
- Hand tear - sort
猜你喜欢

Force buckle 540 A single element in an ordered array

Learn more about materialapp and common attribute parsing in fluent
![[C language] Dynamic Planning --- from entry to standing up](/img/7e/29482c8f3970bb1a40240e975ef97f.png)
[C language] Dynamic Planning --- from entry to standing up

Spring moves are coming. Watch the gods fight

The original author is out! Faker. JS has been controlled by the community..

Delete the code you wrote? Sentenced to 10 months!

Finally got byte offer. The 25-year-old inexperienced perception of software testing is written to you who are still confused

【leetcode】74. Search 2D matrix

Play with concurrency: draw a thread state transition diagram

Lei Jun wrote a blog when he was a programmer. It's awesome
随机推荐
Monkey测试
The original author is out! Faker. JS has been controlled by the community..
Microsoft Research Institute's new book "Fundamentals of data science", 479 Pages pdf
Common sense of cloud server security settings
Recyclerview add header
Monkey test
Force buckle 540 A single element in an ordered array
JVM knowledge points
Common locks in MySQL
【c语言】基础篇学习笔记
Pytorch---使用Pytorch进行鸟类的预测
2022-07-01: at the annual meeting of a company, everyone is going to play a game of giving bonuses. There are a total of N employees. Each employee has construction points and trouble points. They nee
Opencv learning example code 3.2.4 LUT
Pytorch-Yolov5從0運行Bug解决:
Introduction to JSON usage scenarios and precautions
Which is better, industrial intelligent gateway or edge computing gateway? How to choose the right one?
[source code analysis] NVIDIA hugectr, GPU version parameter server - (1)
【提高课】ST表解决区间最值问题【2】
How muddy is the water in the medical beauty industry with a market scale of 100 billion?
Introduction to vmware workstation and vSphere