״̬ģʽ-State Pattern(JavaʵÏÖ)
2018-06-18 00:54:11À´Ô´£ºÎ´Öª ÔĶÁ ()
״̬ģʽ-State Pattern
ÔÚ״̬ģʽ(State Pattern)ÖÐ,ÀàµÄÐÐΪÊÇ»ùÓÚËüµÄ״̬¸Ä±äµÄ¡£µ±Ò»¸ö¶ÔÏóµÄÄÚÔÚ״̬¸Ä±äʱÔÊÐí¸Ä±äÆäÐÐΪ£¬Õâ¸ö¶ÔÏó¿´ÆðÀ´ÏñÊǸıäÁËÆäÀà¡£
State½Ó¿Ú
±íÃ÷״̬, ʵÌåÀàÊǸù¾Ý״̬µÄ±ä»¯¶ø·¢ÉúÏìÓ¦ÐÐΪµÄ±ä»¯µÄ.
/** * ״̬³éÏó¶¨Òå */ public interface State { void onEnterState(); void observe(); }
AngryStateÀà
״̬µÄÒ»ÖÖʵÏÖ.
/** * ÉúÆø״̬ */ public class AngryState implements State { private Mammoth mammoth; public AngryState(Mammoth mammoth) { this.mammoth = mammoth; } @Override public void observe() { System.out.printf("%s ´¦ÓÚ±©Ôê״̬!\n", mammoth); } @Override public void onEnterState() { System.out.printf("%s ¿ªÊ¼ÉúÆøÁË!\n", mammoth); } }
PeacefulStateÀà
״̬µÄÒ»ÖÖʵÏÖ.
/** * ƽ¾²×´Ì¬ */ public class PeacefulState implements State { private Mammoth mammoth; public PeacefulState(Mammoth mammoth) { this.mammoth = mammoth; } @Override public void observe() { System.out.printf("%s ÏÖÔÚºÜƽ¾².\n", mammoth); } @Override public void onEnterState() { System.out.printf("%s ¿ªÊ¼Àä¾²ÏÂÀ´ÁË.\n", mammoth); } }
MammothÀà
±¾ÀàÊÇ״̬StateµÄ³ÖÓÐÕß
/** * ÃÍáï´óÏó */ public class Mammoth { private State state; public Mammoth() { state = new PeacefulState(this); } public void timePasses() { if (state.getClass().equals(PeacefulState.class)) { changeStateTo(new AngryState(this)); } else { changeStateTo(new PeacefulState(this)); } } private void changeStateTo(State newState) { this.state = newState; this.state.onEnterState(); } public void observe() { this.state.observe(); } @Override public String toString() { return "ÃÍáï´óÏó"; } }
Main
ÓÃÓÚÄ£Äⳡ¾°ÒÔ¼°ÔËÐдúÂë
public class Main { public static void main(String[] args) { Mammoth mammoth = new Mammoth(); // ¿´¿´´óÏóÏÖÔÚÊÇʲô״̬ mammoth.observe(); // ¹ýÁËÒ»»á¶ù mammoth.timePasses(); // ¿´¿´´óÏóÏÖÔÚÊÇʲô״̬ mammoth.observe(); // ¹ýÁËÒ»»á¶ù mammoth.timePasses(); // ¿´¿´´óÏóÏÖÔÚÊÇʲô״̬ mammoth.observe(); } }
ÔËÐнá¹ûÈçÏÂ:
±êÇ©£º
°æȨÉêÃ÷£º±¾Õ¾ÎÄÕ²¿·Ö×ÔÍøÂ磬ÈçÓÐÇÖȨ£¬ÇëÁªÏµ£ºwest999com@outlook.com
Ìرð×¢Ò⣺±¾Õ¾ËùÓÐתÔØÎÄÕÂÑÔÂÛ²»´ú±í±¾Õ¾¹Ûµã£¬±¾Õ¾ËùÌṩµÄÉãÓ°ÕÕƬ£¬²å»£¬Éè¼Æ×÷Æ·£¬ÈçÐèʹÓã¬ÇëÓëÔ×÷ÕßÁªÏµ£¬°æȨ¹éÔ×÷ÕßËùÓÐ
- Éè¼Æģʽ-ίÅÉ/²ßÂÔģʽ 2020-06-09
- ÉîÈëÀí½â:Éè¼ÆģʽÖеÄÆß´óÉè¼ÆÔÔò 2020-06-07
- Éè¼Æģʽ---ÀàÖ®¼äµÄ¹Øϵ֪¶àÉÙ 2020-06-07
- ÄãÓëÃæÊÔ¹ÙËùÁ˽âµÄµ¥Àýģʽ²¢²»Ò»Ñù£¡ 2020-06-06
- ¸ßÊÖÑÛÖеĹ۲ìÕßģʽÓÐʲô²»Ò»Ñù 2020-06-05
IDC×ÊѶ£º Ö÷»ú×ÊѶ ×¢²á×ÊѶ ÍйÜ×ÊѶ vps×ÊѶ ÍøÕ¾½¨Éè
ÍøÕ¾ÔËÓª£º ½¨Õ¾¾Ñé ²ß»®Ó¯Àû ËÑË÷ÓÅ»¯ ÍøÕ¾Íƹã Ãâ·Ñ×ÊÔ´
ÍøÕ¾ÁªÃË£º ÁªÃËÐÂÎÅ ÁªÃ˽éÉÜ ÁªÃ˵ãÆÀ Íø׬¼¼ÇÉ
ÐÐÒµ×ÊѶ£º ËÑË÷ÒýÇæ ÍøÂçÓÎÏ· µç×ÓÉÌÎñ ¹ã¸æ´«Ã½
ÍøÂç±à³Ì£º Asp.Net±à³Ì Asp±à³Ì Php±à³Ì Xml±à³Ì Access Mssql Mysql ÆäËü
·þÎñÆ÷¼¼Êõ£º Web·þÎñÆ÷ Ftp·þÎñÆ÷ Mail·þÎñÆ÷ Dns·þÎñÆ÷ °²È«·À»¤
Èí¼þ¼¼ÇÉ£º ÆäËüÈí¼þ Word Excel Powerpoint Ghost Vista QQ¿Õ¼ä QQ FlashGet ѸÀ×
ÍøÒ³ÖÆ×÷£º FrontPages Dreamweaver Javascript css photoshop fireworks Flash
³ÌÐòÉè¼Æ£º Java¼¼Êõ C/C++ VB delphi
- Photoshop»æÖÆÁ¢Ìå·ç¸ñµÄ΢Ц±íÇé
- PSÎÄ×ÖÌØЧ½Ì³Ì£ºÖÆ×÷¹«Â·ÉϸöÐÔµÄÍ¿Ñ»
- PhotoshopÉè¼Æ¾í±ßЧ¹ûµÄ±äÐνð¸ÕµçÓ°
- PSÉ«²ÊÐÞ¸´½Ì³Ì£ºÀûÓÃÉ«½×¹¤¾ß¿ìËÙ¸øºì
- PS°ë͸Ã÷ÎïÌå¿Ùͼ£ºÀûÓÃͨµÀÑ¡Çø¹¤¾ß¿Ù
- PSº£±¨Éè¼Æ¼¼Çɽ̳̣ºÑ§Ï°ÖÆ×÷¸öÐԵč
- PSͼƬÌØЧÖÆ×÷½Ì³Ì£ºÑ§Ï°¸øÌúËþͼƬÖÆ
- ѧϰÓÃphotoshop°Ñ»ë×ǵĺ£Ë®ÕÕƬºóÆÚ
- PS¹Å·çÕÕƬ½Ì³Ì£º¸ø¹Å·çÃÀÅ®´òÔì³öÅ®ÏÀ
- PS¸öÐÔÈËÎﺣ±¨ÖÆ×÷£ºÉè¼Æ´´ÒâʱÉеIJ£
- ʲôÈí¼þ¿ÉÒÔµÁȡ΢ÐźÅÃÜÂë,ÔõôµÁ±ð
- ´ÅÁ¦ËÑË÷ÍøÕ¾µ¼º½2020Äê¸üÐÂ
- springcloudѧϰ֮·: (Ò») ×î¼òµ¥µÄ´î
- ´´½¨Gradle¹¤³Ì³öÏÖCould not install
- ³¹µ×Ū¶®¡°PKIX path building failed
- TomcatÆô¶¯±¨´í:org.apache.catalina.L
- spring boot ´íÎó£ºCheck your ViewRes
- »ùÓÚHttpClientµÄаæÕý·½½ÌÎñϵͳģÄâ
- Ö»ÓгÌÐòÔ±²ÅÄÜ¿´¶®µÄ³µÅÆ£¬¶®µÃ×ÔÈ»¶®
- mybatis ×¢½â@Results¡¢@Result¡¢@Resu