SlideShare ist ein Scribd-Unternehmen logo
1 von 30
Downloaden Sie, um offline zu lesen
Delegate	
  pa)ern	
  
State	
  pa)ern	
Giang	
  Minh
Outline	
•  Delegate	
  
–  Bài	
  toán,	
  vấn	
  đề	
  
–  Giới	
  thiệu	
  delegate	
  và	
  áp	
  dụng	
  
–  Các	
  trường	
  hợp	
  nên	
  sử	
  dụng	
  

•  State	
  
–  Bài	
  toán	
  thực	
  tế	
  	
  
–  Vấn	
  đề	
  khi	
  giải	
  quyết	
  bằng	
  phương	
  pháp	
  thông	
  
thường	
  
–  Các	
  giải	
  quyết	
  với	
  state	
  
Bài toán	
•  Slidemenu
Bài toán	
•  Slidemenu
Bài toán	
•  Slidemenu
Bài	
  toán	
•  Vấn	
  đề	
  
–  Cùng	
  một	
  dạng	
  nhưng	
  được	
  lặp	
  lại	
  
•  Có	
  một	
  vài	
  hành	
  vi	
  khác	
  nhau	
  

•  Thông	
  thường	
  
–  Dư	
  thừa	
  code	
  
–  Không	
  sử	
  dụng	
  lại	
  được	
  code
Delegate pattern	
•  	
  
Hoàn cảnh sử dụng	
•  Giảm	
  thiểu	
  sự	
  móc	
  nối	
  các	
  func^on	
  giữa	
  các	
  
class	
  
•  Có	
  những	
  thành	
  phần	
  cố	
  định	
  
–  Nhưng	
  hành	
  vi	
  thay	
  đổi	
  tuỳ	
  ngữ	
  cảnh
Nhận xét	
•  Điểm	
  mạnh	
  
–  Reuse	
  
–  Mềm	
  dẻo	
  

•  Điểm	
  yếu	
  
–  Tốn	
  công	
  
–  Khi	
  cần	
  phản	
  hồi	
  đến	
  nhiều	
  delegator
Bài toán 2	
•  Nhân	
  vật	
  trong	
  game	
  có	
  hành	
  động	
  
–  Nhảy	
  khi	
  ấn	
  phím	
  B
Bài toán 2	
•  Nhân	
  vật	
  trong	
  game	
  có	
  hành	
  động	
  
–  Nhảy	
  khi	
  ấn	
  phím	
  B	
void	
  Heroine::handleInput(Input	
  input)	
  
{	
  
	
  	
  if	
  (input	
  ==	
  PRESS_B)	
  
	
  	
  {	
  
	
  	
  	
  	
  yVelocity_	
  =	
  JUMP_VELOCITY;	
  
	
  	
  	
  	
  setGraphics(IMAGE_JUMP);	
  
	
  	
  }	
  
}	
  
Bài toán 2	
•  Nhân	
  vật	
  trong	
  game	
  có	
  hành	
  động	
  
–  Nhảy	
  khi	
  ấn	
  phím	
  B	
  
void	
  Heroine::handleInput(Input	
  input)	
  
{	
  
	
  	
  if	
  (input	
  ==	
  PRESS_B)	
  
	
  	
  {	
  
	
  	
  	
  	
  yVelocity_	
  =	
  JUMP_VELOCITY;	
  
	
  	
  	
  	
  setGraphics(IMAGE_JUMP);	
  
	
  	
  }	
  
}	
  
	

–  Vấn	
  đề????
