Object-Oriented Design and Construction Second Summary Assignment

write in front

  Through the training of these three assignments, the design method of multithreading has been preliminarily mastered. These three assignments have given me a deeper understanding of this course. This course is indeed a heavy course. It is not only difficult, high-intensity, but also durable. Teacher Wu Ji calls it the "Kunlun Course". "No exaggeration. This is a double exercise of body and mind.

The seventh operation - the passenger call and answer system of the simulated taxi

---contents of homework

  This operation simulates the passenger calling and answering system of taxis, trains thread-safe design methods, and applies object-oriented analysis methods and design principles to carry out analysis and design.

  The objects involved in the job are mainly maps, taxis and taxi dispatching systems. The passenger's request is input from the console. After the passenger sends a request, the system broadcasts to the taxis in the 4x4 area centered on the passenger. The taxi that receives the broadcast will grab the order. The broadcast has a certain time window. After the window is closed, the system From all the taxis vying for orders, choose a suitable taxi based on the taxi credit and the distance between the taxi and the passenger. After the taxi service ends, the information such as the route and time of the service needs to be output to the file.

---job design

  The picture below is the class diagram for this assignment. The main classes are Taxi class, Scheduler class and Map RoadMap class. From the thread point of view, there are 100 taxi threads in total, the dispatch thread due to the request, the main dispatch thread, the input processing thread and the main thread. The input processing thread is responsible for receiving and processing the console input. The main scheduling thread creates a scheduling thread for each request. The scheduling thread broadcasts in the 4x4 area and selects a taxi. After the taxi service ends, the scheduling thread outputs the information and ends. The relationship between threads can be seen from the collaboration diagram that follows.

  RoadMap can store maps, find the shortest path between two points, and determine whether there is a road from a point to a certain direction. A path is a Path object, represented by direction + distance. The length of each road in the job is the same, so the distance information is omitted and only the direction is recorded. Path objects are immutable and therefore thread-safe. There is only one RoadMap object in the whole program, but it is designed as an immutable object, so it is also thread-safe, and different taxis can call the shortestPath() method at the same time.

  Since the trajectory of the taxi and the state of the taxi at a certain moment are to be output, in order to manage these information, the PathRecord object and the TaxiInfo object are used to record. PathRecord objects are mutable and new tracks can be added. TaxiInfo objects are immutable. In addition, immutable objects include Request objects, Point objects, and these immutable objects are thread-safe.

  The taxi class has serve(), wander(), rest(), takeOrder() methods, which correspond to the taxi's service, waiting, stopping, and order-taking states, respectively. The taxi thread mainly interacts with the scheduling thread. Therefore, the focus of thread coordination and synchronization control is on the design of the taxi thread and the scheduling thread.

 

 

 

---Metric analysis

  The maximum cyclomatic complexity is 13, mainly from the construction method of RoadMap and the run() method of the taxi class. The maximum value of NestedBlockDepth is 4, which is mainly caused by two layers of loops plus if-else nesting. There are three three-level nested code and three two-level nested code in total.

  The total number of lines of code is about 860 lines, of which Taxi class, Scheduler class and RoadMap class each account for about 200 lines. The total number of lines of method code is about 570 lines, the maximum number of lines of each method code is 30, the average is 6.2, and the total number of methods is 92.

 ---Advantages and disadvantages

  The bug in this job is that it ignores the processing of requests with the same origin and destination, and the case where the taxi is exactly at the passenger's position when the order is taken. Both of these cases result in a null pointer when looking for the shortest path to construct the Path object. I think the reasons for the problem are as follows:

  1. The homework instruction book clarifies that the same origin and destination are considered invalid requests, but I forgot this during the program writing process.
  2. For each request, the taxi has two journeys, one is from the location where the taxi took the order to the passenger's location, and the other is from the passenger's location to the destination. The requirements of the guide book guarantee that the second point will not go wrong, while the first point is not guaranteed, so it needs to be taken into account when coding.
  3. The empty path should be considered when constructing the Path object, but this problem was not found during the design and implementation.

  I think the advantages of this assignment are as follows:

  1. Class responsibilities are relatively balanced.
  2. The requirements are basically met in terms of thread coordination and synchronization control.

  The disadvantages are as follows:

  1. Some designs have room for improvement in performance. For example, to find taxis in the 4x4 area around the passenger, I used the method of querying 100 taxis, but some optimization can be done.
  2. The complexity is somewhat high. For example, in the run() method of the taxi thread mentioned above, although the details are not expanded in the run() method, there are still multi-layer loops or nested branches. I didn't feel anything when I wrote it, but when I look back now, it's hard to tell why I wrote it this way, and whether I wrote it this way.

  The amount of code for each method is small, but the number of methods is large. I don't know if this is an advantage or a disadvantage. From the screenshot above, you can see that the average number of lines of code for a method is 6.2, but there are 92 methods. For readers of the code, too many methods make it difficult to grasp the overall structure, and a single method is too large to understand the parts. If you'd like to share your insights with me, please comment below.

The sixth job - monitoring file attribute changes

---contents of homework

  The job content is to implement a monitoring program, for the monitoring objects within a given monitoring range, to detect the changes of the related attributes of the monitoring objects in a scanning manner, thereby triggering the specified processing actions. The monitoring scope refers to a directory tree in the computer file system, and the monitoring object refers to the specific files within the monitoring scope. Processing actions include restore, log details, log summary. The goal of the job is to train how to balance the conflict between thread access control and shared objects for thread safety issues.

