What is meant by "implement a wrapper method"?
Asked Answered
L

6

11

I have been given a programming assignment and one of the things I have to do is implement method which a wrapper method which relies on another method to sort the coordinates from lowest to highest. I am unsure on what exactly is meant by implementing a wrapper method.

static void sortCoordsByZ(double[][] coords) {
    //implement the wrapper method for the recursive sort method. all work is done the recursive sort method
}

static void recursiveSort(double[][] coords, int lo, int hi) {
    //recursive sort method
}
Laureate answered 15/11, 2009 at 1:14 Comment(1)
@Dougman - it's not clear that this is homework (I for one get 'programming assignments' at work), so I'm removing the tag unless the questioner says otherwise. See meta.stackexchange.com/questions/11189/….Navicular
N
14

A wrapper method is an adapter or a façade; it provides an alternative interface for an existing method.

You've been asked to write a façade (facade) - to provide a simpler interface for clients that don't need to specify high and low values.

Navicular answered 15/11, 2009 at 1:17 Comment(0)
C
11

You're acting as a wrapper method right now by asking your assignment question on Stack Overflow!

A wrapper method answers a question by asking an "expert" method for the answer. Generally, it does three things:

  1. It frames the question in such a way that the "expert" method can understand it.
  2. It asks the "expert" method the question
  3. It does something easy to the answer to put it in the right format for the caller.

In your case, the "expert" method is recursiveSort(), and your sortCoordsByZ() method will have to call recursiveSort() with the right parameters, and then maybe do something with the answer before returning it.

Clare answered 15/11, 2009 at 3:16 Comment(0)
G
8

When you implement a wrapper method, you are effectively coding up a variant of an existing method, usually because the existing method doesn't satisfy your current requirements. The original method may be too complicated (too many parameters), or it may not quite do the required thing, which means you have to write a wrapper (or overload) that does the extra work the original method doesn't. Usually when writing a wrapper you will still leverage the original function for whatever it does, and fill in the gaps with your wrapper.

Gayden answered 15/11, 2009 at 1:19 Comment(0)
T
3

It wraps another method :) Probably it adds some additional arguments, like initial values of lo and hi in your case and acts as an entry point to your sorting.

Tolman answered 15/11, 2009 at 1:16 Comment(0)
L
3

In that case shouldn't this be all I need to put in the "sort by z" method if its going through an array called coords that has all its slots filled?

recursiveSort(coords, 0, coords.length-1);
Laureate answered 15/11, 2009 at 5:54 Comment(1)
Yes, that's precisely what you should do.How
K
2

Wrapper Methods can be used for Abstraction, Standardization, and Refactoring

For example if you import a library of special helper methods and use them through out your application, then later decide to switch to a different library that will give you more functionality you would then have to rewrite every line of code that calls the previous libraries methods to call the new libraries methods. With application reaching tens and hundreds of thousands of lines this is a huge task. This happens more frequently when software is licensed and then when it expires a new option is picked.

To solve this problem you can wrap all of the Helper methods in your own methods. These methods simply be 1 line that calls the Methods in your imported library. You would call all of the wrapper methods through out your application instead of directly calling the imported library.

Now the benefit of all the abstraction comes in when you want to import the new library. Instead of rewriting every call through out the application you can simply just rewrite the wrapper methods which should be its own class. Now changing 1 method in the wrapper class updates the entire application where that method is used. (See MVC4 .NET HTML Helper methods for an example)

On the subject of standardization the wrappers can be used to set default values for many of the things that are set manually when the object is created. But now if they are set in the wrapper they no longer must be set every time the original method is called. For example you could set the dimensions of a Grid View in the Gridview wrapper and now when you call the wrapper method in your application all grid views have the same dimensions and fewer lines of code need to be maintained throughout the application; also accomplishing refactoring.

Kluge answered 21/12, 2013 at 22:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.