OO about a certain C # computer question

Source: Internet
Author: User

Two days ago, in the garden, someone came up with an idea about a C # computer question, which roughly meant programming with the idea of OO, followed by another friend, I also wrote my own answer. This friend is very good. From class diagrams, interfaces, encapsulation, and polymorphism, they are all implemented one by one. I really admire it, but it is a bit overdesigned, then there was another prawns who completed their own oo answers and listed all generic, mutable, and immutable items. It was really admirable, but I think maybe I understood it wrong, however, I think all three of you have deviated from the subject and OO, but you have used the features of OO.

Question

17 people are in a circle, starting from the first person to report data, reporting to 3 and leaving until the last person is left to do this with the object-oriented thinking.

Comments

I am not a master and I am not qualified to comment on everyone. I just put forward my own opinions.

Joyaspx only implements one object, that is, a person, but puts "Exit 3" in the execution method. The person object also needs to know his brother and brother, maybe joyaspx is not enough time on the machine. I feel that this method is not object-oriented development, but it is still solved by problem-oriented.

Ooli has to admire that everything about Oo, from design to interface to implementation, is actually over-designed, but OO in it is not flattering, such as the initial data, when hard encoding is used, the first person also needs to give him a number and assign a state to the person object to determine whether to remove the object based on the state. His exit is also very interesting, cut yourself out .... Tell your brother that you do not have me, and your brother is my younger brother. Then I want to ask, where have I been?

Yangq, this guy, I have to sayProgramIt is really not object-oriented, it is completely process-oriented development. Although you use generic, it does not mean that generic is object-oriented development. I hope the middleware can continue to work hard, understand and understand what object-oriented development is.

My understanding

The question is very short. We should also understand him very well. There is only one object in total, that is, person. This is correct and everyone thinks of it. However, in this question, I did not say that I need to know who the next person is and who the last person is, because they are playing games and reporting games, "Exit 3" is just a rule of the game. Not everyone needs to play this game, but we only need 17 people. So for the person object, perv and next, including exit actions, are not in the category of "people". They are only "people" in the scenario of "reporting game, for OO programming, everything is an object. That is to say, games are also an object.

This question is very subtle. If Oo is not required, it should be a data structure.AlgorithmThe problem, that is, what the previous big brother said, is what structure I cannot call. I think it is a ring, and everyone pulls it in a circle by hand.

Start

After understanding the question, we know that we need two objects: person and game. The game must depend on people. Because there are no people, the game won't start, and people don't need to know the game, only participants can understand the game. Let's take a look at the definition of the person object:

 Public class  Person { Public Person ( Int Personid ){ This . Personid = personid ;}Public int Personid { Get ; Set ;} Public void Say (){ If ( This . Said! = Null ) This . Said ( This , New  Personeventargs (This ));} Public event  Eventhandler < Personeventargs > Said ;}

Each person has his/her own ID. Because it is a demo or name, I will not add it. There is a way to say, because we need to talk about the number of messages, and we do not execute anything. If we need the content, we can add it ourselves. For people, each time we speak, we do not need to respond to ourselves or others, but I need to notify a certain object that I am talking to, even if you are talking to the wall, you still notified the wall, "Hi, the wall, I spoke", so I added a delegate event to send a notification to an object, in this question, I will notify the "game" object, which should belong to the notification mode.

Personeventargs:

Public classPersoneventargs:Eventargs{PublicPersoneventargs (PersonPerson ){This. Person = person ;}PublicPersonPerson {Get;Set;}}

Next we will focus on the game. For the rest of us (except for the players in the game), I am a referee. I only need to say that the game starts. When a certain condition is reached, the game is over. So we only need to issue commands to get the game started.

GameGame =NewGame(17 );// 17 indicates the number of participantsGame. Start ();

This is the interface for program testing. It is relatively simple to construct this game object, because we only need to tell it how many people participate, and then the game starts to OK, we only need to expose a constructor, and a start method is enough.

 
Public classGame{PublicGame (IntPersonnumber ){}Public voidStart (){}}

