Today I saw two blog posts 《Some ideas about a C # computer questionAnd 《<Some thoughts on a C # computer question>Are the OO implementations of the Joseph ring.ArticleI was inspired, but there are still some differences between the implementation of the two and me, so I can't help but start by myself.
Since it is Oo, it must be in line with the OO principle. The first thing that comes to mind is the open and closed principle. I thought about what is changeable and what is immutable.
Variable part:
1. The length of the ring and the starting position (this is the most basic extension );
2. Find the next person location (three or five, or even three or two );
3. Find the operation done by a person (simply output or let him jump three hops and give a big red flower );
4. People are not necessarily in the circle, so can cats and dogs.
Unchanged parts:
1. The ring will certainly not change;
2. There must be a way to determine the next position;
3. After finding a person, no matter whether you give him a safflower, but he must make a column, or the game will not be over.
Based on the above analysis, the unchanged part is written into an abstract generic base class. Abstract is used because many changes are uncertain. Generic is used to prepare a cat and a dog.
Base Class
Public Abstract Class Joseph phusring < T >
{
Private Int Currentposition;
Protected List < T > Ring;
Public Joseph phusring ( int length, int startposition)
{< br> createring (length); /// place the structure of the ring in the subclass to complete the process. It should be regarded as the factory method mode.
This . currentposition = startposition;
}
Public void nextstep ()
{< br> currentposition = next (currentposition);
process (currentposition);
ring. removeat (currentposition); /// Columns are unchanged
}
Public VoidRuntoend ()
{
While(Ring. Count> 0)
{
Nextstep ();
}
}
Protected Abstract Int Next ( Int Current ); // There must be a way to find the next location, but you are not sure how to find it.
Protected Abstract Void Process ( Int Current ); // After finding it, I had to handle it. So I had to skip three hops or wear safflower.
Protected Abstract Void Createring ( Int Length );
}
The base class implements the core operations of the Joseph ring, and the subsequent derivation can be customized as needed.
Derived class
Public Class Joseph phusringint32: Joseph phusring < Int >
{
Public Joseph phusringint32 ( Int Length, Int Startposition)
: Base (Length, startposition)
{
}
Protected Override Int Next ( Int Current) // From 1 to 3
{
Return (Current + 2 ) % Base . Ring. count;
}
Protected Override VoidProcess (IntCurrent)//Simply output
{
Console. writeline (Base. Ring [current]. tostring ());
}
// The process of constructing a ring can only be completed in a derived class. If it is a circle of cats, you may have to name them one by one.
Protected Override Void Createring ( Int Length)
{
Int [] R = New Int [Length];
For ( Int I = 0 ; I < Length; ++ I)
R [I] = I + 1 ;
Base . Ring = New List < Int > (R );
}
}
This derived class completes the simplest and most traditional operations, and of course it can be extended more complex. Then, execute as required.
Run
Class Program
{
Static Public Void Main ()
{
Joseph ringint32 aring = New Joseph phusringint32 ( 17 , 0 );
Aring. runtoend ();
}
}
The results won't work 《<Some thoughts on a C # computer question>The same article.
The above is purely a one-on-one view. It may not be a good understanding of the open and closed principles. I also ask experts to give advice.