voting设计模式

Voting 模式

1.语境:这个设计模式是从多人投票的模型中抽象出来的,其中参与者voter是一个个投票者,每个投票者都维护一个状态COUNTVOTE()操作可以改变COUNT的状态,COUNTER是计票器,每个投票者可以通过ATTACH()DETACH()操作与COUNTER联系起来,同时COUNTER通知每个登记的投票者去投票,然后通过COUNTWINNER()操作来对得票进行统计,其它用户通过GETCOUNT()操作得到最终投票结果。其中所有的COUNT成员变量都在同一个取值范围。与观察者模式很相像,不同在于观察者模式强调维护两者的状态一致性,而VOTING模式强调并行处理并对处理结果进行统计等处理。

2.意图:同一件事情交由多个不同的处理者(相同的接口,不同的实现)处理,然后对结果进行汇总处理。

3.效果:
1.COUNTERVOTER的抽象耦合

每个COUNTER所知道的仅仅是它有一系列VOTER,每个都符合抽象的VOTER简单的接口。COUNTER不知道任何一个VOTER属于哪个具体的类,这样两者之间的耦合是最小的。

2.支持广播通信


注:这个模式是我2年前想到的,后来看的书多了才知道这其实是“主控-从属”模式的一个变体

阅读更多

Australian Voting

09-09

Australian ballots require that the voter rank the candidates in order of choice. Initially only the first choices are counted and if one candidate receives more than 50% of the vote, that candidate is elected. If no candidate receives more than 50%, all candidates tied for the lowest number of votes are eliminated. Ballots ranking these candidates first are recounted in favour of their highest ranked candidate who has not been eliminated. This process continues [that is, the lowest candidate is eliminated and each ballot is counted in favour of its ranked non-eliminated candidate] until one candidate receives more than 50% of the vote or until all candidates are tied.nnnInputnnThe first line of input is an integer n <= 20 indicating the number of candidates. The next n lines consist of the names of the candidates in order. Names may be up to 80 characters in length and may contain any printable characters. The next line contains an integer m <= 1000, and m lines follow; each contains the contents of a ballot. That is, each contains the numbers from 1 to n in some order. The first number indicates the candidate of first choice; the second number indicates candidate of second choice, and so on.nnProcess to the end of file.nnnOutputnnThe Output consists of either a single line containing the name of the winner or several lines containing the names of the candidates who tied.nnnSample Inputnn3nJohn DoenJane SmithnSirhan Sirhann5n1 2 3n2 1 3n2 3 1n1 2 3n3 1 2nnnSample OutputnnJohn Doe

Transferable Voting

03-08

The Transferable Vote system for determining the winner of an election requires that a successful candidate obtain an absolute majority of the votes cast, even when there are more than two candidates. To achieve this goal, each voter completes his or her ballot by listing all the candidates in order of preference. Thus if there are five candidates for a single position, each voter's ballot must contain five choices, from first choice to fifth choice.nIn this problem you are to determine the winner, if any, of elections using the Transferable Vote system. If there are c candidates for the single position, then each voter's ballot will include c distinct choices, corresponding to identifying the voter's first place, second place, , and nth place selections. Invalid ballots will be discarded, but their presence will be noted. A ballot is invalid if a voter's choices are not distinct (choosing the same candidate as first and second choice isn't permitted) or if any of the choices aren't legal (that is, in the range 1 to c).nnFor each election candidates will be assigned sequential numbers starting with 1. The maximum number of voters in this problem will be 100, and the maximum number of candidates will be 5.nn nn Table A Table Bn ------------------------------- --------- n Voter First Second Thirdn Choice Choice Choicen 1 1 2 4n 2 1 3 2 1 3 2n 3 3 2 1 3 2 1n 4 3 2 1 3 2 1n 5 1 2 3 1 2 3n 6 2 3 1 3 1n 7 3 2 1 3 2 1n 8 3 1 1n 9 3 2 1 3 2 1n 10 1 2 3 1 2 3n 11 1 3 2 1 3 2n 12 2 3 1 3 1n nnConsider a very small election. In Table A are the votes from the 12 voters for the three candidates. Voters 1 and 8 cast invalid ballots; they will not be counted. This leaves 10 valid ballots, so a winning candidate will require at least 6 votes (the least integer value greater than half the number of valid ballots) to win. Candidates 1 and 3 each have 4 first choice votes, and candidate 2 has two. There is no majority, so the candidate with the fewest first choice votes, candidate 2, is eliminated. If there had been several candidates with the fewest first choice votes, any of them, selected at random, could be selected for elimination.nnWith candidate 2 eliminated, we advance the second and third choice candidates from those voters who voted for candidate 2 as their first choice. The result of this action is shown in Table B. Now candidate 3 has picked up 2 additional votes, giving a total of 6. This is sufficient for election. Note that if voter 12 had cast the ballot ``2 1 3" then there would have been a tie between candidates 1 and 3.nnnInputnnThere will be one or more elections to consider. Each will begin with a line containing the number of candidates and the number of voters, c and n. Data for the last election will be followed by a line containing two zeroes.nnFollowing the first line for each election will be n additional lines each containing the choices from a single ballot. Each line will contain only c non-negative integers separated by whitespace.nnnOutputnnFor each election, print a line identifying the election number (they are numbered sequentially starting with 1). If there were any invalid ballots, print an additional line specifying the number of such. Finally, print a line indicating the winner of the election, if any, or indication of a tie; be certain to identify the candidates who are tied. Look at the samples below for the exact output format.nnnSample Inputnn3 12n1 2 4n1 3 2n3 2 1n3 2 1n1 2 3n2 3 1n3 2 1n3 1 1n3 2 1n1 2 3n1 3 2n2 3 1n3 12n1 2 4n1 3 2n3 2 1n3 2 1n1 2 3n2 3 1n3 2 1n3 1 1n3 2 1n1 2 3n1 3 2n2 1 3n4 15n4 3 1 2n4 1 2 3n3 1 4 2n1 3 2 4n4 1 2 3n3 4 2 1n2 4 3 1n3 2 1 4n3 1 4 2n1 4 2 3n3 4 1 2n3 2 1 4n4 1 3 2n3 2 1 4n4 2 1 4n0 0nnnSample OutputnnElection #1n 2 bad ballot(s)n Candidate 3 is elected.nElection #2n 2 bad ballot(s)n The following candidates are tied: 1 3nElection #3n 1 bad ballot(s)n Candidate 3 is elected.

没有更多推荐了,返回首页