Features of moving objects between reconstruction methods [4]. Features of moving objects between Reconstruction Methods

Source: Internet
Author: User

Features of moving objects between reconstruction methods [4]. Features of moving objects between Reconstruction Methods

Total returned directory

Directory of this section

  • Introduce Foreign Method (introduces an additional function)

  • Introduce Local Extension (introducing Local extensions)

7 Introduce Foreign Method (introducing an additional function) Overview

You need to add a function to the class that provides services, but you cannot modify this class.

Create a function in the customer class and input a service instance as the first parameter.

Motivation

Well, I have to say this is called an extension function in C. This is actually nothing to say. It has happened too many times. Suppose you are using the string class, which basically provides the functions we need. However, you are creating a new service, and the string class does not happen to be available. At this time, do you want to modify the source code of the string and add this function. Unfortunately, we cannot do this, But C # allows us to create new extension functions.

Example

Assume that our program uses a large number of Dictionary <string, string>. To obtain the value of a Dictionary, we can do this:

var dictionary = new Dictionary<string, string>();string result;dictionary.TryGetValue("key", out result);

However, it is inconvenient to define an out parameter every time. As for how to create an extension function, please use Baidu.

public static class DictionaryExt{    public static string TryGetValue(this Dictionary<string, string> thisObj, string key)    {        if (thisObj == null || !thisObj.ContainsKey(key))            return null;        return thisObj[key] ?? "";    }}

In this way, we can use it like this:

var dictionary = new Dictionary<string, string>();var res = dictionary.TryGetValue("key");

This greatly improves our efficiency.

Summary

If the customer class only uses this function once, the extra coding work is no big deal, or even the class that originally provided the service is not required at all. However, if you need to use this function multiple times, you have to repeat the code.

Repeated code is the source of all evil software. Repeated code should be extracted and put into the same function.

8 Introduce Local Extension (introducing Local extensions) Overview

You need to provide some additional functions for the class that provides services, but you cannot modify this class.

Create a new class to include these additional functions. Make this extension a subclass of the source class.

Motivation

Class authors cannot predict the future. They often fail to prepare some useful functions for you. If you only need one or two functions, you can use Introduce Foreign Method. However, if more than two additional functions are required, it is difficult to control them. Therefore, these functions are organized together to make them child classes of the source class. This subclass is called a local extension.

Example

Take Dictionary <string, string> as an example.

Create a new DictionaryString to make it a subclass of Dictionary <string, string>.

class DictionaryString : Dictionary<string, string>{}

Add new features to the extension class, and use the Move Method to Move all the added functions to the extension class.

class DictionaryString : Dictionary<string, string>{    public string TryGetValue(Dictionary<string, string> thisObj, string key)    {        if (thisObj == null || !thisObj.ContainsKey(key))            return null;        return thisObj[key] ?? "";    }    //other methods...}

The usage is the same as the previous method:

DictionaryString dic=new DictionaryString();dic.TryGetValue("key");
Summary

Using local Extensions allows us to adhere to the principle that "functions and data should be uniformly encapsulated. If the code that should have been put in the extension class is scattered in other classes, it will only make other classes overly complicated and make the functions difficult to reuse.

Stage Summary

In the design process of objects, "deciding where to put the responsibility" is not the most important thing, but also one of the most important. We may not be able to make sure we do the right thing at the beginning. However, we can use the Move Field and Move Method to simply Move object behavior to solve these problems.

Class is often bloated because of taking too many responsibilities. In this case, you can use Extract Class to separate some of the responsibilities. If a Class becomes too "irresponsible", use Inline Class to integrate it into another Class. If one class uses another class, use Hide Delegate to Hide the relationship. Sometimes hiding the delegate class will cause frequent changes to the owner's interface. In this case, you need to use Remove Middle Man.

When you cannot access the source code of a class and want to transfer the responsibility to the class that cannot be modified, use Introduce Foreign Method and Introduce Local Extension. If one or two functions are added, Introduce Foreign Method is used. If more than one or two functions are added, Introduce Local Extension is used.

 

To Be Continued ......

 

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.