前言
開始
Step1.
避免Method的參數為Method的用法
Method
: BreakIntoSentences、Cleanup、Breakintosentences
Interface
: ITextProcessor
Class
: LinesTrimmer
Step2.
將回傳連續String的Method,拆解成物件,且物件繼承相同的Interface
Interface
: ITextProcessor
Class
: SentencesBreaker、LinesBreaker、LinesTrimmer
Step3-方法1.
建立新的Interface 「ITextProcessor」來處理這些「繼承相同的Interface」的Class
Interface
: ITextProcessor
Class
: Pipeline
Step3-方法2.
同上
Step3-方法3.
建立靜態Method來處理這些「繼承相同的Interface」的Class
Interface
: ITextProcessor
Class
: Pipeline、LinesTrimmer
Step4.
參數個別存在時”沒有意義”,所以將其重構為Class
Class
: TimedText
總覽
參考Source-Code
BreakLongLines
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
範例
1
2
3
4
5
private static IEnumerable<string> BreakIntoSentences(IEnumerable<string> text)
{
string remaining = string.Empty;
yield return remaining;
}
Cleanup
範例
1
2
3
4
5
private static IEnumerable<string> Cleanup(string[] text)
{
string remaining = string.Empty;
yield return remaining;
}
ITextProcessor
範例
1
2
3
4
internal interface ITextProcessor
{
IEnumerable<string> Execute(IEnumerable<string> text);
}
SentencesBreaker
範例
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
範例
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
範例
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
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
範例
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);
}