---job design

  From the perspective of threads, a monitoring command corresponds to a monitoring thread and a file scanning thread. The Monitor class can determine whether the triggers of the monitored object (including rename, move, size change, and last modification time change) are triggered, and perform corresponding processing actions. The Snapshot class is a snapshot of file attributes, and its structure is a directory tree. SnapshotManager is a snapshot management class, and FileScanner is a file scanning thread, which creates snapshots and hands them over to SnapshotManager for management. The Analyzer class and the TaskPerformer class are the snapshot analysis class and the task execution class respectively. The monitoring thread (Monitor) takes the latest snapshot from the SnapshotManager and sends it to the Analyzer for analysis. If it is triggered, the TaskPerformer will execute the processing action. Summary and Detail classes are classes that manage detailed information and summaries, which are regularly written to files.

  The Snapshot object is immutable. It records the attributes of all files in the directory at the time of scanning. It is a recursive structure, so it is more convenient to use when analyzing snapshots. The TaskTuple object is also immutable, and records the monitoring objects, triggers and tasks corresponding to a monitoring command. The Summary class, Detail class, and SnapshotManager class are all designed as thread-safe classes, because all monitoring threads share the same Summary and Detail objects and may be accessed by multiple threads at the same time. SnapshotManager is shared between file scanning threads and monitoring threads. Also needs to be thread safe. SafeFile is a thread-safe file access class.

 

  

 

---Metric analysis

  The maximum cyclomatic complexity is 12, from the method of processing the input. Through the metric analysis of the past several assignments, I found that the cyclomatic complexity of input processing is high, probably because it needs to deal with various input errors. If you have any method to reduce the complexity, please comment below.

  The maximum nesting depth is 5, and there are 1 4-level nesting and 3 3-level nesting codes (inside the method). The maximum nesting depth comes from the run() method of Monitor. The loop and try-catch take up two layers, and the if-else branch takes up two layers, making a total of four layers. The other 3 levels of nesting are all one level loop plus two levels of if-else. This kind of multi-level nested code is not very readable, and its correctness is difficult to judge, so it should be avoided as much as possible.

  The number of all properties is 50, the number of methods is 69, and the total code size is about 670 lines. The number of lines of code per method, the average is 6.4 lines, most of them are 10-20 lines, and there are two methods that are about 30 lines.

 ---Advantages and disadvantages

  The bug of this job is that when monitoring renaming, if there are multiple files with the same size and the same last modification time in the same directory, an error will be judged. The criterion for judging renaming is that the original file disappears (the absolute path cannot be found), and a new file is added with the same size and last modification time as the disappeared file. This bug appeared when I ignored the "add" requirement.

  The advantages are as follows:

  1. The overall structure is relatively clear.
  2. Achieved the goal of training thread safety and mastering how to balance the contradiction between thread access control and shared objects.

  The disadvantages are as follows:

  1. The simplest method is used to compare file names and file paths. In fact, hashing can be used to improve the speed.
  2. Some places are deeply nested, not easy to read, and prone to errors.

The fifth operation - multi-elevator dispatching system

---contents of homework

  This work is to design a set of multi-elevator dispatching system composed of 3 elevators. By adopting the thread mechanism, the new dispatching system procedure is completed on the basis of the procedure realized by the third operation. The elevator can support piggybacking, and the dispatching system dispatches floor requests according to the motion balance strategy.

---job design

  Because there are too many program classes written this time, and the relationship between classes is complicated, no class diagram is given. The following picture is a general structure, each box represents an object, and each ellipse represents a tray for placing and fetching requests (because I am not familiar with writing multi-threaded programs for the first time, so I explicitly use "" tray" to indicate). A single arrow indicates the flow direction of the request, and two segments with arrows indicate interaction between the two threads. RequestSimulator is a request simulator that reads input from the console and generates requests. The MultiScheduler is the overall scheduler, which is responsible for distributing the direct elevator requests to each elevator, and scheduling the floor requests with piggybacking and motion balancing strategies. Further down is the Scheduler, which is responsible for scheduling single-step elevators, such as handling piggyback requests, etc., which reduces to the problem of the third job.

---Metric analysis

  The maximum cyclomatic complexity is 20, from Scheduler's scheduling method. The total number of lines of code is about 1200 lines, and the total number of methods is 151. It can be found that the latter two operations (6-7) did not reach such a large scale, which may be due to the complexity of the elevator scheduling logic.

 ---Self-evaluation

  There are two main bugs in this job. First, in the processing of input, the processing of multiple empty requests in one line does not match the description in the documentation. It is a problem that I neglected to test. If I test it, I can find it. Second, there is a problem with the piggybacking of the floor request. The specific problem has not been studied in depth. I guess it may be a logical problem and a problem of unfamiliarity with the threading mechanism.

  The disadvantages are as follows:

  1. Logical confusion. For example, the scheduling method of the scheduler, various methods of the elevator. First, the design of the previous two operations was inherited, which made it difficult to modify, which affected the whole body. Second, it is difficult to sort out the relationship of each part after adding multi-threading.
  2. The complexity is too high. High complexity makes it error-prone, hard to test, and hard to read.
  3. Violates many design principles such as OCP, LSP.

  Since it has been a long time since the 5th assignment, the code was written in a mess, and I couldn't bear to look directly, so I will briefly analyze it.

 end

  Thank you for taking the time to read this article. This is a summary of my three homework assignments. Most of the content is an analysis of my homework. The readers may not know the specific details. Therefore, when writing, I try to avoid details as much as possible, which makes it seem a bit broad. If you have questions or suggestions, please comment below.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325114211&siteId=291194637