在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
{没有应用状态模式的代码}
//工程文件 program Project1; {$APPTYPE CONSOLE} uses var aGumballMachine.InsertQuarter; Writeln; aGumballMachine.InsertQuarter; Writeln; aGumballMachine.InsertQuarter; Writeln; aGumballMachine.InsertQuarter; aGumballMachine.Free; Readln; //单元文件 unit uGumballMachine; interface type implementation const { TGumballMachine } constructor TGumballMachine.Create(aCount: Integer); procedure TGumballMachine.Dispense; if FState = NO_QUARTER then if FState = SOLD_OUT then if FState = HAS_QUARTER then procedure TGumballMachine.EjectQuarter; if FState = NO_QUARTER then if FState = SOLD then if FState = SOLD_OUT then procedure TGumballMachine.InsertQuarter; if FState = NO_QUARTER then if FState = SOLD_OUT then if FState = SOLD then procedure TGumballMachine.TurnCrank; if FState = NO_QUARTER then if FState = SOLD_OUT then if FState = HAS_QUARTER then end.
{应用状态模式的代码} //工程文件 program Project1; {$APPTYPE CONSOLE} uses var aGumballMachine.InsertQuarter; Writeln; aGumballMachine.InsertQuarter; Writeln; aGumballMachine.InsertQuarter; Writeln; aGumballMachine.InsertQuarter; aGumballMachine.Free; Readln; //单元文件 unit uGumballMachine; interface type TState = class(TObject) TNoQuarterState = class(TState) THasQuarterState = class(TState) TSoldState = class(TState) TSoldOutState = class(TState) implementation { TNoQuarterState } constructor TNoQuarterState.Create(aGumballMachine: TGumballMachine); procedure TNoQuarterState.Dispense; procedure TNoQuarterState.EjectQuarter; procedure TNoQuarterState.InsertQuarter; procedure TNoQuarterState.TurnCrank; { THasQuarterState } constructor THasQuarterState.Create(aGumballMachine: TGumballMachine); procedure THasQuarterState.Dispense; procedure THasQuarterState.EjectQuarter; procedure THasQuarterState.InsertQuarter; procedure THasQuarterState.TurnCrank; { TSoldState } constructor TSoldState.Create(aGumballMachine: TGumballMachine); procedure TSoldState.Dispense; procedure TSoldState.EjectQuarter; procedure TSoldState.InsertQuarter; procedure TSoldState.TurnCrank; { TSoldOutState } constructor TSoldOutState.Create(aGumballMachine: TGumballMachine); procedure TSoldOutState.Dispense; procedure TSoldOutState.EjectQuarter; procedure TSoldOutState.InsertQuarter; procedure TSoldOutState.TurnCrank; { TGumballMachine } constructor TGumballMachine.Create(aNumberGumballs: Integer); destructor TGumballMachine.Destroy; procedure TGumballMachine.EjectQuarter; function TGumballMachine.GetCount: Integer; function TGumballMachine.GetHasQuarterState: TState; function TGumballMachine.GetNoQuarterState: TState; function TGumballMachine.GetSoldOutState: TState; function TGumballMachine.GetSoldState: TState; procedure TGumballMachine.InsertQuarter; procedure TGumballMachine.ReleaseBall; procedure TGumballMachine.SetState(aState: TState); procedure TGumballMachine.TurnCrank; end. //运行结果
//工程文件 program Project1; {$APPTYPE CONSOLE} uses var aGumballMachine.InsertQuarter; Writeln; aGumballMachine.InsertQuarter; Writeln; aGumballMachine.InsertQuarter; Writeln; aGumballMachine.InsertQuarter; aGumballMachine.Free; Readln; //单元文件 unit uGumballMachine; interface type implementation const { TGumballMachine } constructor TGumballMachine.Create(aCount: Integer); procedure TGumballMachine.Dispense; if FState = NO_QUARTER then if FState = SOLD_OUT then if FState = HAS_QUARTER then procedure TGumballMachine.EjectQuarter; if FState = NO_QUARTER then if FState = SOLD then if FState = SOLD_OUT then procedure TGumballMachine.InsertQuarter; if FState = NO_QUARTER then if FState = SOLD_OUT then if FState = SOLD then procedure TGumballMachine.TurnCrank; if FState = NO_QUARTER then if FState = SOLD_OUT then if FState = HAS_QUARTER then end.
{应用状态模式的代码} //工程文件 program Project1; {$APPTYPE CONSOLE} uses var aGumballMachine.InsertQuarter; Writeln; aGumballMachine.InsertQuarter; Writeln; aGumballMachine.InsertQuarter; Writeln; aGumballMachine.InsertQuarter; aGumballMachine.Free; Readln; //单元文件 unit uGumballMachine; interface type TState = class(TObject) TNoQuarterState = class(TState) THasQuarterState = class(TState) TSoldState = class(TState) TSoldOutState = class(TState) implementation { TNoQuarterState } constructor TNoQuarterState.Create(aGumballMachine: TGumballMachine); procedure TNoQuarterState.Dispense; procedure TNoQuarterState.EjectQuarter; procedure TNoQuarterState.InsertQuarter; procedure TNoQuarterState.TurnCrank; { THasQuarterState } constructor THasQuarterState.Create(aGumballMachine: TGumballMachine); procedure THasQuarterState.Dispense; procedure THasQuarterState.EjectQuarter; procedure THasQuarterState.InsertQuarter; procedure THasQuarterState.TurnCrank; { TSoldState } constructor TSoldState.Create(aGumballMachine: TGumballMachine); procedure TSoldState.Dispense; procedure TSoldState.EjectQuarter; procedure TSoldState.InsertQuarter; procedure TSoldState.TurnCrank; { TSoldOutState } constructor TSoldOutState.Create(aGumballMachine: TGumballMachine); procedure TSoldOutState.Dispense; procedure TSoldOutState.EjectQuarter; procedure TSoldOutState.InsertQuarter; procedure TSoldOutState.TurnCrank; { TGumballMachine } constructor TGumballMachine.Create(aNumberGumballs: Integer); destructor TGumballMachine.Destroy; procedure TGumballMachine.EjectQuarter; function TGumballMachine.GetCount: Integer; function TGumballMachine.GetHasQuarterState: TState; function TGumballMachine.GetNoQuarterState: TState; function TGumballMachine.GetSoldOutState: TState; function TGumballMachine.GetSoldState: TState; procedure TGumballMachine.InsertQuarter; procedure TGumballMachine.ReleaseBall; procedure TGumballMachine.SetState(aState: TState); procedure TGumballMachine.TurnCrank; end. //运行结果
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论