Methods for mutual control when objects are called

Source: Internet
Author: User

Object-Oriented Programming encourages programmers to distribute functions to multiple objects, so that each object has only a unique function (SRP), and the objects are highly cohesive enough. Such code is easier to understand, maintenance, modification, and reuse. At the same time, objects must call each other to complete complex operations. However, if there are too many calls between objects, high coupling between objects may occur, so that modifications to a code affect the seemingly unrelated Code (the bounce effect) in multiple places. In order to achieve high cohesion, objects should be less dependent, it depends on the required minimum interface (ISP ). This article uses a small example to analyze several ways in which callers and callers can control each other during inter-object calls. This morning, I took the (Oh, it should be a station) subway station and thought about the call method between objects. I just thought that Beijing Metro was about to raise the price recently. So I came up with an example of displaying the fare based on the bus stops and bus stops. Specifically, the user selects the upper station, the lower station and the bus strategy on the screen (minimum site and minimum time), and then the fare is displayed on the screen. In implementation, the PriceDisplayer class is responsible for displaying the fare. Its display method accepts start (indicating the upper station), end (lower station), and strategy (bus strategy), and is then calculated, the fare is displayed on the screen. There is another class, StationManager. The getStationDistance method also accepts the start, end, and strategy parameters and returns the shortest Number of sites between the two sites, the fastest Number of sites and the least number of transfer sites. PriceDisplayer. display calls StationManager. getStationDistance obtains the number of sites, calculates the fare based on a calculation method (for example, the first two, the full five stations and the first one), and finally calls a displayed api to explicitly display the fare. In its outermost layer, TicketMachine is responsible for creating PriceDisplayer and calling its display. Here we show the most common methods of Inter-object control (caller-to-called) and anti-control (called-to-caller. The caller (PriceDisplayer) controls the caller (StationManager) through parameters, and the caller controls the caller through the return value (or outgoing parameters. In terms of dependency, only the caller depends on the called user, but the called user is not aware of the caller. The called user will not change because of the change of the caller (because there is no dependency), and its creation is very simple (because it does not need to create its dependent class), and its reusability is also very strong. Relatively speaking, the caller depends on a specific class and will be changed due to changes in the interface and Implementation of the caller. However, if cooperation between objects is required, this one-way dependency is required. Then we want to add a new travel strategy with the least transfer. In this case, you can use if-else In StationManager to add judgment on the policy, and then add an enumeration type to the strategy parameter. The modification of StationManager may change the existing behavior of PriceDisplay. For example, when a new policy is added, the behavior of the original two policies is accidentally modified (in violation of OCP ). In this case, the strategy mode is used to extract an interface named StationManagerInterface. getStationDistance accepts the start and end parameters. There are three implementations for three strategies: MinStationCountStationManager, MinTimeStationManager, and MinTransferStationManager. You can use different implementations to initialize a PriceDisplayer. In this way, PriceDisplayer only depends on the Interface StationManagerInterface and does not depend on the specific implementation. If a new policy is added, the PriceDisplayer Code does not need to be changed or affect the original three policies. The following shows how callers change behavior by calling different implementations of the same interface. For static code during compilation, the caller only depends on one interface, instead of the specific class. This interface can be called as long as it is implemented by the caller during running. It is still one-way dependency, but it is dependent on the interface instead of implementation. At this time, the caller is more reusable (each of its implementations does not need to be modified, and new features only need to be added, so the user is more assured), while the caller (PriceDisplayer) because it can be implemented by the caller without any need or more flexibly by the caller (TicketMachine), the reusability is also enhanced. However, unlike the previous method, at this time, the caller (TicketMachine) must initialize the caller (PriceDisplayer) and the caller (StationManagerInterface), which makes the caller (TicketMachine) more complex. Now it is assumed that the distance algorithm of the StationManager computing site is very time-consuming and needs to be calculated on a server. Therefore, the actual operations of PriceDisplay can only be done asynchronously. In this case, one implementation method is to pass in the implementation of a Callback interface, which contains the execute Method for StationManager to call back when calculating the site distance. The following shows how to control the caller by passing in executable methods (methods can be passed in Js and python, java and c ++ can only pass in objects with implementation methods. Although it is also through parameter control, in the first method, the parameter is only static data, and in this case, the parameter is an operation. The transfer operation is not limited to callback, but the Command mode in the design mode is also the operation that passes as a Command. Through the input operation, the caller can change certain steps in the caller's process. For our example, the callback of the last step is changed, but it can also change any step in the process. For example, if you do not specify start and end in a parameter, you can specify interfaces including getStart and getEnd. For example, in the second method above, TicketMachine sets the specific StationManager to PriceDisplayer (via constructor or setter), which is also an example of passing in executable objects. this method is still one-way dependency. PriceDisplayer depends on StationManager and StationManager depends on Callback. Although Callback is defined in PriceDisplay (or PriceDisplay implements Callback), StationManager does not know. In the same scenario, PriceDisplay transmits itself to StationManager (rather than Callback implementation ). In this case, it can directly call back all methods of PriceDisplay. In this case, it is a bidirectional dependency. The caller and the called are tightly coupled. We can avoid this too tight dependency by extracting a smaller interface (method 3) or directly passing the value back as a parameter and returning the returned value (method 1.

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.