Java °æ (¾«»ªÇø)

·¢ÐÅÈË: Javor (ÖíͷС¶Ó³¤¡¤ÀֺǺÇ), ÐÅÇø: Java
±ê  Ìâ: [ºÏ¼¯] Stateģʽ¾ÙÀý
·¢ÐÅÕ¾: ¹þ¹¤´ó×϶¡Ïã (Mon Jun  7 13:10:01 2004), Õ¾ÄÚ

©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤
  dongbin (ÀË»¨)                   ÓÚ  (Fri Jun  4 12:21:36 2004)  ËµµÀ:

±¾À´ÊÇҪдFactory MethodµÄ£¬µ«ÊÇ´ÓÀ´Ã»ÓùýÕâ¸öģʽ£¬²»ºÃÒâ˼ÐÅ¿ÚºúÖߣ¬ËùÒÔ¾ÙÒ»
¸ö×Ô¼ºÓùýµÄģʽÀ´ÁÄÁÄ¡£
StateģʽµÄÒâͼ£ºÔÊÐí¶ÔÏóÔÚÆäÄÚ²¿×´Ì¬¸Ä±äʱ¸Ä±äËüµÄÐÐΪ£¬¶ÔÏó¿´ÆðÀ´ËƺõÐÞ¸ÄÁËËü
µÄÀà¡££¨Òý×Ô¡¶Éè¼Æģʽ ¿É¸´ÓÃÃæÏò¶ÔÏóÈí¼þµÄ»ù´¡¡·£©
Õâ¾ä»°ÌýÆðÀ´ºÜ»Þɬ£¬Éè¼ÆģʽÕⶫÎ÷¾ÍÊÇÕâÑù£¬²»¾Ù¸öÀý×Ó³öÀ´ÉñÏÉҲ˵²»Çå³þ¡£GOFµÄ
ÊéÀï¾ÙµÄÊÇTCPÁ¬½ÓµÄÀý×Ó£¬³õѧÕß¿ÉÄܲ»Ì«½ÓÊÜ£¬ÆäʵÕâ¸öģʽÀÏÉÙÏÌÒ˵ÄÀý×ӺܶࡣÆä
ÖбȽϺõÄÀý×Ó¾ÍÊÇһЩ»­Í¼³ÌÐò¡£
»­Ò»¸öÔ²ºÜ¼òµ¥£¬Ê×ÏÈ´Ó¹¤¾ßÀ¸ÖеãÖл­Ô²°´Å¥£¬È»ºóÔÚͼ°åÉÏÊ×ÏÈÓÃÊó±êµã»÷һϣ¬È»
ºóÍ϶¯Êó±ê£¬¾Í»áÒԸõãΪԲÐÄ»­Ô²£¬Êó±êÍϵ½ÄÄÀ°ë¾¶¾ÍÒƵ½ÄÄÀï¡£×îºóÔÙµã»÷Ò»ÏÂ
Êó±ê£¬Ô²°ë¾¶¾Í¹Ì¶¨ÏÂÀ´£¬»­Ô²½áÊø¡£³ÌÐòʵÏָùý³ÌÈçÏ£º
public class Screen{