In this way, we complete the encapsulation. For the external, we only need to know that this is enough. Next, let's look at what else we need in game.

Since we need people and many people play games, there must be a players attribute. When the game starts, we need to start reporting data. At this time, we need to report data by one person, what is the result of the report? It is a game status (Note: it is the object status, not the type). Let's look at the game class I wrote:

 Public class  Game { Private int Currentnumber = 0; Private  List < Person > Currentquitpersons = New  List < Person > (); Private  List < Person > Players { Get ; Set ;} Private event  Eventhandler < Personeventargs > Gameover; Public Game ( Int Personnumber) {ready (personnumber );} Public void Start () {++ currentnumber; This . Gameover + = New  Eventhandler < Personeventargs > (Game_gameover); go ();} Private void Ready ( Int Personnumber ){ This . Players = New  List < Person > (Personnumber ); For ( Int I = 0; I <personnumber; I ++ ){ Person Person = New  Person (I); person. Said + =New  Eventhandler < Personeventargs > (Person_said ); This . Players. Add (person );}} Private void Go (){ VaR Persons = This . Players; persons. foreach (P => {P. Say (); currentnumber ++ ;}); If ( This . Players. Count> 1 ){ If (Currentquitpersons. Any ()){ This . Players. removeall (P => currentquitpersons. Contains (p); currentquitpersons. Clear () ;}go ();} Else { This . Gameover ( This , New  Personeventargs ( This . Players. First ()));}} Private void Person_said ( Object Sender, Personeventargs E ){If (Currentnumber % 3 = 0) {currentquitpersons. Add (E. person ); Console . Writeline ( "The player quit, ID: {0}, currentnumber: {1 }" , E. Person. personid, currentnumber );}} Private void Game_gameover ( Object Sender, Personeventargs E ){ Console . Writeline ( "Last person's person ID is {0 }" , E. Person. personid ); Console . Writeline ( "Game over ." );}}

Sorry, it's a long time. Please read it with patience.

There is a currentnumber field that represents the current status of the game object, that is, a number of the number. Players: the participants. when constructing the function, they will prepare for the initialization of this players attribute. for each person, we will assign an ID, then, a person_said delegate will be delegated to let the game know that the play number is returned, and then an action will be reflected based on the number. In this question, "exit from 3 ".

After everything is ready, we start. At the beginning, from 1, the current number is changed to 1 (to distinguish the result, I set the initial serial number of the person, it starts from 0). In the go method, each person starts to report data. If there is still one person left, the game is over, let's take a look at the running results.

OK. We expected the program to end and run correctly.

Summary

This time, we have the opportunity to experience object-oriented programming. In fact, the question is not very difficult. It depends on how we understand it, it is not to say that the feature of object-oriented is an object-oriented development. This is totally a misunderstanding, just as you have used one by one mode in the project, mode madman does not mean that your program is a mode program. The mode is gradually formed after development, so that we can better expand and encapsulate the program, so that everyone can better understand (such as UML), so the same is true for Object-Oriented systems. Its features are entirely because people have discovered these features during development and listed them, and formed a standard document, so that everyone can quickly get started with object-oriented, not to say that with these features, it is object-oriented development. In other words, the characteristics of a singer can sing, but they are not the same as those of a singer.

Insufficient

I can't say that my answers are perfect. I just want to take this opportunity to explain some of my views and opinions. There are also some shortcomings, because I have not considered algorithms at all, and I have not considered performance at all. In addition, there is also a failure, that is, the currentquitpersons field. I originally thought that at the time of person_said, players was directly exited at 3, but after removing it, the serial numbers will be directly re-arranged, resulting in errors. So using this field, I will remove the players to be removed at the end of each round, which ensures the continuity of the report number, I am really upset. I don't know if you have any good solutions?

Related Article

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.