Features of moving objects between reconstruction techniques [2]. Features of moving reconstruction techniques between objects

Source: Internet
Author: User

Features of moving objects between reconstruction techniques [2]. Features of moving reconstruction techniques between objects

Total returned directory

Directory of this section

  • Extract Class (extraction Class)
  • Inline Class (associating classes)
3 Extract Class (Extract Class) Overview

A class does something that should be done by two classes.

Create a new class to move related fields and functions from the old class to the new class.

Motivation

If a class contains a large amount of functions and data, this class is too big and hard to understand. In this case, you need to consider which parts can be separated and separate them into a separate class.

If subclass only affects some features of a class, or if some features need to be subclass in one way, some features need to be subclass in another way, this also means that the original class needs to be decomposed.

Example

Let's first look at a simple Person class.

class Person{    public string Name { get; set; }    public string OfficeAreaCode { get; set; }    public string OfficeNumber { get; set; }    public string GetTelephoneNumber()    {        return $"({OfficeAreaCode})-{OfficeNumber}";    }}

Here, we can separate the behaviors related to phone numbers into a separate class.

First, define a TelePhoneNumber class to indicate "phone number ":

class TelePhoneNumber{}

Then, establish a connection from Person to TelePhoneNumber in the Person class:

 private TelePhoneNumber _phoneNumber = new TelePhoneNumber();

Now we use the Move Field to Move the property and the Move Method to Move the relevant function to the TelePhoneNumber class.

class Person{    private TelePhoneNumber _phoneNumber = new TelePhoneNumber();    public string Name { get; set; }    public string GetTelephoneNumber()    {        return _phoneNumber.GetTelephoneNumber();    }}
class TelePhoneNumber{    public string OfficeAreaCode { get; set; }    public string OfficeNumber { get; set; }    public string GetTelephoneNumber()    {        return $"({OfficeAreaCode})-{OfficeNumber}";    }}
Summary

The single principle of class indicates that a class should be a clear abstraction to deal with specific responsibilities. If a class has two classes, it should be refined.

4. Summary of Inline Class

A class does not do much.

Move all features of this class to another class, and then remove the original class.

Motivation

Inline Class and Extract Class are the opposite. If a class is no longer responsible enough and there is no longer a reason for its own existence, it will be inserted into another class.

Example

In section 3rd, we extract a TelePhoneNumber class, And now we will insert it back to Person.

class Person{    private TelePhoneNumber _phoneNumber = new TelePhoneNumber();    public string Name { get; set; }    public string GetTelephoneNumber()    {        return _phoneNumber.GetTelephoneNumber();    }}class TelePhoneNumber{    public string OfficeAreaCode { get; set; }    public string OfficeNumber { get; set; }    public string GetTelephoneNumber()    {        return $"({OfficeAreaCode})-{OfficeNumber}";    }}

First, declare all public functions of TelePhoneNumber in Person.

class Person{    private TelePhoneNumber _phoneNumber = new TelePhoneNumber();    public string Name { get; set; }    public string OfficeAreaCode { get; set; }    public string OfficeNumber { get; set; }    public string GetTelephoneNumber()    {        return _phoneNumber.GetTelephoneNumber();    }    public TelePhoneNumber GetOfficeTelePhoneNumber()    {        return _phoneNumber;    }}

Now, we need to find all the places where TelePhoneNumber is used and let them use the Person interface. The following code:

Person martin=new Person();martin.GetOfficeTelePhoneNumber().OfficeAreaCode = "781";

It becomes:

Person martin=new Person();martin.OfficeAreaCode = "781";

Finally, we use the Move Method and Move Field repeatedly until the TelePhoneNumber class no longer exists.

Summary

If a Class is idle, this is a bad taste-Lazy Class. When encountering this type, we should not hesitate to hold a "funeral" for it and stuff it into other classes.

 

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.