From 01f490da0e54a5d3531555c3d1961e816e6c9428 Mon Sep 17 00:00:00 2001 From: jc Date: Wed, 28 Feb 2024 21:57:05 +0800 Subject: [PATCH] change name --- Docs/README-en.md | 50 +++++++++---------- Docs/README-tw.md | 22 ++++---- .../TestCallSwitch.cs | 6 +-- PinionCyber.StateManagement.Tests/Tests.cs | 12 ++--- PinionCyber.StateManagement/ActiverChanger.cs | 40 +++++++++++++++ PinionCyber.StateManagement/IActivable.cs | 8 +++ PinionCyber.StateManagement/IState.cs | 2 +- PinionCyber.StateManagement/ISwitch.cs | 8 --- .../PinionCyber.StateManagement.csproj | 12 +++-- PinionCyber.StateManagement/README.md | 2 - PinionCyber.StateManagement/StateMachine.cs | 8 +-- PinionCyber.StateManagement/Switcher.cs | 28 ----------- 12 files changed, 106 insertions(+), 92 deletions(-) create mode 100644 PinionCyber.StateManagement/ActiverChanger.cs create mode 100644 PinionCyber.StateManagement/IActivable.cs delete mode 100644 PinionCyber.StateManagement/ISwitch.cs delete mode 100644 PinionCyber.StateManagement/README.md delete mode 100644 PinionCyber.StateManagement/Switcher.cs diff --git a/Docs/README-en.md b/Docs/README-en.md index ab0fad7..9167ba2 100644 --- a/Docs/README-en.md +++ b/Docs/README-en.md @@ -13,14 +13,14 @@ namespace PinionCyber.StateManagement.Sample1 { class StateA : PinionCyber.StateManagement.IState { - void ISwitch.End() + void IActivable.Disbale() { - // State ends + // State Disbales } - void ISwitch.Start() + void IActivable.Enable() { - // State starts + // State Enables } void IUpdate.Update() @@ -40,14 +40,14 @@ namespace PinionCyber.StateManagement.Sample1 var machine = new PinionCyber.StateManagement.StateMachine(); - // Switching states + // Changing states var stateA = new StateA(); - machine.Switch(stateA); + machine.Change(stateA); var stateB = new StateB(); // Another state - machine.Switch(stateB); + machine.Change(stateB); // Updating state - machine.Active().Update(); + machine.Activer().Update(); // Clearing states machine.Empty(); @@ -57,8 +57,8 @@ namespace PinionCyber.StateManagement.Sample1 ## Cookbook State machines are a design pattern. The above code handles the core part of a state machine. To fulfill a project's requirements, some control handling is needed. Below are the ways to use a state machine. -### State Switching -Use events to output the result of a state. Below is the switching between StateA and StateB. +### State Changeing +Use events to output the result of a state. Below is the Changeing between StateA and StateB. ```csharp class StateA : IState @@ -77,7 +77,7 @@ class StateB : IState public event System.Action DoneEvent; } ``` -Switch between two states using ```DoneEvent```. +Change between two states using ```DoneEvent```. ```csharp class Sample @@ -86,22 +86,22 @@ class Sample public Sample() { _Machine = new PinionCyber.StateManagement.StateMachine(); - // First switch to StateA + // First Change to StateA _ToStateA(); } void _ToStateA() { var state = new StateA(); - state.DoneEvent += _ToStateB; // When StateA completes, switch to StateB - _Machine.Switch(state); + state.DoneEvent += _ToStateB; // When StateA completes, Change to StateB + _Machine.Change(state); } void _ToStateB() { var state = new StateB(); - state.DoneEvent += _ToStateA; // When StateB completes, switch back to StateA - _Machine.Switch(state); + state.DoneEvent += _ToStateA; // When StateB completes, Change back to StateA + _Machine.Change(state); } } ``` @@ -156,18 +156,18 @@ class Sample _Machine = new PinionCyber.StateManagement.StateMachine(); } - // This method makes it easy to start the initial state after event registration + // This method makes it easy to Enable the initial state after event registration // Or the class Sample itself can also be a state. - public void Start() + public void Enable() { - // First switch to StateA + // First Change to StateA _ToStateA(); } void _ToStateA() { var state = new StateA(); - state.DoneEvent += _ToStateB; // When StateA completes, switch to StateB - _Machine.Switch(state); + state.DoneEvent += _ToStateB; // When StateA completes, Change to StateB + _Machine.Change(state); // State notification AccessibleEvent?.Invoke(state); } @@ -175,8 +175,8 @@ class Sample void _ToStateB() { var state = new StateB(); - state.DoneEvent += _ToStateA; // When StateB completes, switch back to StateA - _Machine.Switch(state); + state.DoneEvent += _ToStateA; // When StateB completes, Change back to StateA + _Machine.Change(state); // State notification GetterEvent?.Invoke(state); } @@ -205,9 +205,9 @@ class Controller _CommandHandler = _CreateHandler(getter); }; } - public void Start() + public void Enable() { - _Sample.Start(); + _Sample.Enable(); } // Get state public SampleState GetSampleState() diff --git a/Docs/README-tw.md b/Docs/README-tw.md index 1371077..ac2ac90 100644 --- a/Docs/README-tw.md +++ b/Docs/README-tw.md @@ -12,12 +12,12 @@ namespace PinionCyber.StateManagement.Sample1 { class StateA : PinionCyber.StateManagement.IState { - void ISwitch.End() + void IActivable.Disable() { // ª¬ºAµ²§ô } - void ISwitch.Start() + void IActivable.Enable() { // ª¬ºA°_©l } @@ -41,9 +41,9 @@ namespace PinionCyber.StateManagement.Sample1 // ª¬ºA¤Á´« var stateA = new StateA(); - machine.Switch(stateA); + machine.Change(stateA); var stateB = new StateB(); // ¥t¤@­Óª¬ºA - machine.Switch(stateB); + machine.Change(stateB); // ª¬ºA§ó·s machine.Activer().Update(); @@ -91,14 +91,14 @@ class Sample { var state = new StateA(); state.DoneEvent += _ToStateB; // ·í StateA §¹¦¨«h¤Á´«¨ì StateB - _Machine.Switch(state); + _Machine.Change(state); } void _ToStateB() { var state = new StateB(); state.DoneEvent += _ToStateA;// ·í StateB §¹¦¨«h¤Á´«¨ì StateA - _Machine.Switch(state); + _Machine.Change(state); } } ``` @@ -153,7 +153,7 @@ class Sample } // ³Ð«Ø³oµÛ¤èªk¤è«K¨Æ¥óµù¥U§¹«á±Ò°Êªì©lª¬ºA // ©ÎªÌ class Sample ¥»¨­¤]¥i¥H¬O­Óª¬ºA. - public void Start() + public void Enable() { // ­º¥ý¥ý¤Á¨ì StateA _ToStateA(); @@ -162,7 +162,7 @@ class Sample { var state = new StateA(); state.DoneEvent += _ToStateB; // ·í StateA §¹¦¨«h¤Á´«¨ì StateB - _Machine.Switch(state); + _Machine.Change(state); // ª¬ºA³qª¾ AccessibleEvent(state); } @@ -171,7 +171,7 @@ class Sample { var state = new StateB(); state.DoneEvent += _ToStateA;// ·í StateB §¹¦¨«h¤Á´«¨ì StateA - _Machine.Switch(state); + _Machine.Change(state); // ª¬ºA³qª¾ GetterEvent(state); } @@ -199,9 +199,9 @@ class Controller _CommandHandler = _CreateHandler(getter); }; } - public void Start() + public void Enable() { - _Sample.Start(); + _Sample.Enable(); } // ¨ú±oª¬ºA public SampleState GetSampleState() diff --git a/PinionCyber.StateManagement.Tests/TestCallSwitch.cs b/PinionCyber.StateManagement.Tests/TestCallSwitch.cs index fd1c093..c23d32b 100644 --- a/PinionCyber.StateManagement.Tests/TestCallSwitch.cs +++ b/PinionCyber.StateManagement.Tests/TestCallSwitch.cs @@ -1,16 +1,16 @@ namespace PinionCyber.StateManagement.Tests { - class TestCallSwitch : ISwitch + class TestCallSwitch : IActivable { public int EndCallCount; public int StartCallCount; - void ISwitch.End() + void IActivable.Disable() { EndCallCount++; } - void ISwitch.Start() + void IActivable.Enable() { StartCallCount++; } diff --git a/PinionCyber.StateManagement.Tests/Tests.cs b/PinionCyber.StateManagement.Tests/Tests.cs index f792b89..7ad8cba 100644 --- a/PinionCyber.StateManagement.Tests/Tests.cs +++ b/PinionCyber.StateManagement.Tests/Tests.cs @@ -10,12 +10,12 @@ public void Setup() } [Test] - public void Switch() + public void ActiverChanger() { var state = new TestCallSwitch(); - var controller = new Switcher(new TestCallSwitch()); - controller.Switch(state); - controller.Switch(new TestCallSwitch()); + var controller = new ActiverChanger(new TestCallSwitch()); + controller.Change(state); + controller.Change(new TestCallSwitch()); Assert.That(1,Is.EqualTo(state.EndCallCount)); @@ -23,11 +23,11 @@ public void Switch() } [Test] - public void StateWithEmpty() + public void StateMachine() { var state = new TestCallState(); var machine = new StateMachine(); - machine.Switch(state); + machine.Change(state); machine.Activer().Update(); machine.Empty(); Assert.That(1, Is.EqualTo(state.EndCallCount)); diff --git a/PinionCyber.StateManagement/ActiverChanger.cs b/PinionCyber.StateManagement/ActiverChanger.cs new file mode 100644 index 0000000..187c279 --- /dev/null +++ b/PinionCyber.StateManagement/ActiverChanger.cs @@ -0,0 +1,40 @@ +using System; + +namespace PinionCyber.StateManagement +{ + public class ActiverChanger where T : IActivable + { + T _Activable; + /// + /// An empty implementation is usually passed here. + /// This constructor calls the IActivable.Enable on the parameter. + /// + /// + public ActiverChanger(T first) + { + _Activable = first; + _Activable.Enable(); + } + + /// + /// Changes the current activable object to the provided one and manages their active states. + /// It disables the current activable object before switching to the new one, which is then enabled. + /// + /// The new activable object to switch to. + public void Change(T activer) + { + _Activable.Disable(); + _Activable = activer; + _Activable.Enable(); + } + + /// + /// Returns the current activable object. + /// + /// The current activable object. + public T Activer() + { + return _Activable; + } + } +} diff --git a/PinionCyber.StateManagement/IActivable.cs b/PinionCyber.StateManagement/IActivable.cs new file mode 100644 index 0000000..0de57c7 --- /dev/null +++ b/PinionCyber.StateManagement/IActivable.cs @@ -0,0 +1,8 @@ +namespace PinionCyber.StateManagement +{ + public interface IActivable + { + void Enable(); + void Disable(); + } +} diff --git a/PinionCyber.StateManagement/IState.cs b/PinionCyber.StateManagement/IState.cs index 6541cc3..1051a2a 100644 --- a/PinionCyber.StateManagement/IState.cs +++ b/PinionCyber.StateManagement/IState.cs @@ -2,7 +2,7 @@ namespace PinionCyber.StateManagement { - public interface IState : ISwitch , IUpdate + public interface IState : IActivable , IUpdate { } } diff --git a/PinionCyber.StateManagement/ISwitch.cs b/PinionCyber.StateManagement/ISwitch.cs deleted file mode 100644 index 38b6098..0000000 --- a/PinionCyber.StateManagement/ISwitch.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace PinionCyber.StateManagement -{ - public interface ISwitch - { - void Start(); - void End(); - } -} diff --git a/PinionCyber.StateManagement/PinionCyber.StateManagement.csproj b/PinionCyber.StateManagement/PinionCyber.StateManagement.csproj index 6845a65..7d72491 100644 --- a/PinionCyber.StateManagement/PinionCyber.StateManagement.csproj +++ b/PinionCyber.StateManagement/PinionCyber.StateManagement.csproj @@ -3,12 +3,16 @@ netstandard2.1 enable - 0.1.0 + 0.1.1 + jc + PinionCyber + A very simple state machine. + StateMachine + https://github.com/jiowchern/PinionCyber.StateManagement + git - - - + <_Parameter1>PinionCyber.StateManagement.Tests diff --git a/PinionCyber.StateManagement/README.md b/PinionCyber.StateManagement/README.md deleted file mode 100644 index 46d719c..0000000 --- a/PinionCyber.StateManagement/README.md +++ /dev/null @@ -1,2 +0,0 @@ -# PinionCyber.StateManagement -A c# State machine kit. diff --git a/PinionCyber.StateManagement/StateMachine.cs b/PinionCyber.StateManagement/StateMachine.cs index 9cad746..c64018b 100644 --- a/PinionCyber.StateManagement/StateMachine.cs +++ b/PinionCyber.StateManagement/StateMachine.cs @@ -3,16 +3,16 @@ namespace PinionCyber.StateManagement { - public class StateMachine : Switcher + public class StateMachine : ActiverChanger { class EmptyState : IState { - void ISwitch.End() + void IActivable.Disable() { } - void ISwitch.Start() + void IActivable.Enable() { } @@ -32,7 +32,7 @@ public StateMachine(IState first) : base(first) } public void Empty() { - Switch(new EmptyState()); + Change(new EmptyState()); } } diff --git a/PinionCyber.StateManagement/Switcher.cs b/PinionCyber.StateManagement/Switcher.cs deleted file mode 100644 index 9c546c3..0000000 --- a/PinionCyber.StateManagement/Switcher.cs +++ /dev/null @@ -1,28 +0,0 @@ - - -using System; - -namespace PinionCyber.StateManagement -{ - public class Switcher where T : ISwitch - { - T _Switch; - - public Switcher(T first) - { - _Switch = first; - _Switch.Start(); - } - public void Switch(T state) - { - _Switch.End(); - _Switch = state; - _Switch.Start(); - } - - public T Activer() - { - return _Switch; - } - } -}