The worst design writes the most reasonable program, the worst Design

Source: Internet
Author: User

The worst design writes the most reasonable program, the worst Design

I found that I had no comments or recommendations for the socket and 2 PC series code blog posts at the beginning, which directly led me to no longer want to write more in-depth code blog posts. Write such a messy blog, which looks quite popular.

Take the popular travel apps (such as Didi and kuaidi) for example to illustrate how to use the worst design to write the most reasonable program. From another perspective, let's look at program design.

 

When a passenger sets the starting and ending points of the route and clicks the Taxi call, what does the server need to do at this time?

Match the request according to the passenger's starting point and send it to some taxi drivers nearby. It is assumed that a taxi is a nearby driver within 1km square meters (because taxis do not need to consider the direction like a shuttle bus, and do not need to consider the refusal for the time being), and the driver decides whether to accept the taxi, when the driver accepts the ticket, the system prompts other drivers that the passenger has been on the bus.

According to official data, there are 100 k taxis in Shanghai (currently only in Shanghai). Based on various calculations, the number of concurrent operations per second in a single thread is about, in the worst design, a single passenger's taxi request (regardless of the network time consumption, only the server processing) can meet the market requirements after processing within 10 ms, drivers all traverse once. If the time can be completed within 10 ms, then we will do the program design without any problems, simple and direct, and meet the needs, this is a rational program design.

 

The problem arises. Because the driver's location is calculated at 120 km/h (33 m/s), the data is constantly changing, and the memory for storing driver data needs to be kept locked, direct foreach processing may not meet our needs. How can this problem be solved?

So we came up with the second solution to store driver data directly in rmdb, and perform non-clustered index on driver's geographical location, select a single table with 80 K data records, which is extremely fast. However, because the data is constantly changing, the calculation is based on 33 Mb/s. 1km is about 33 s, and m is about 16 s. In the worst design, we only need to test, the update that rmdb can support. In the case of 80 k/16 s (5 k/s), we do not have any problems in program design, it is simple and direct, and meets the needs, this is also a reasonable procedure.

So the question is, what if we are not a taxi but a private car? According to official data, the number of private cars in Shanghai is about 20%, and the annual growth rate is about. At this time, the database is far from meeting our needs. What should we do?

So we come up with the third solution, which divides the driver's data into one square meter (Shanghai has 7000 square kilometers) based on geographical longitude and latitude positions ), if there are about 7000 groups, then 1400 k/7000 = 200. Based on the starting point of a passenger, calculate the corresponding square. Then we only need to traverse the square, and the surrounding 8 squares (refer to the 9 cells) data, 200*9 = 1800. Then, based on the driver's location changes, the driver is constantly moving from one square to another. Under the worst design, if we traverse 1800 driver data at a time to meet our needs, then we will have no problems in program design, simple and direct, this is the most reasonable procedure to meet the requirements. If it is still slow, it can be split into 0.5 square kilometers. Generally, this is the end. This design is almost applicable to any travel application.

To put it another question: if the data is still large and cannot be met, we can continue to sort the drivers or process the intervals in the square data to reduce the traversal complexity, the algorithm question is not in the scope stated in this article. No specific descriptions are provided.

Please note that what I used previously is generally here and it is over. What is not so general?

At this point, if the driver is cheap, Lao Tzu will not pick up the passengers, and the passengers will also be cheap. I found that the driver did not receive the ticket for a long time, and Lao Tzu still will not cancel the taxi hailing. In addition, when a driver or passenger moves into another square, the driver must be prompted automatically, and the passenger is calling the car. Note: If the requested car hailing request is not completed and the geographical location changes, the new request that meets the matching requirement and does not meet the matching requirement, the message must be pushed, inform the driver that the passenger has exceeded the range or that a new passenger is calling the car.

As the number of taxi hailing requests increases, the number of concurrent requests to be processed increases to ensure the normal operation of the system.

Although this kind of scenario is unlikely to happen, this abnormal development demand often appears in some non-designated taxi-Hailing travel apps.

The problem arises. What should I do when such a demand comes?

It is still the old method. In the worst design, the number of requests that can be processed by a single thread is assumed to be 1000, and all requests are put into a queue. When a new request comes in, it determines whether there are already 1000 requests in the queue, which is being processed. If yes, the request will not be processed or the request will be sent to the queue of another thread or distributed to another machine. It continues until the number of queues is less than 1000 (the user cancels the Taxi call and automatically gives up upon timeout ). Of course, there are many specific processing methods. But in general, it is based on the worst case to design the program's Critical Value and processing method.

In general, a reasonable program must satisfy its worst design.


[High reward] design an adaptive algorithm in C/C ++ language (first, best, or worst adaptive algorithm)

I tested the dynamic partition content of the memory. It's easy to write.
1. you can use numbers to simulate the division of memory areas. For example, you can create an array of 100 size (Structure: struc (Region code, value). The value 0 indicates idle, and the value 1 indicates occupied, initialize Several partitions that have been identified as occupied. Partition 1, 1-5 occupy, and 6-12 idle ,......., it is easy to create a table in the idle area. You can scan the array from start to end.
2. first Adaptation: Find the first consecutive idle area greater than the request size from the memory address. For example, if you request five spaces, create partitions 2, 6-11 in the idle area at the beginning, occupied
3. Optimal Adaptation: refers to the one where all idle blocks are most suitable for the request size. min (idle block size-request size)
4. Worst: refers to the idle area that adapts to the request size and has the largest

Abstract A Class reasonably and write programs using the object-oriented programming ideology

# Include <iostream. h>
Class Number
{
Public:
Number (int a, int B = 1): flag (1)
{
Num1 =;
}
~ Number ()
{
Cout <"delete data! "<Endl;
}
Friend void display (Number & s );
Friend void modify (Number & s );
Static int n;
Void operator <(Number & s );
Void operator> (Number & s );
Private:
Int num1;
Const flag;
};
Void display (Number & s)
{
Cout <s. num1 <endl <s. flag <endl;
}
Void modify (Number & s)
{
Cout <"ä äè ë ý ¾" <endl;
Cin> s. num1;
}
Void Number: operator <(Number & s)
{
Display (s );
}
Void Number: operator> (Number & s)
{
Modify (s );
}
Int Number: n = 100;
Void main ()
{
Number n1 (12), n2 (13 );
Display (n1 );
N1> n1;
N1 <n1;
Cout <"show static member! "<Endl;
Cout <Number: n <endl;
}
It contains the knowledge points you want, and the variable meanings in it seem to have little to do with it. The main function is implementation, and the Private Member's friend interface. You can check it yourself and have limited capabilities, you can leave me a message if you don't understand it! Haha

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.