java - Applying key bindings to state transitions when implementing a state pattern -


here's programming style question best strategy map input keys actions in class implement state pattern.

i'm dealing 2 classes:

the first implements state pattern, controls multi-state physical device:

class devicecontroller {     state _a, _b, _current;      // actions may prompt transition 1 state     public void actiona() { ... }     public void actionb() { ... }     public void actionc() { ... }      public state getstatea() { ... }     public state getstateb() { ... }      public void setcurrentstate() { ... } }; 

the second keylistener retrieves keyboard input , calls appropriate action device controller when pressed input key matches (for time being) hard-coded bindings table:

class keydemo implements keylistener {      devicecontroller _controller;     ...     @override     public void keypressed(keyevent arg0) {         char key = character.touppercase(arg0.getkeychar());         switch (key) {         case 'a':             _controller.actiona();             break;         case 'b' :         ...     }     ... } 

is there best-practice coding style bind keys actions in controller ? have go through switch statement, in sample code ? seems me solution dirty code: isn't state pattern supposed eliminate unmaintanable if , switch control structures ?

thank suggenstions.

using polymorphism can achive goal. i've used enum maybe more appropriated use interfaces or abstract class , implement each of key processors. think?

import java.awt.event.keyevent; import java.awt.event.keylistener;  enum keyprocessor {     {         void executeaction() {             _controller.actiona();         }     },     b {         void executeaction() {             _controller.actionb();         }     };      private static final devicecontroller _controller = new devicecontroller();      void executeaction() {         system.out.println("no action defined");     } }  class devicecontroller {     state _a;     state _b;     state _current;      // actions may prompt transition 1 state     public void actiona() {         system.out.println("action performed.");      }      public void actionb() {         system.out.println("action b performed.");     }      public void actionc() {     }      public state getstatea() {         return null;     }      public state getstateb() {         return null;     }      public void setcurrentstate() {     } } // end class devicecontroller  public class keydemo implements keylistener {      devicecontroller _controller;      // ...     @override     public void keypressed(keyevent arg0) {         keypressed(character.touppercase(arg0.getkeychar()));         // ...     }      public void keypressed(char c) {         keyprocessor processor = keyprocessor.valueof(c + "");         if (processor != null) {         processor.executeaction();     }      }      @override     public void keytyped(keyevent e) {     }      @override     public void keyreleased(keyevent e) {     }      public static final void main(string[] args) {         keydemo main = new keydemo();         main.keypressed('a');         main.keypressed('b');     } } // end class keydemo  class state { } 

Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -