Refuse API

I wrote a framework for myself in C # and my framework runs on top of Xna frameworks. In my application code that uses this framework, I often need to include references to my Xna framework and framework. In most cases, these Xna references only include structural classes such as Vector2 and Rectangle. Because of this, I try to keep my class names consistent with Xna class names. It can get tedious and even confusing for me when I have classes where I came up with a similar name but not the same as Xna. (i.e. GamePadDevice and GamePad)

I had an idea lately, but I don't know if it's worth it. I only use a few, 5 or 6 structures in the whole structure. Is it worth abstracting these structures so that my application code only has to deal with my map? I could do this by writing my own versions of structs or by inheriting from them, or someone might suggest a better way. Is the overhead simplifying my application code?

+2


source to share


2 answers


I don't know how deeply your framework continues to replace the XNA layer, but if the user no longer has XNA contact and "just" uses your framework, then it would be better if your framework only exposes its own structures to the client. After all, can you ditch XNA one day and support XNB or XNC?



If you write a mapper in your structure that translates you will be able to do it later

+3


source


A good way to abstract away from third-party libraries is to inject the interfaces with the required methods and create the object through the Factory. Especially in your case, when you are just using a bunch of classes, this is the best choice in my opinion.

Let's say we need to sanitize the html code and find a kickass library that can do this for us, but we don't want to stick with this library - maybe someone will use a super kickass lib someday we would like to use.

It can be implemented like this (code in java)

First, defining the interface:

public interface HtmlSanitizer {
      String sanitizeHtml(String html);
}

      

Then we create a concrete implementation for our interface based on our sanitizing banass

public class MyKickassHtmlSanitizer implements HtmlSanitizer {
    private com.foolib.kickass.html.Sanitizer delegate;

    public MyKickassHtmlSanitizer() {
      this.delegate= new com.foolib.kickass.html.Sanitizer();
    }

   public String sanitizeHtml(String html) {
    return delegate.kickassSanitizeHtml(html);
   }
} 

      



Now the Factory that creates our disinfectant

public class HtmlSanitizerFactory {
      private static final HtmlSanitizerFactory instance = new HtmlSanitizerFactory();

      private HtmlSanitizerFactory() {}

      public static HtmlSanitizerFactory get() { return instance;}
      public HtmlSanitizer getSanitizer() {
          return new MyKickassHtmlSanitizer();
      }
} 

      

To get an instance, just use:

 HtmlSanitizerFactory.get().getSanitizer();

      

While Factory may provide static methods or not, may be solid or not. A matter of taste and use.

Now your dependency on the kickass libs sanitizer is reduced to one point, and to change the implementation just type in SuperKickassHtmlSanitizer and return Factory.

Hope it helps :-)

0


source







All Articles