呵呵,到了高潮部分了,我們先寫一段測試案例來測試組合流水號:
TTestCompositeSerialNumberGenerator
1procedure TTestCompositeSerialNumberGenerator.SetUp;
2begin
3 inherited;
4 fGenerator := TSerialNumberGenerator.Create([
5 TConstantCodeSerialNumberGenerator.Create('KA'),
6 TNumbericSerialNumberGenerator.Create('001', '999'),
7 TLetterSerialNumberGenerator.Create,
8 TNumbericSerialNumberGenerator.Create('001', '999')
9 ]);
10end;
11
12procedure TTestCompositeSerialNumberGenerator.TestNextSerialNumber;
13begin
14 CheckEquals('KA001A002', fGenerator.NextSerialNumber('KA001A001'));
15 CheckEquals('KA001B001', fGenerator.NextSerialNumber('KA001A999'));
16 CheckEquals('KA001Z002', fGenerator.NextSerialNumber('KA001Z001'));
17 CheckEquals('KA002A001', fGenerator.NextSerialNumber('KA001Z999'));
18end
再實現TSerialNumberGenerator:
TSerialNumberGenerator
1constructor TSerialNumberGenerator.Create(
2 const generators: arrayof ISerialNumberGenerator);
3var
4 i: Integer;
5begin
6 inherited Create;
7 fList := TInterfaceList.Create;
8 for i :=0to High(generators) do
9 begin
10 fList.Add(generators[i]);
11 end;
12end;
13
14destructor TSerialNumberGenerator.Destroy;
15begin
16 fList.Free;
17 inherited;
18end;
19
20function TSerialNumberGenerator.DoNextSerialNumber(
21 const serialNumber: string): string;
22begin
23
24end;
25
26function TSerialNumberGenerator.Validate(const serialNumber: string): Boolean;
27var
28 i: Integer;
29begin
30 for i :=0to fList.Count -1do
31 begin
32
33 end;
34end