[Rebuilding to Mode-state pattern] rebuilding the scoring tactics of tennis to state Mode

Source: Internet
Author: User

I just reconstructed the scoring tactics of tennis to the state mode.
Process:
1) achieve simple scoring without leading (no AD) first, and reconstruct it to the state mode.
Tennis scoring
1.1) if the first player wins 4 points first, the player wins a game ).

1.2) scoring in each game is measured in a unique way in the tennis world: Winning 0, 1, 2, and 3 balls, with love, 15, 30, and 40 points respectively.

2) Consider scoring with leading conditions.
Tennis scoring
2.1) if the first player wins at least four points and at least two more matches than the opponent, the player wins a game ).
2.2) scoring in each game is measured in a unique way in the tennis world: Winning 0, 1, 2, and 3 balls, with love, 15, 30, and 40 points respectively.
2.3) if each contestant has won at least three balls and scored the same, each player is called "Deuce )".
2.4) if each contestant has already won at least three matches and one of them wins one more than the other, the contestant is called "advanced )".

The following are some experiences.
* 1) refactoring to a pattern is a little "carved" from the code without a pattern, driven by the pattern intent direction, rather than falsely dropping several classes in the pattern.
* 2) perform the following steps to refactor to the state mode:
* 2.1) First, use the if or switch statement to implement the business logic without considering the design mode.
* 2.2) create a context class and move the business logic to this place
* 2.3) create a State abstract class and delegate the business logic to the State abstract class by holding the State object in the context.
* 2.4) Implement specific business logic in the subclass of State to eliminate if or switch statements and save the next state to the state domain held by context, new status held as context

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.