How to generate a Program template by generating an abstract class

Posted by Byron-Lim Timothy Steffan on Stack Overflow See other posts from Stack Overflow or by Byron-Lim Timothy Steffan
Published on 2012-03-15T15:13:22Z Indexed on 2012/03/21 11:29 UTC
Read the original article Hit count: 338

Filed under:
|
|

i have the following problem.

The 1st step is to implement a program, which follows a specific protocol on startup. Therefore, functions as onInit, onConfigRequest, etc. will be necessary. (These are triggered e.g. by incoming message on a TCP Port)

My goal is to generate a class for example abstract one, which has abstract functions as onInit(), etc. A programmer should just inherit from this base class and should merely override these abstract functions of the base class.

The rest as of the protocol e.g. should be simply handled in the background (using the code of the base class) and should not need to appear in the programmers code.

What is the correct design strategy for such tasks? and how do I deal with, that the static main method is not inheritable? What are the key-tags for this problem? (I have problem searching for a solution since I lack clear statements on this problem)

Goal is to create some sort of library/class, which - included in ones code - results in executables following the protocol.

EDIT (new explanation):

Okay let me try to explain more detailled:

In this case programs should be clients within a client server architecture. We have a client server connection via TCP/IP. Each program needs to follow a specific protocol upon program start:

As soon as my program starts and gets connected to the server it will receive an Init Message (TcpClient), when this happens it should trigger the function onInit(). (Should this be implemented by an event system?) After onInit() a acknowledgement message should be sent to the server. Afterwards there are some other steps as e.g. a config message from the server which triggers an onConfig and so on. Let's concentrate on the onInit function.

The idea is, that onInit (and onConfig and so on) should be the only functions the programmer should edit while the overall protocol messaging is hidden for him.

Therefore, I thought using an abstract class with the abstract methods onInit(), onConfig() in it should be the right thing. The static Main class I would like to hide, since within it e.g. there will be some part which connects to the tcp port, which reacts on the Init Message and which will call the onInit function. 2 problems here: 1. the static main class cant be inherited, isn it? 2. I cannot call abstract functions from the main class in the abstract master class.

Let me give an Pseudo-example for my ideas:

 public abstract class MasterClass
{ 
    static void Main(string[] args){

         1. open TCP connection
         2. waiting for Init Message from server
         3. onInit();
         4. Send Acknowledgement, that Init Routine has ended successfully
         5. waiting for Config message from server
         6.....
    }

    public abstract void onInit();
    public abstract void onConfig();
}

I hope you get the idea now! The programmer should afterwards inherit from this masterclass and merely need to edit the functions onInit and so on.

Is this way possible? How? What else do you recommend for solving this?

EDIT: The strategy ideo provided below is a good one! Check out my comment on that.

© Stack Overflow or respective owner

Related posts about c#

Related posts about design-patterns