Refactoring Method 64: extract interface (extraction Interface)

Source: Internet
Author: User

Several customers use the same subset of the class interface, or the interfaces of the two classes are the same.Extracts the same subset into an independent interface.

Motivation: There are several ways of mutual use between classes. "Using a class" usually means that all the responsibility zones of the class are used. In another case, a group of customers only use a specific subset of the class responsibility area. In another case, this class needs to work with all classes that assist in processing certain requests.

In the latter two cases, it is usually meaningful to separate the actually used part of the responsibility, because this can make the system usage clearer and make it easier to see the responsibility division of the system. If the new class needs to support the previous subset, you can also see something in the subset.

In many object-oriented languages, this division of responsibility is achieved through multi-inheritance. In C #, you can use interfaces to demonstrate and implement the preceding requirements.

Extract subclassThere are some similarities between (abstraction subclass) and extract interface (abstraction interface. Extract interface (extract Interface) can only extract common interfaces, but cannot extract common interfacesCode. The use of extract interface (extract Interface) may cause an unpleasant "DUPLICATE" bad taste. Fortunately, you can use extract class (extract class) to put the common behavior into a component first, then, assign the work to the component to solve the problem. If there are many common behaviors, extract superclass (extract super class) is relatively simple, but each class can only have one super class.

If a class plays different roles in different environments, it is a good idea to use interfaces. You can extract the corresponding interfaces for each role using the extract interface. Another scenario where extract interface (extract Interface) can be used is: You want to describe the external dependent interface of a class. If you want to add other types of service objects in the future. You only need to require them to implement this interface.

 

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.