首頁 C# Refactoring Reference
文章
Cancel

C# Refactoring Reference

前言

開始

Step1.

避免Method的參數為Method的用法
Method : BreakIntoSentencesCleanupBreakintosentences
Interface : ITextProcessor
Class : LinesTrimmer
Desktop View

Step2.

將回傳連續String的Method,拆解成物件,且物件繼承相同的Interface
Interface : ITextProcessor
Class : SentencesBreakerLinesBreakerLinesTrimmer

Desktop View

Step3-方法1.

建立新的Interface 「ITextProcessor」來處理這些「繼承相同的Interface」的Class
Interface : ITextProcessor
Class : Pipeline
Desktop View

Step3-方法2.

同上
Desktop View

Step3-方法3.

建立靜態Method來處理這些「繼承相同的Interface」的Class
Interface : ITextProcessor
Class : PipelineLinesTrimmer
Desktop View

Step4.

參數個別存在時”沒有意義”,所以將其重構為Class
Class : TimedText
Desktop View

總覽

Desktop View

參考Source-Code

BreakLongLines

To Step1

1
2
3
4
5
6
private static IEnumerable<string> BreakLongLines(
    IEnumerable<string> text, int maxLineCharacters, int minBrokenLength)
{
    string remaining = string.Empty;
    yield return remaining;
}

BreakIntoSentences

To Step1

範例

1
2
3
4
5
private static IEnumerable<string> BreakIntoSentences(IEnumerable<string> text)
{
    string remaining = string.Empty;
    yield return remaining;
}

Cleanup

To Step1

範例

1
2
3
4
5
private static IEnumerable<string> Cleanup(string[] text)
{
    string remaining = string.Empty;
    yield return remaining;
}

ITextProcessor

To Step1 To Step2 To Step3

範例

1
2
3
4
internal interface ITextProcessor
{
    IEnumerable<string> Execute(IEnumerable<string> text);
}

SentencesBreaker

To Step2

範例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
internal interface ITextProcessor
{
    IEnumerable<string> Execute(IEnumerable<string> text);
}
class SentencesBreaker : ITextProcessor
{
    public IEnumerable<string> Execute(IEnumerable<string> text)
    {
        string remaining = string.Empty;
        yield return remaining;
    }

    private IEnumerable<string> BreakSentences(string text)
    {
        string remaining = text.Trim();
        yield return remaining;
    }
}

LinesBreaker

To Step2

範例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
internal interface ITextProcessor
{
    IEnumerable<string> Execute(IEnumerable<string> text);
}
class LinesBreaker : ITextProcessor
{
    private int MaxLineLength { get; }
    private int MinLengthToBreakInto { get; }

    public LinesBreaker(int maxLineLength, int minLengthToBreakInto)
    {
        this.MaxLineLength = maxLineLength;
        this.MinLengthToBreakInto = minLengthToBreakInto;
    }
    public IEnumerable<string> Execute(IEnumerable<string> text) 
    {
        string remaining = text.Trim();
        yield return remaining;
    }

    public IEnumerable<string> BreakLongLine(string line)
    {
        string remaining = line.Trim();
        yield return remaining;
    }

}

LinesTrimmer

To Step1 To Step2 To Step3

範例

1
2
3
4
5
6
7
8
9
class LinesTrimmer : ITextProcessor
{
    public IEnumerable<string> Execute(IEnumerable<string> text)  
    {
        string remaining = text.Trim();
        yield return remaining;
    }

}

Pipeline

To Step3 To Step3 範例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
internal interface ITextProcessor
{
    IEnumerable<string> Execute(IEnumerable<string> text);
}
class Pipeline : ITextProcessor
{
    private IEnumerable<ITextProcessor> Processors { get; }

    public Pipeline(IEnumerable<ITextProcessor> processors)
    {
        this.Processors = processors.ToList();
    }

    public Pipeline(params ITextProcessor[] processors)
        : this((IEnumerable<ITextProcessor>) processors)
    {
    }

    public IEnumerable<string> Execute(IEnumerable<string> text) =>
        this.Processors.Aggregate(text, (current, processor) => processor.Execute(current));
}

TimedText

To Step4

範例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class TimedText
{
    public IEnumerable<string> Content { get; }
    public TimeSpan Duration { get; }

    public TimedText(IEnumerable<string> content, TimeSpan duration)
    {
        this.Content = content;
        this.Duration = duration;
    }

    public TimedText Apply(ITextProcessor processor) =>
        new TimedText(processor.Execute(this.Content), this.Duration);
}
本文由作者按照 CC BY 4.0 進行授權