Sửa bug1	
void	
  Heroine::handleInput(Input	
  input){	
  
	
  	
  if	
  (input	
  ==	
  PRESS_B){	
  
	
  	
  	
  	
  if	
  (!isJumping_){	
  
	
  	
  	
  	
  	
  	
  isJumping_	
  =	
  true;	
  
	
  	
  	
  	
  	
  	
  //	
  Jump...	
  
	
  	
  	
  	
  }	
  
	

•  -­‐>	
  OK	
  
•  Muốn	
  thêm	
  động	
  tác	
  ngồi???	
  
Thêm	
  trạng	
  thái	
•  Thêm	
  if	
  

void	
  Heroine::handleInput(Input	
  input){	
  
	
  	
  if	
  (input	
  ==	
  PRESS_B)	
  {	
  
	
  	
  	
  	
  //	
  Jump	
  if	
  not	
  jumping...	
  
	
  	
  }	
  else	
  if	
  (input	
  ==	
  PRESS_DOWN){	
  
	
  	
  	
  	
  if	
  (!isJumping_)	
  	
  	
  	
  {	
  
	
  	
  	
  	
  	
  	
  setGraphics(IMAGE_DUCK);	
  
	
  	
  	
  	
  }	
  
	
  	
  }	
  	
  else	
  if	
  (input	
  ==	
  RELEASE_DOWN)	
  	
  {	
  
	
  	
  	
  	
  setGraphics(IMAGE_STAND);	
  
	
  	
  }	
  
}	

•  Any	
  problem??
Thêm	
  trạng	
  thái	
•  Thêm	
  if	
  

void	
  Heroine::handleInput(Input	
  input){	
  
	
  	
  if	
  (input	
  ==	
  PRESS_B)	
  {	
  
	
  	
  	
  	
  //	
  Jump	
  if	
  not	
  jumping...	
  
	
  	
  }	
  else	
  if	
  (input	
  ==	
  PRESS_DOWN){	
  
	
  	
  	
  	
  if	
  (!isJumping_)	
  	
  	
  	
  {	
  
	
  	
  	
  	
  	
  	
  setGraphics(IMAGE_DUCK);	
  
	
  	
  	
  	
  }	
  
	
  	
  }	
  	
  else	
  if	
  (input	
  ==	
  RELEASE_DOWN)	
  	
  {	
  
	
  	
  	
  	
  setGraphics(IMAGE_STAND);	
  
	
  	
  }	
  
}	

	
  
	
  
•  Any	
  problem??	
  

–  Ngồi	
  -­‐>	
  ấn	
  nhảy,	
  thả	
  phím	
  down	
  -­‐>	
  đứng	
  dậy	
  trên	
  
không
Sửa bug2	
•  Thêm	
  checker	
  	

void	
  Heroine::handleInput(Input	
  input){	
  
	
  	
  if	
  (input	
  ==	
  PRESS_B)	
  	
  {	
  
	
  	
  	
  	
  if	
  (!isJumping_	
  &&	
  !isDucking_)	
  	
  	
  	
  {	
  
	
  	
  	
  	
  	
  	
  //	
  Jump...	
  
	
  	
  	
  	
  }	
  
	
  	
  }	
  	
  else	
  if	
  (input	
  ==	
  PRESS_DOWN)	
  	
  {	
  
	
  	
  	
  	
  if	
  (!isJumping_)	
  	
  	
  	
  {	
  
	
  	
  	
  	
  	
  	
  isDucking_	
  =	
  true;	
  
	
  	
  	
  	
  	
  	
  setGraphics(IMAGE_DUCK);	
  
	
  	
  	
  	
  }	
  
	
  	
  }	
  	
  else	
  if	
  (input	
  ==	
  RELEASE_DOWN)	
  	
  {	
  
	
  	
  	
  	
  if	
  (isDucking_){	
  
	
  	
  	
  	
  	
  	
  isDucking_	
  =	
  false;	
  
	
  	
  	
  	
  	
  	
  setGraphics(IMAGE_STAND);	
  
	
  	
  	
  	
  }	
  
	
  	
  }	
  
}	
  
Thêm chức năng	
•  Tấn	
  công	
  khi	
  đang	
  nhảy	
  và	
  ấn	
  DOWN	
  
Thêm chức năng	
•  Tấn	
  công	
  khi	
  đang	
  nhảy	
  và	
  ấn	
  DOWN	
  
•  Đặt	
  bom	
  khi	
  ngồi	
  lâu	
  
•  …………………	
  
	
  
Thêm chức năng	
•  Tấn	
  công	
  khi	
  đang	
  nhảy	
  và	
  ấn	
  DOWN	
  
•  Đặt	
  bom	
  khi	
  ngồi	
  lâu	
  
•  …………………	
  

•  -­‐>	
  quá	
  nhiều	
  checker,	
  flag,	
  countdown	
  
Finite State machine	
•  a
Finite state machine	
•  Enum	
  +	
  if	
  
enum	
  State	
  
{	
  
	
  	
  STATE_STANDING,	
  
	
  	
  STATE_JUMPING,	
  
	
  	
  STATE_DUCKING,	
  
	
  	
  STATE_DIVING	
  
};	

void	
  Heroine::handleInput(Input	
  input)	
  
{	
  
	
  	
  switch	
  (state_)	
  
	
  	
  {	
  
	
  	
  	
  	
  case	
  STATE_STANDING:	
  
	
  	
  	
  	
  	
  	
  if	
  (input	
  ==	
  PRESS_B)	
  
	
  	
  	
  	
  	
  	
  {	
  
	
  	
  	
  	
  	
  	
  	
  	
  state_	
  =	
  STATE_JUMPING;	
  
	
  	
  	
  	
  	
  	
  	
  	
  yVelocity_	
  =	
  JUMP_VELOCITY;	
  
	
  	
  	
  	
  	
  	
  	
  	
  setGraphics(IMAGE_JUMP);	
  
	
  	
  	
  	
  	
  	
  }	
  
	
  	
  	
  	
  	
  	
  else	
  if	
  (input	
  ==	
  PRESS_DOWN)	
  
	
  	
  	
  	
  	
  	
  {	
  
	
  	
  	
  	
  	
  	
  	
  	
  state_	
  =	
  STATE_DUCKING;	
  
	
  	
  	
  	
  	
  	
  	
  	
  setGraphics(IMAGE_DUCK);	
  
	
  	
  	
  	
  	
  	
  }	
  
	
  	
  	
  	
  	
  	
  break;	
  
…………………………………
Finite state machine	
•  Enum	
  +	
  if	
  
enum	
  State	
  
{	
  
	
  	
  STATE_STANDING,	
  
	
  	
  STATE_JUMPING,	
  
	
  	
  STATE_DUCKING,	
  
	
  	
  STATE_DIVING	
  
};	

void	
  Heroine::handleInput(Input	
  input)	
  
{	
  
	
  	
  switch	
  (state_)	
  
	
  	
  {	
  
	
  	
  	
  	
  case	
  STATE_STANDING:	
  
	
  	
  	
  	
  	
  	
  if	
  (input	
  ==	
  PRESS_B)	
  
	
  	
  	
  	
  	
  	
  {	
  
	
  	
  	
  	
  	
  	
  	
  	
  state_	
  =	
  STATE_JUMPING;	
  
	
  	
  	
  	
  	
  	
  	
  	
  yVelocity_	
  =	
  JUMP_VELOCITY;	
  
	
  	
  	
  	
  	
  	
  	
  	
  setGraphics(IMAGE_JUMP);	
  
	
  	
  	
  	
  	
  	
  }	
  
	
  	
  	
  	
  	
  	
  else	
  if	
  (input	
  ==	
  PRESS_DOWN)	
  
	
  	
  	
  	
  	
  	
  {	
  
	
  	
  	
  	
  	
  	
  	
  	
  state_	
  =	
  STATE_DUCKING;	
  
	
  	
  	
  	
  	
  	
  	
  	
  setGraphics(IMAGE_DUCK);	
  
	
  	
  	
  	
  	
  	
  }	
  
	
  	
  	
  	
  	
  	
  break;	
  
…………………………………	

Ø  Số	
  lượng	
  state	
  tăng	
  
Ø  Mỗi	
  state	
  có	
  dữ	
  liệu	
  riêng	
  
l  Countdown	
  
l  Timing
State	
  pa)ern	
•  Mỗi	
  state	
  được	
  implement	
  riêng	
  
•  State	
  <	
  -­‐-­‐	
  >	
  Behavior	
  
•  StateInterface	
class	
  HeroineState	
  
{	
  
public:	
  
	
  	
  virtual	
  void	
  handleInput(Heroine&	
  heroine,	
  Input	
  input)	
  {}	
  
	
  	
  virtual	
  void	
  update(Heroine&	
  heroine)	
  {}	
  
	
  	
  virtual	
  void	
  enter(Heroine&	
  heroine){}	
  
};
class	
  DuckingState	
  :	
  public	
  HeroineState{	
  
public:	
  
	
  	
  DuckingState()	
  	
  :	
  chargeTime_(0)	
  	
  {}	
  
	
  
	
  	
  void	
  handleInput(Heroine&	
  heroine,	
  Input	
  input)	
  {	
  
	
  	
  	
  	
  if	
  (input	
  ==	
  RELEASE_DOWN)	
  	
  	
  	
  {	
  
	
  	
  	
  	
  	
  	
  //	
  Change	
  to	
  standing	
  state...	
  
	
  	
  	
  	
  	
  	
  heroine.setGraphics(IMAGE_STAND);	
  
	
  	
  	
  	
  }	
  
	
  	
  }	
  
	
  	
  void	
  update(Heroine&	
  heroine)	
  {	
  
	
  	
  	
  	
  chargeTime_++;	
  
	
  	
  	
  	
  if	
  (chargeTime_	
  >	
  MAX_CHARGE){	
  
	
  	
  	
  	
  	
  	
  heroine.superBomb();	
  
	
  	
  	
  	
  }	
  
	
  	
  }	
  
private:	
  
	
  	
  int	
  chargeTime_;	
  
};
class	
  Heroine	
  
{	
  
public:	
  
	
  	
  void	
  handleInput(Input	
  input)	
  
	
  	
  {	
  
	
  	
  	
  	
  state_-­‐>handleInput(*this,	
  input);	
  
	
  	
  }	
  
	
  
	
  	
  void	
  update()	
  
	
  	
  {	
  
	
  	
  	
  	
  state_-­‐>update(*this);	
  
	
  	
  }	
  
	
  	
  
	
  	
  void	
  sw^chState(HeroineState*	
  new_state)	
  
	
  	
  {	
  
	
  	
  	
  	
  state_	
  	
  =	
  	
  new_state;	
  
	
  	
  	
  	
  state_-­‐>enter	
  (*this);	
  
	
  	
  }	
  
	
  	
  	
  
	
  	
  //	
  Other	
  methods...	
  
private:	
  
	
  	
  HeroineState*	
  state_;	
  
};
Các	
  state	
  khác	
•  Standing,	
  Jumping,	
  DivingState
Hoàn cảnh sử dụng	
•  Hành	
  vi	
  phụ	
  thuộc	
  vào	
  một	
  vài	
  trạng	
  thái	
  của	
  
object	
  
•  Phân	
  chia	
  được	
  hoạt	
  động	
  của	
  object	
  ra	
  
những	
  state	
  khác	
  biệt,	
  cụ	
  thể	
  
•  Object	
  phản	
  hồi	
  theo	
  những	
  chuỗi	
  input	
  hoặc	
  
sự	
  kiện
Nhận xét	
•  Thêm	
  state	
  dễ	
  dàng	
  
–  Sơ	
  đồ	
  chuyển	
  trạng	
  thái	
  
–  Tách	
  rời	
  các	
  state	
  

•  Quản	
  lý	
  giữa	
  các	
  state	
  rõ	
  ràng	
  
–  Bỏ	
  flag,	
  checker….
THẢO	
  LUẬN
Thực hành	
•  Nhân	
  vật	
  có	
  3	
  trạng	
  thái	
  
–  Đứng	
  
–  Chạy	
  chậm	
  
–  Chạy	
  nhanh	
  

•  KeyboardDelegate	
  
•  StatePa)ern	
  

Weitere ähnliche Inhalte

Andere mochten auch

Andere mochten auch (16)

Game engine
Game engineGame engine
Game engine
 
Anatomia, Histologia y Fisiologia Pancreatica
Anatomia, Histologia y Fisiologia PancreaticaAnatomia, Histologia y Fisiologia Pancreatica
Anatomia, Histologia y Fisiologia Pancreatica
 
MANAGEMENT 3.0 - change and innovation practices
MANAGEMENT 3.0 - change and innovation practicesMANAGEMENT 3.0 - change and innovation practices
MANAGEMENT 3.0 - change and innovation practices
 
secure multi-owner data sharing for dynamic groups
secure multi-owner data sharing for dynamic groupssecure multi-owner data sharing for dynamic groups
secure multi-owner data sharing for dynamic groups
 
Suspension and Chassis_Glossary
Suspension and Chassis_GlossarySuspension and Chassis_Glossary
Suspension and Chassis_Glossary
 
Relational Database and mysql insight
Relational Database and mysql insightRelational Database and mysql insight
Relational Database and mysql insight
 
Buy dissertation
Buy dissertationBuy dissertation
Buy dissertation
 
Terms and conditions
Terms and conditionsTerms and conditions
Terms and conditions
 
College essays
College essaysCollege essays
College essays
 
Uk essays
Uk essaysUk essays
Uk essays
 
Math help
Math helpMath help
Math help
 
College essays
College essaysCollege essays
College essays
 
Research papers
Research papersResearch papers
Research papers
 
Buy dissertation
Buy dissertationBuy dissertation
Buy dissertation
 
College essays
College essaysCollege essays
College essays
 
Research papers
Research papersResearch papers
Research papers
 

Mehr von mentallog

Builder pattern
Builder pattern Builder pattern
Builder pattern mentallog
 
Façade pattern
Façade patternFaçade pattern
Façade patternmentallog
 
Break out game
Break out gameBreak out game
Break out gamementallog
 
Composite pattern
Composite patternComposite pattern
Composite patternmentallog
 
Mentallog12 10
Mentallog12 10Mentallog12 10
Mentallog12 10mentallog
 
Agile software development
Agile software developmentAgile software development
Agile software developmentmentallog
 
Introduction about GIT
Introduction about GITIntroduction about GIT
Introduction about GITmentallog
 

Mehr von mentallog (8)

Builder pattern
Builder pattern Builder pattern
Builder pattern
 
Façade pattern
Façade patternFaçade pattern
Façade pattern
 
Nosql
NosqlNosql
Nosql
 
Break out game
Break out gameBreak out game
Break out game
 
Composite pattern
Composite patternComposite pattern
Composite pattern
 
Mentallog12 10
Mentallog12 10Mentallog12 10
Mentallog12 10
 
Agile software development
Agile software developmentAgile software development
Agile software development
 
Introduction about GIT
Introduction about GITIntroduction about GIT
Introduction about GIT
 

Present delegate-state

  • 1. Delegate  pa)ern   State  pa)ern Giang  Minh
  • 2. Outline •  Delegate   –  Bài  toán,  vấn  đề   –  Giới  thiệu  delegate  và  áp  dụng   –  Các  trường  hợp  nên  sử  dụng   •  State   –  Bài  toán  thực  tế     –  Vấn  đề  khi  giải  quyết  bằng  phương  pháp  thông   thường   –  Các  giải  quyết  với  state  
  • 6. Bài  toán •  Vấn  đề   –  Cùng  một  dạng  nhưng  được  lặp  lại   •  Có  một  vài  hành  vi  khác  nhau   •  Thông  thường   –  Dư  thừa  code   –  Không  sử  dụng  lại  được  code
  • 8. Hoàn cảnh sử dụng •  Giảm  thiểu  sự  móc  nối  các  func^on  giữa  các   class   •  Có  những  thành  phần  cố  định   –  Nhưng  hành  vi  thay  đổi  tuỳ  ngữ  cảnh
  • 9. Nhận xét •  Điểm  mạnh   –  Reuse   –  Mềm  dẻo   •  Điểm  yếu   –  Tốn  công   –  Khi  cần  phản  hồi  đến  nhiều  delegator
  • 10. Bài toán 2 •  Nhân  vật  trong  game  có  hành  động   –  Nhảy  khi  ấn  phím  B
  • 11. Bài toán 2 •  Nhân  vật  trong  game  có  hành  động   –  Nhảy  khi  ấn  phím  B void  Heroine::handleInput(Input  input)   {      if  (input  ==  PRESS_B)      {          yVelocity_  =  JUMP_VELOCITY;          setGraphics(IMAGE_JUMP);      }   }  
  • 12. Bài toán 2 •  Nhân  vật  trong  game  có  hành  động   –  Nhảy  khi  ấn  phím  B   void  Heroine::handleInput(Input  input)   {      if  (input  ==  PRESS_B)      {          yVelocity_  =  JUMP_VELOCITY;          setGraphics(IMAGE_JUMP);      }   }   –  Vấn  đề????
  • 13. Sửa bug1 void  Heroine::handleInput(Input  input){      if  (input  ==  PRESS_B){          if  (!isJumping_){              isJumping_  =  true;              //  Jump...          }   •  -­‐>  OK   •  Muốn  thêm  động  tác  ngồi???  
  • 14. Thêm  trạng  thái •  Thêm  if   void  Heroine::handleInput(Input  input){      if  (input  ==  PRESS_B)  {          //  Jump  if  not  jumping...      }  else  if  (input  ==  PRESS_DOWN){          if  (!isJumping_)        {              setGraphics(IMAGE_DUCK);          }      }    else  if  (input  ==  RELEASE_DOWN)    {          setGraphics(IMAGE_STAND);      }   } •  Any  problem??
  • 15. Thêm  trạng  thái •  Thêm  if   void  Heroine::handleInput(Input  input){      if  (input  ==  PRESS_B)  {          //  Jump  if  not  jumping...      }  else  if  (input  ==  PRESS_DOWN){          if  (!isJumping_)        {              setGraphics(IMAGE_DUCK);          }      }    else  if  (input  ==  RELEASE_DOWN)    {          setGraphics(IMAGE_STAND);      }   }     •  Any  problem??   –  Ngồi  -­‐>  ấn  nhảy,  thả  phím  down  -­‐>  đứng  dậy  trên   không
  • 16. Sửa bug2 •  Thêm  checker   void  Heroine::handleInput(Input  input){      if  (input  ==  PRESS_B)    {          if  (!isJumping_  &&  !isDucking_)        {              //  Jump...          }      }    else  if  (input  ==  PRESS_DOWN)    {          if  (!isJumping_)        {              isDucking_  =  true;              setGraphics(IMAGE_DUCK);          }      }    else  if  (input  ==  RELEASE_DOWN)    {          if  (isDucking_){              isDucking_  =  false;              setGraphics(IMAGE_STAND);          }      }   }  
  • 17. Thêm chức năng •  Tấn  công  khi  đang  nhảy  và  ấn  DOWN  
  • 18. Thêm chức năng •  Tấn  công  khi  đang  nhảy  và  ấn  DOWN   •  Đặt  bom  khi  ngồi  lâu   •  …………………    
  • 19. Thêm chức năng •  Tấn  công  khi  đang  nhảy  và  ấn  DOWN   •  Đặt  bom  khi  ngồi  lâu   •  …………………   •  -­‐>  quá  nhiều  checker,  flag,  countdown  
  • 21. Finite state machine •  Enum  +  if   enum  State   {      STATE_STANDING,      STATE_JUMPING,      STATE_DUCKING,      STATE_DIVING   }; void  Heroine::handleInput(Input  input)   {      switch  (state_)      {          case  STATE_STANDING:              if  (input  ==  PRESS_B)              {                  state_  =  STATE_JUMPING;                  yVelocity_  =  JUMP_VELOCITY;                  setGraphics(IMAGE_JUMP);              }              else  if  (input  ==  PRESS_DOWN)              {                  state_  =  STATE_DUCKING;                  setGraphics(IMAGE_DUCK);              }              break;   …………………………………
  • 22. Finite state machine •  Enum  +  if   enum  State   {      STATE_STANDING,      STATE_JUMPING,      STATE_DUCKING,      STATE_DIVING   }; void  Heroine::handleInput(Input  input)   {      switch  (state_)      {          case  STATE_STANDING:              if  (input  ==  PRESS_B)              {                  state_  =  STATE_JUMPING;                  yVelocity_  =  JUMP_VELOCITY;                  setGraphics(IMAGE_JUMP);              }              else  if  (input  ==  PRESS_DOWN)              {                  state_  =  STATE_DUCKING;                  setGraphics(IMAGE_DUCK);              }              break;   ………………………………… Ø  Số  lượng  state  tăng   Ø  Mỗi  state  có  dữ  liệu  riêng   l  Countdown   l  Timing
  • 23. State  pa)ern •  Mỗi  state  được  implement  riêng   •  State  <  -­‐-­‐  >  Behavior   •  StateInterface class  HeroineState   {   public:      virtual  void  handleInput(Heroine&  heroine,  Input  input)  {}      virtual  void  update(Heroine&  heroine)  {}      virtual  void  enter(Heroine&  heroine){}   };
  • 24. class  DuckingState  :  public  HeroineState{   public:      DuckingState()    :  chargeTime_(0)    {}        void  handleInput(Heroine&  heroine,  Input  input)  {          if  (input  ==  RELEASE_DOWN)        {              //  Change  to  standing  state...              heroine.setGraphics(IMAGE_STAND);          }      }      void  update(Heroine&  heroine)  {          chargeTime_++;          if  (chargeTime_  >  MAX_CHARGE){              heroine.superBomb();          }      }   private:      int  chargeTime_;   };
  • 25. class  Heroine   {   public:      void  handleInput(Input  input)      {          state_-­‐>handleInput(*this,  input);      }        void  update()      {          state_-­‐>update(*this);      }          void  sw^chState(HeroineState*  new_state)      {          state_    =    new_state;          state_-­‐>enter  (*this);      }            //  Other  methods...   private:      HeroineState*  state_;   };
  • 26. Các  state  khác •  Standing,  Jumping,  DivingState
  • 27. Hoàn cảnh sử dụng •  Hành  vi  phụ  thuộc  vào  một  vài  trạng  thái  của   object   •  Phân  chia  được  hoạt  động  của  object  ra   những  state  khác  biệt,  cụ  thể   •  Object  phản  hồi  theo  những  chuỗi  input  hoặc   sự  kiện
  • 28. Nhận xét •  Thêm  state  dễ  dàng   –  Sơ  đồ  chuyển  trạng  thái   –  Tách  rời  các  state   •  Quản  lý  giữa  các  state  rõ  ràng   –  Bỏ  flag,  checker….
  • 30. Thực hành •  Nhân  vật  có  3  trạng  thái   –  Đứng   –  Chạy  chậm   –  Chạy  nhanh   •  KeyboardDelegate   •  StatePa)ern