    private boolean startDrawCircle ; //»­Ô²Ê±µÄ״̬£¬±íʾ¸Õ¿ªÊ¼»­£¬»¹ÊÇ»æÖÆÍê
³É
    ...................
    public void mouseClick(MouseEvent e){
            if(startDrawCircle){
                startDrawCircle = false ;
                Circle.drawCircle();       //ÈçºÎ»­Ô²µÄʵÏÖ£¬ÕâÀï²»¾ßÌå˵ÁË

            }else{
                startDrawCircle = true;
                Circle.drawCircle();
            }
    }
    public void mouseMove(MouseEvent e){
            if(startDrawCircle){
                Circle.drawCircle();                        }
    }
}
Õâ¶Î³ÌÐòÓÐÁ½¸ö±×²¡
1.Èç¹û»­Ô²µÄ״̬½Ï¶à£¬¾Í»áÔÚmouseClickÀïÓкܳ¤µÄif else,»òÕßswitch case,¶øÇÒÈÎ
ºÎ²Ù×÷Ö»ÒªÉæ¼°µ½¸Ã״̬¾ÍÒªÖØи´ÖÆÕâ¶Îif else ÁÐ±í£¬±ÈÈçmouseMove·½·¨¡£
2.״̬±ä»»µÄ±©Â¶£¬Õâ¶Î³ÌÐòÖÐÓÉScreenÀฺÔð״̬±ä»»£¬¾ÍÊÇÕâ¶Î´úÂë
  if(startDrawCircle){
                startDrawCircle = false ;
  .......
£¬¶øÇÒ²»Í¬×´Ì¬¶Ô²»Í¬Ê¼þµÄ´¦Àí´úÂ뼯ÖÐÔÚÒ»Æð£¬Ôì³ÉÁ˲»±ØÒªµÄÏà¹ØÐÔ£¬Ëæ×Å״̬µÄ
Ôö¶à£¬»áµ¼ÖÂScreenÔ½À´Ô½¸´ÔÓ£¬ÄÑÒÔά»¤¡£


Stateģʽ°ÑÄÚ²¿×´Ì¬ºÍ»·¾³°þÀ룬°Ñ״̬±ä»»ºÍ״̬Ïà¹ØÐÐΪ½øÐзâ×°¡£
Ê×Ïȶ¨ÒåCircleState½Ó¿Ú

interface CircleState{
    public void click(Screen screen);
    public void drawCircle();
}
ʵÏÖÁ½¸ö¾ßÌåÀà
public class CircleStartState implements CircleState{
    public void click(Screen screen){
        Circle.drawCircle();
        screen.changeState(CircleEndState.Instance());//ÕâÊÇStateģʽµÄÖص㣬°Ñ

                                        //״̬ÈçºÎ±ä»»·â×°µ½¾ßÌåµÄ״̬¶ÔÏóÀï¡£

    }
    public void drawCircle(){
        Circle.drawCircle();
    }
}

public class CircleEndState implements CircleState{
    public void click(Screen screen){
        Circle.drawCircle();
        screen.changeState(CircleStartState.Instance());
    }
    public void drawCircle(){
    }
}

ÆäÖÐCircleEndState.Instance()Êǵõ½Ò»¸öCircleEndStateÒýÓõĵ¥Àý·½·¨£¬Ä¿µÄÊǾ¡Á¿
¼õÉÙ¶ÔÏóµÄ´´½¨£¬±ÜÃâ²»±ØÒªµÄ¿ªÏú¡£


ScreenµÄʵÏÖ´úÂë¾Í±ä³ÉÁË
public class Screen{
    private CircleState state = CircleEndState.Instance(); //»­Ô²Ê±µÄ״̬£¬
                                                   //±íʾ¸Õ¿ªÊ¼»­£¬»¹ÊÇ»æÖÆÍê³É

    public void changeState(CircleState state){
        this.state = state ;
    }
    ...................
    public void mouseClick(MouseEvent e){
         state.click(this);
    }
    public void mouseMove(MouseEvent e){
         state.drawCircle();
    }
}

ÏÖÔÚScreenÊDz»ÊǺÿ´¶àÁË£¬Êµ¼ÊÖеÄÇé¿öÒª¸´ÔӵĶ࣬Ö÷ÒªÊÇ״̬±ä»»µÄ¸´ÔÓÐÔËæ×Å×´
̬µÄÔö¶à¶øÖ¸Êý¼¶Ôö¼Ó¡£
µ±×´Ì¬Ì«¸´ÔÓʱ£¬ÈçºÎ³éÏóState¶ÔÏó±È½ÏÀ§ÄÑ£¬ÏÈÒª»¨µãʱ¼ä»­Ò»ÏÂÓÐÏÞ״̬»ú£¬¿´Ò»ÏÂ
¶Ôÿһ¸ö״̬µÄ²Ù×÷¿ÉÄܱ任³ÉΪÆäËûʲô״̬¡£¿ÉÒÔ˵Stateģʽ¾ÍÊÇÓÐÏÞ״̬»úµÄÒ»ÖÖ
ʵÏÖ²ßÂÔ£¬ÕæÊÇÍò±ä²»ÀëÆäÖа¡¡£

ÓÐЩÈËʹÓÃStateģʽʱ°Ñ״̬±ä»»Å²µ½»·¾³£¨±¾ÎÄÖоÍÊÇScreenÀࣩÖУ¬State¶ÔÏóÖ»·â
×°Ïà¹Ø״̬µÄ²Ù×÷£¬Õâô×öµÄЧ¹ûÖ»ÊǼõÉÙÁËif else ÁбíµÄÊýÁ¿£¬Êµ¼ÊÉÏÕâ¾ÍʧȥÁËSt
ateģʽµÄÒâÒ壬¶øÍË»¯³ÉÁË
Strategyģʽ¡£

×î½üæ×ÅдÂÛÎÄ£¬ÖÕÓÚ³é³öʱ¼äдÁËÕâƪ׾×÷£¬²»ÖªµÀJavorÂúÒâ·ñ£¬»¶Ó­´ó¼ÒÌÖÂÛ


©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤©¤
  kingheap (A Million Dollars)     ÓÚ  (Fri Jun  4 13:39:53 2004)  ËµµÀ:

Very good!
Thank you very much!

¡¾ ÔÚ dongbin (ÀË»¨) µÄ´ó×÷ÖÐÌáµ½: ¡¿
: ±¾À´ÊÇҪдFactory MethodµÄ£¬µ«ÊÇ´ÓÀ´Ã»ÓùýÕâ¸öģʽ£¬²»ºÃÒâ˼ÐÅ¿ÚºúÖߣ¬ËùÒÔ..
: ¸ö×Ô¼ºÓùýµÄģʽÀ´ÁÄÁÄ¡£
: StateģʽµÄÒâͼ£ºÔÊÐí¶ÔÏóÔÚÆäÄÚ²¿×´Ì¬¸Ä±äʱ¸Ä±äËüµÄÐÐΪ£¬¶ÔÏó¿´ÆðÀ´ËƺõÐÞ¸..
: µÄÀà¡££¨Òý×Ô¡¶Éè¼Æģʽ ¿É¸´ÓÃÃæÏò¶ÔÏóÈí¼þµÄ»ù´¡¡·£©
: Õâ¾ä»°ÌýÆðÀ´ºÜ»Þɬ£¬Éè¼ÆģʽÕⶫÎ÷¾ÍÊÇÕâÑù£¬²»¾Ù¸öÀý×Ó³öÀ´ÉñÏÉҲ˵²»Çå³þ¡£..
: ÊéÀï¾ÙµÄÊÇTCPÁ¬½ÓµÄÀý×Ó£¬³õѧÕß¿ÉÄܲ»Ì«½ÓÊÜ£¬ÆäʵÕâ¸öģʽÀÏÉÙÏÌÒ˵ÄÀý×Ӻܶ..
: ÖбȽϺõÄÀý×Ó¾ÍÊÇһЩ»­Í¼³ÌÐò¡£
: »­Ò»¸öÔ²ºÜ¼òµ¥£¬Ê×ÏÈ´Ó¹¤¾ßÀ¸ÖеãÖл­Ô²°´Å¥£¬È»ºóÔÚͼ°åÉÏÊ×ÏÈÓÃÊó±êµã»÷Ò»ÏÂ..
: ...................





[°Ù±¦Ïä] [·µ»ØÊ×Ò³] [Éϼ¶Ä¿Â¼] [¸ùĿ¼] [·µ»Ø¶¥²¿] [Ë¢ÐÂ] [·µ»Ø]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
Ò³ÃæÖ´ÐÐʱ¼ä£º4.047ºÁÃë