当前位置:网站首页>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 calculates the distance between the two places
- First acquaintance with P4 language
- 【leetcode】74. Search 2D matrix
- What is 5g industrial wireless gateway? What functions can 5g industrial wireless gateway achieve?
- Wechat applet - realize the countdown of 60 seconds to obtain the mobile verification code (mobile number + verification code login function)
- C language practice - binary search (half search)
- Learn more about materialapp and common attribute parsing in fluent
- PIP installation of third-party libraries
- There is no prompt for SQL in idea XML, and the dialect setting is useless.
猜你喜欢

C language practice - binary search (half search)

What is 5g industrial wireless gateway? What functions can 5g industrial wireless gateway achieve?

Recently, the weather has been extremely hot, so collect the weather data of Beijing, Shanghai, Guangzhou and Shenzhen last year, and make a visual map

10 minutes to understand CMS garbage collector in JVM

Introduction to vmware workstation and vSphere
![[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!](/img/46/d36ae47c3d44565d695e8ca7f34980.jpg)
[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!

Opencv learning example code 3.2.4 LUT

Spring moves are coming. Watch the gods fight

Pytoch --- use pytoch to realize u-net semantic segmentation

A thorough understanding of the development of scorecards - the determination of Y (Vintage analysis, rolling rate analysis, etc.)
随机推荐
Three ways for programmers to learn PHP easily and put chaos out of order
Www 2022 | rethinking the knowledge map completion of graph convolution network
Okcc why is cloud call center better than traditional call center?
Wechat applet pull-down loading more waterfall flow loading
Bitmap principle code record
Demonstration description of integrated base scheme
C语言猜数字游戏
Yolov5网络修改教程(将backbone修改为EfficientNet、MobileNet3、RegNet等)
Introduction to JSON usage scenarios and precautions
MySQL error: expression 1 of select list is not in group by claim and contains nonaggre
[untitled]
Installation and use of blue lake
Microsoft Research Institute's new book "Fundamentals of data science", 479 Pages pdf
Hand tear - sort
Play with concurrency: what's the use of interruptedexception?
C language practice - binary search (half search)
Pytoch --- use pytoch to realize u-net semantic segmentation
Pytorch-Yolov5從0運行Bug解决:
[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!
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