开发者

c# method parameters API design [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

Closed 4 years ago.

Improve this question 开发者_开发技巧

I want to discuss API method design.

We are building a c# dll that will be responsible of some machine actions (like open door, close door etc.) I want to expose to the client one function (let's call it "doAction(parameters...)"). This function will get one parameter, and the parameter is the type of the action (an argument).

My question is: what is the best way to design the argument and if you can give me links to examples. It can be aclass of const. strings or class of enums, anything that can be professionally designed and yet easy to use.

Thanks.


Have a look at Action Delegate

Other than that, you might want to implement an IAction interface with a DoAction Method.

So, something like

interface IAction
{
    void DoMethod();
}

and

public class OpenDoor : IAction
{
    public void DoMethod()
    {
        //Open the door
    }
}


It seems more natural to expose each of these as a separate method, as in:

public class Machine
{
    public void OpenDoor();
    public void CloseDoor();
    // other methods
}

This would be used as follows:

Machine NewMachine = new Machine();
NewMachine.OpenDoor();
NewMachine.CloseDoor();

That code is very readable.

However, if you're really interested in doing it all from one method, then an enumerated value can suffice:

public class Machine
{
    public enum Action
    {
        OpenDoor,
        CloseDoor,
        // other values
    }

    public void DoAction(Action ActionToDo)
    {
        switch(ActionToDo)
        {
            case OpenDoor:
                // open the door
                break;
            case CloseDoor:
                // close the door
                break;
            default:
                break;
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