开发者

implementing the interface

开发者 https://www.devze.com 2023-03-23 02:38 出处:网络
public interface IBSTNode { IBSTNode Left { get; } IBSTNode Right { get; } int Value { get; } IDictionary<string, object> DataFields { get; set; }
public interface IBSTNode
    {
        IBSTNode Left { get; }
        IBSTNode Right { get; }
        int Value { get; }        
        IDictionary<string, object> DataFields { get; set; }
        void Insert(IBSTNode node);
        string Print();   }  

this is the interface design that I have to use. BUT I have to have the Print() function to be implemented in a way that i开发者_高级运维t can take user input like

Pre-order Post-order

How can I do that?


so, have print take a parameter, perhaps an enum:

enum PrintMethod {Inorder, Postorder};

and

string Print(PrintMethod p);

If you cannot change the interface, you could do the unthinkable, and have Print read from stdin... *shudder *


The print function in the interface is paramterless it however does not limit you from adding functions that are not in the interface to the concrete class. An interface assures a caller that a specified set of functionality is provided not that there cannot be more functionality.


Without having the user specifically know about your class, you can't do it with that interface. IBSTNode doesn't provide a Print method with arguments, so there's no way to pass the information via the call.

If they're specifically working with an instance of your class (as opposed to just an instance of some IBSTNode), you can define an additional method which takes these extra arguments (but you still need to implement the parameterless Print() method as well, due to the interface).

If absolutely necessary, you can also hack it and define a static variable somewhere (either in your own class, or perhaps in some new PrintOptions class) which they can set before calling Print() - your method can then read this variable and act accordingly. This option should be considered the absolute last resort, however.

Of course, this is assuming Print is supposed to be doing the traversal in the first place - that is, calling Print on a node will print out the children as well. A much better choice would be to move the traversal out of the IBSTNode, and only having Print print the individual node - and it is entirely likely that this is indeed how Print is supposed to work.

Since you have full access to the internals of an IBSTNode, you can write any kind of traversal you wish (assuming one isn't already defined), and if Print isn't just supposed to print the value of the individual node, you can even print Value on your own as part of that traversal.


You could extend the interface with extension methods.

If it is possible to implement the print methods as a helper method that operates on the IBSTNode instance via it's interface, then you could add additional print methods as extension methods.

This means the methods could be called directly on the node object when referenced as a IBSTNode, rather than forcing you to cast to a concrete type with overloads defined.

public static class IBSTNodeExtensions
{
    public static string PrintPreOrder(this IBSTNode node)
    {
        ...
    }

    public static string PrintPreOrder(this IBSTNode node)
    {
        ...
    }
}

Above I've shown two extension methods, but you could have a single one that took a parameter.

0

精彩评论

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