您好,欢迎来到筏尚旅游网。
搜索
您的当前位置:首页《重构》:Extract Class and Inline Class

《重构》:Extract Class and Inline Class

来源:筏尚旅游网

hey,gays lets go on to refator those sh!t .

i fork a rep, this the link

all right, lets see the genel description.

提取类

对立:内联类

目的:将大类分成小类

场景:维护一个大量函数和数据的类

class Person{
  get officeAreaCode(){
    return this._officeAreaCode;
  }
  get officeNumber(){
    return this._officeNumber;
  }
}
class Person{
  constructor(telephoneNumber){
    this._telephoneNumber = telephoneNumber;
  }
  get officeAreaCode(){
    return this._telephoneNumber.areaCode;
  }
  get officeNumber(){
    return this._telephoneNumber.number;
  }
}
class TelephoneNumber{
  get areaCode(){
    return this._areaCode;
  }
  get number(){
    return this._number;
  }
}
内联类

对立:提炼类

目的:减少不必要的类

场景:

如果一个类不再承担足够的责任
重新分类两个类的不同职责

class Person{
  constructor(telephoneNumber){
    this._telephoneNumber = telephoneNumber;
  }
  get officeAreaCode(){
    return this._telephoneNumber.areaCode;
  }
  get officeNumber(){
    return this._telephoneNumber.number;
  }
}
class TelephoneNumber{
  get areaCode(){
    return this._areaCode;
  }
  get number(){
    return this._number;
  }
}
class Person{
  get officeAreaCode(){
    return this._officeAreaCode;
  }
  get officeNumber(){
    return this._officeNumber;
  }
}

我在阅读重构的时候,对内联类和提取类有一些疑问,他们看起来是对立的,那么在实际的代码中,我该如何选择呢?给我一些具体的例子?

有代码实例吗?

当然,以下是一些具体的C#代码示例,以帮助您更好地理解内联类和提取类:

**内联类的示例:**

public class Person {
    public string Name { get; set; }
    public Address Address { get; set; }

    public void PrintAddress() {
        Console.WriteLine("Address: " + this.Address.ToString());
    }

    // other methods
}

public class Address {
    public string Street { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }

    public override string ToString() {
        return $"{this.Street}, {this.City}, {this.State} {this.ZipCode}";
    }
}

在上面的代码中,`Person` 类只使用了一次 `Address` 属性,并且只有一个方法使用了这个属性。因此,我们可以将 `Address` 类内联到 `Person` 类中,使代码更加简洁:

public class Person {
    public string Name { get; set; }
    public string Street { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }

    public void PrintAddress() {
        Console.WriteLine($"Address: {this.Street}, {this.City}, {this.State} {this.ZipCode}");
    }

    // other methods
}

**提取类的示例:**

public class Order {
    public int OrderId { get; set; }
    public string CustomerName { get; set; }
    public string CustomerAddress { get; set; }
    public List<OrderItem> Items { get; set; }

    public void ProcessOrder() {
        // process the order
    }

    // other methods
}

在上面的代码中,`Order` 类包含了订单信息和订单处理的逻辑。如果我们将订单处理逻辑提取成一个的类 `OrderProcessor`,那么代码会更加清晰,并且每个类都只需要关注自己的职责:

public class Order {
    public int OrderId { get; set; }
    public string CustomerName { get; set; }
    public string CustomerAddress { get; set; }
    public List<OrderItem> Items { get; set; }

    // other methods
}

public class OrderProcessor {
    public void ProcessOrder(Order order) {
        // process the order
    }
}

希望这些示例可以帮助您更好地理解内联类和提取类的使用场景。

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- efsc.cn 版权所有 赣ICP备2024042792号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务