(#--------------------程式頭-------------------------------------------------------------) procedure TForm1.Button3Click(Sender: TObject); begin CallTooManyIfMethod(GetCondition); end; procedure TForm1.CallTooManyIfMethod(const sCondition: string); begin if (sCondition = 'ABC') then begin DoSomeThing; end else if (sCondition = 'DEF') then begin DoSomeThing; end else if (sCondition = 'GHI') then begin DoSomeThing; end else if (sCondition = 'JKL') then begin DoSomeThing; end; ... end; (#--------------------程式尾------------------------------) (#--------------------程式頭-------------------------------------------------------------) procedure TForm1.btnCalculateTaxClick(Sender: TObject); begin if (Self.cbTaxKind.Text = '自用') then ShowTax(GetPrivateTax(StrToInt(edtCC.Text))) else ShowTax(GetBusinessTax(StrToInt(edtCC.Text))); end; function TForm1.GetBusinessTax(const CC : Integer) : Integer; begin Result := 56700; if (cc < 500) then Result := 900 else if (cc < 600) then Result := 1200 else if (cc < 1200) then Result := 2160 else if (cc < 1800) then Result := 3060 else if (cc < 2400) then Result := 6480 else if (cc < 3000) then Result := 9900 else if (cc < 4200) then Result := 16380 else if (cc < 5400) then Result := 24300 else if (cc < 6600) then Result := 33660 else if (cc < 7800) then Result := 44460; end; function TForm1.GetPrivateTax(const CC : Integer) : Integer; begin Result := 151200; if (cc < 500) then Result := 1620 else if (cc < 600) then Result := 1620 else if (cc < 1200) then Result := 4320 else if (cc < 1800) then Result := 7120 else if (cc < 2400) then Result := 11230 else if (cc < 3000) then Result := 15210 else if (cc < 4200) then Result := 28220 else if (cc < 5400) then Result := 46170 else if (cc < 6600) then Result := 69690 else if (cc < 7800) then Result := 117000; end; (#--------------------程式尾-----------------------------------) (#--------------------程式頭-------------------------------------------------------------) Case AMessage Of wm_Destroy: Begin PostQuitMessage (0); Exit; End; wm_Command: Begin … End; Wm_MOUSEMOVE : … End; WindowProc := DefWindowProc(Window,AMessage,Wparam,Lparam); End; (#--------------------程式尾----------------------------------------) (#--------------------程式頭----------------------------------) const CAR_BASE = $BD00; CAR_PRANGE1 = CAR_BASE+ 1; …(#--------------------程式尾----------------------------------) (#--------------------程式頭----------------------------------) SendMessage(Self.Handle, GetCarRange(StrToInt(edtCC.Text)), 0, 0); (#--------------------程式頭----------------------------------) * 撰寫訊息處理函式 (#--------------------程式頭----------------------------------) procedure TForm1.CARPRANGE1(var Message: TMessage); begin ShowTax(pTaxArray[Message.Msg - CAR_BASE]); end; (#--------------------程式尾---------------------------------) * 藉由Dispatcher設計樣例把訊息ID和處理連結起來即可,例如在Delphi/C++Builder中可以直接使用message關鍵字來定義 (#--------------------程式頭----------------------------------) procedure CARPRANGE1(var Message: TMessage); message CAR_PRANGE1;   (#--------------------程式尾---------------------------------) (#--------------------程式頭-------------------------------------------------------------) procedure TForm1.DoMethod2; begin if (Self.cbTaxKind.Text = '自用') then SendMessage(Self.Handle, GetCarRange(StrToInt(edtCC.Text)), 0, 0) else SendMessage(Self.Handle, GetCarRange(StrToInt(edtCC.Text)) + bOffset, 0, 0); end; function TForm1.GetCarRange(const iCC: Integer): Integer; var idx : Integer; begin idx := 1; while (iCC > iCCArray[idx]) do Inc(idx); Result := CAR_BASE + idx; end; (#--------------------程式尾--------------------------------------) 最後在每一個訊息處理函式中只需要根據牌照稅的級距回傳相對的費用即可: (#--------------------程式頭-------------------------------------------------------------) procedure TForm1.CARPRANGE1(var Message: TMessage); begin ShowTax(pTaxArray[Message.Msg - CAR_BASE]); end; (#--------------------程式尾--------------------------------) (#--------------------程式頭-------------------------------------------------------------) ITax = interface ['{F74E913E-3526-4A42-97DC-ACFA3824A8C3}'] procedure calculateTax(iCC : Integer); end; (#--------------------程式尾----------------------------------)   (#--------------------程式頭-------------------------------------------------------------) TCreator = class(TObject) private iRef : Integer; public class function getBusinessTax : Integer; class function getPrivateTax : Integer; function factoryMethod(taxType : Integer) : ITax; virtual; abstract; end; (#--------------------程式尾----------------------------------)   (#--------------------程式頭-------------------------------------------------------------) TConcreteCreator = class(TCreator) private prTax : TPrivateTax; buTax : TBusinessTax; function ReturnBusinessTax: ITax; function ReturnPrivateTax: ITax; public Destructor Destroy; override; function factoryMethod(taxType : Integer) : ITax; override; end; implementation { TConcreteCreator } destructor TConcreteCreator.Destroy; begin FreeAndNil(prTax); FreeAndNil(buTax); inherited; end; function TConcreteCreator.factoryMethod(taxType: Integer): ITax; begin Result := nil; if (taxType = TCreator.getBusinessTax) then Result := ReturnBusinessTax else if (taxType = TCreator.getPrivateTax) then Result := ReturnPrivateTax; end; function TConcreteCreator.ReturnBusinessTax: ITax; begin if (not Assigned(buTax)) then buTax := TBusinessTax.Create; Result := ITax(buTax); end; function TConcreteCreator.ReturnPrivateTax: ITax; begin if (not Assigned(prTax)) then prTax := TPrivateTax.Create; Result := ITax(prTax); end; (#--------------------程式尾----------------------------------)   (#--------------------程式頭-------------------------------------------------------------) procedure TForm1.DoMethod3; var iKind : Integer; tax : ITax; begin if (Self.cbTaxKind.Text = '自用') then iKind := TCreator.getPrivateTax else iKind := TCreator.getBusinessTax; tax := taxCreator.factoryMethod(iKind); tax.calculateTax(StrToInt(edtCC.Text)); end; (#--------------------程式尾----------------------------------)