Summary:
When I read the interview questions today, I found it interesting to have a proper question. The original question is as follows:
Program Design: The Cat shouted, and all the mice started to escape, and the host was awakened. (C # language)
Requirement: 1. The interaction between the mouse and the host must be active.
2. Considering scalability, the call of a cat may cause other association effects.
First of all, it is quite simple to see this question. The idea is as follows:
// Define the cat class, which is called an action. Considering the linkage effect, call the Awakening method of the primary human and the escape method of the mouse class at the same time.
// Defines the primary human, with a wake-up method
// Defines the mouse class and has the escape Method
// Define the main call function and call the actions of the cat class
--------------------------------
The above function enables the cat to wake up and escape from the mouse; however
"Requirement 2. Considering scalability, the call of a cat may cause other linkage effects ." This requirement is obviously not met.
What kind of design is scalable?
First, we will analyze whether there are any commonalities between the primary human and the mouse, and whether abstract methods can be extracted. We can see that after a cat is called, our program will print the prompt language of waking and escaping from the mouse, so can we abstract this output print action? Our program needs to be slightly modified as follows:
// Define the abstract response class (waking up and escaping) -- output the action
// Define the primary human and inherit the self-response abstract class-implement the output action ("wake up the master ");
// Define the mouse class and inherit the self-response abstract class-implement the output action ("rat escape ");
When the program is written here, we will think, how can we link the host and mouse together when the cat calls it?
In the cat class, the main human and mouse classes are directly instantiated and then called directly. This method is not what we want, but what we need is a loosely coupled method. Of course, there are many implementation methods, for example, use "intermediate components ".
I don't know whether a student remembers to delegate the ability to bind multiple methods to an event. In this way, the host will be awake and the mouse will be bound to the event first, then, you can activate this event in the cat category to trigger "wake up", "Mouse escape", and other actions, even if you wake up your neighbor, you can easily implement it.
// Define the neighbor class and inherit the self-response abstract class-implement the output action ("neighbor waking ");
Then, bind the neighbor to the event;
Some may also ask how to define the delegation, and how to bind the host's waking up, mouse escaping, and other actions to the event?
Here I provide a way of thinking, you can also play it at will. I am binding the output action to the event in the constructor of the "reactive abstract class", and then in the subclass (Primary Human) the constructor of inherits the parent class, so that the sub-class implementation can be bound to the event. The mouse class and neighbor class are also in the same way;
In theory, there are so many codes (I wrote them directly on the website background, but I cannot define the format. Please forgive me ):
// Define the event Delegate class
Public delegate void SubEventHandler ();
Public abstract class Subject
{
// Define events based on Delegation
Public event SubEventHandler SubEvent;
// Scream
Protected void FireWay ()
{
If (this. SubEvent! = Null)
{
// Activate the event to trigger a series of actions. In the response abstract class, all actions must be bound to the delegate.
This. SubEvent ();
}
}
}
// Define the abstract response class -- the constructor binds the response action to the delegate.
Public abstract class AbsObject
{
Public AbsObject (Subject sub)
{
Sub. SubEvent + = new SubEventHandler (Response );
}
Public abstract void Response ();
}
// Define human inheritance response abstract class inheritance constructor, bind specific human to delegate
Public class people: AbsObject
{
Public people (Subject sub): base (sub ){}
Public override void Response ()
{
Console. WriteLine ("host waken ");
}
}
// Define mouse inheritance response abstract class inheritance constructor, bind the specific mouse to the Delegate
Public class mouse: AbsObject
{
Private string name = string. Empty;
Public mouse (string name, Subject sub)
: Base (sub)
{
This. name = name;
}
Public override void Response ()
{
Console. WriteLine (string. Format ("this name: {0}, mouse run", name ));
}
}
// Define the cat inheritance delegate class-the trigger event of the cat call
Public class cart: Subject
{
Public void cary ()
{
// Nickname
Console. WriteLine ("cart cary ");
// Linkage effect
This. FireWay ();
}
}
// Define the main function
Public class mainclass
{
Static void Main ()
{
Cart c = new cart ();
People p = new people (c );
Mouse m = new mouse ("Mickey", c );
Mouse m1 = new mouse ("Mickey", c );
C. cary ();
Console. ReadLine ();
}
}
Summary:
In fact, in Object-Oriented Programming, abstract extraction is very important. Abstract Functions and delegation can be combined to write beautiful code, loosely coupled, and highly aggregated, is the pursuit of program design.
Ps:
Don't be infatuated with brother. Brother is just a legend.