State of grinding design mode 1-Follow cc Design Series

Source: Internet
Author: User

18.1 scenario problems 18.1.1 online voting

For an online voting application, you can only vote for one vote for the same user. If a user repeatedly votes and votes more than five times, it is determined that the vote is malicious, to cancel the user's voting qualification, of course, the user's vote should also be canceled. If a user votes more than 8 times, the user is blacklisted and cannot log on to or use the system.

How can this function be implemented?

18.1.2 no-use Solution

Analysis of the above functions, in order to control user voting, You need to record the user's voting records, but also record the number of user voting times, in order to be simple, directly use two maps to record.

There are four situations in the voting process:

First, users vote normally. Second, users vote normally, intentionally or unintentionally repeatedly. Third, users vote maliciously. Fourth, blacklisted users.

In these cases, the corresponding processing is different. Let's look at the code. The sample code is as follows:

/**

* Vote Management

*/

Public class VoteManager {

/**

* Record the user's voting result, Map Corresponding Map <用户名称,投票的选项>

*/

Private Map MapVote =

New HashMap ();

/**

* Record the number of user votes, Map Corresponding Map <用户名称,投票的次数>

*/

Private Map MapVoteCount =

New HashMap ();

/**

* Vote

* @ Param user refers to the user name for simplicity.

* @ Param voteItem Option

*/

Public void vote (String user, String voteItem ){

// 1: Increase the number of votes for this user first

// Retrieve the number of votes from the record

Integer oldVoteCount = mapVoteCount. get (user );

If (oldVoteCount = null ){

OldVoteCount = 0;

}

OldVoteCount = oldVoteCount + 1;

MapVoteCount. put (user, oldVoteCount );

// 2: determine the user's voting type, whether it is normal voting, repeated voting, malicious Voting

// Blacklist, and then perform corresponding operations based on the voting type

If (oldVoteCount = 1 ){

// Vote normally

// Record to the voting record

MapVote. put (user, voteItem );

System.Out. Println ("Congratulations, your vote succeeded ");

} Else if (oldVoteCount> 1 & oldVoteCount <5 ){

// Repeated voting

// Do not process it for the moment

System.Out. Println ("Please do not vote again ");

} Else if (oldVoteCount> = 5 & oldVoteCount <8 ){

// Malicious Voting

// Cancel the user's voting qualification and cancel the voting record

String s = mapVote. get (user );

If (s! = Null ){

MapVote. remove (user );

}

System.Out. Println ("You have malicious votes, disqualified from voting ");

} Else if (oldVoteCount> = 8 ){

// Blacklist

// Log on to the blacklist. logon to the system is prohibited.

System.Out. Println ("Access to the blacklist will prohibit logon and use of the system ");

}

}

}

Write a client to test whether it meets the functional requirements. The sample code is as follows:

Public class Client {

Public static void main (String [] args ){

VoteManager vm = new VoteManager ();

For (int I = 0; I <8; I ++ ){

Vm. vote ("u1", "");

}

}

}

The running result is as follows:

Congratulations! You voted successfully.

Please do not vote repeatedly

Please do not vote repeatedly

Please do not vote repeatedly

You have malicious ticket brushing and are disqualified from voting

You have malicious ticket brushing and are disqualified from voting

You have malicious ticket brushing and are disqualified from voting

Entering the blacklist will prohibit you from logging on to and using the system.

18.1.3 what's the problem?

It looks simple, isn't it? Fortunately, this is just a signal. Otherwise, you may think about how many judgments are made in the vote () method, and the processing of each judgment function is put together. Is it a little messy, it is a hodgedge. If every function is fully implemented, the vote () method will be very long.

One problem is: If you want to modify the specific function corresponding to a certain voting condition, you need to find the corresponding code block in the hodgedge and then modify it.

Another problem is: If you want to add new features, for example, if you want to vote for more than 8 times but less than 10 times, give a chance To Deny logon and use of the system for three days, permanent Account blocking. What should I do? Then you need to change the voting Management source code. In the above if-else structure, add an else if block for processing.

In either case, you need to find the desired part in a lot of control code and then modify it. This is never a good method, so how can we achieve it: it is easy to add new functions to the vote () method, and to easily modify existing functions?

---------------------------------------------------------------------------

The original content of the Private School Online Learning Network follows the grinding design mode of the cc design series.

Grinding Design Discussion group [252780326]

Original content. For more information, see [http://sishuok.com/forum/blogpost/list/5619.html]

---------------------------------------------------------------------------

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.