Purpose
Make a unified error handling class
Requirements
C/s
Design
Since System. Windows. Forms. MessageDrive and System. Web. MessageDrive are required
I used the existing namespace in the system, which saves the trouble of referencing it separately.
Since they have the same functions, there must be an interface to constrain two classes of System. IMessageDrive.
I also thought that although the implementation functions are different, the two classes have some public functions, so I think they should all be derived from another parent class. System. MessageDrive
Specific design:
Interface
Namespace System
{
Interface IMessageDrive
{
Void Exception (Exception e );
Void Message (Exception e );
Void Warning (Exception e );
}
Enum MessageType
{
Message,
Warning,
Exception,
Notify,
News,
Push
}
}
Parent class
Namespace System
{
Public class MessageDrive
{
Public virtual void Exception (Exception e)
{
}
Public virtual void Message (Exception e)
{
}
Public virtual void Warning (Exception e)
{
}
}
}
Cs subclass
Namespace System. Windows. Forms
{
Public class MessageDrive: System. MessageDrive, IMessageDrive
{
Public override void Exception (Exception e)
{
Base. Exception (e );
MessageBox. Show (e. Message );
MessageDialog dialog = new MessageDialog (MessageType. Exception );
}
Public override void Message (Exception e)
{
MessageBox. Show (e. Message );
MessageDialog dialog = new MessageDialog (MessageType. Message );
}
Public override void Warning (Exception e)
{
MessageBox. Show (e. Message );
MessageDialog dialog = new MessageDialog (MessageType. Warning );
}
}
}
Bs subclass
Namespace System. Web
{
Public class MessageDrive: System. MessageDrive, IMessageDrive
{
Public override void Exception (Exception e)
{
}
Public override void Message (Exception e)
{
}
Public override void Warning (Exception e)
{
}
}
}
Questions:
There is no problem in writing this way, but there is a problem. I think these two classes should be static classes. At least the necessary functions are static functions. But in this way, the problem arises. base must be used in non-static functions and classes, so I began to seek a solution. my goal is to call the general method written in the parent class.
Method 1: directly instantiate the parent class and then call?
Does the parent-child relationship have any significance?
Method 2: Do not use static functions?
This may cause discomfort for the user, because the purpose of this class is to replace class functions such as MessageBox. Show (). If you want to instantiate it, it will obviously cause discomfort.
Method 3: The subclass re-implements the function in the parent class?
In this way, neither the object-oriented nor high cohesion nor low coupling are met, and neither of the two classes can be compiled.
I tend to be in method 1, but I still have doubts and cannot find a good method. So I designed it first. I hope someone can provide me with a good suggestion.