In a software context, the term “wrapper” refers to programs or codes that literally wrap around other program com­po­nents. Several different wrapper functions can be dis­tin­guished. They are often used for ensuring com­pat­i­bil­i­ty or in­ter­op­er­abil­i­ty between different software struc­tures. Al­ter­na­tive­ly, they can be used for visual reasons, as is the case with HTML or CSS wrappers. Wrappers can be in­di­vid­ual software com­po­nents, in­de­pen­dent software products, software ar­chi­tec­tures, classes in object-oriented pro­gram­ming, or frame­works.

If you want to use functions or code blocks of another pro­gram­ming language within a program, you can en­cap­su­late them using a wrapper. The main program com­mu­ni­cates ex­clu­sive­ly with the wrapper, which forwards the commands to the wrapped program and returns the results. The wrapper itself is the only component that com­mu­ni­cates directly with both parts of the program.

Wrappers can be used in a variety of ways in pro­gram­ming and software de­vel­op­ment. The following examples show how wrappers work and the different tasks they perform.

Wrappers as trans­la­tors of user input

Forms in programs or web ap­pli­ca­tions expect input that the program can process. Programs developed in many European countries expect numbers to be entered with a comma instead of a decimal point and di­men­sions to be entered in meters and cen­time­ters. When you use these program com­po­nents in your own ap­pli­ca­tions, it’s not always possible to adapt them to the default user inputs in the US. This in­vari­ably leads to incorrect results or even program errors.

A wrapper can solve this problem. The input form forwards the input directly to the wrapper instead of directly to the external program. The wrapper analyzes the inputs and trans­lates them into valid inputs for the external program without having to modify the program.

Wrappers for database access

Databases from different vendors usually cannot be used together because their data tables, queries, or query languages are not com­pat­i­ble with each other. Here too, a wrapper can be the solution. As with any type of wrapper, the idea is to detect in­con­sis­ten­cies between different software in­ter­faces and use the wrapper to bridge the gap.

Java Database Con­nec­tiv­i­ty (JDBC), a database interface by Oracle, is a typical example of a wrapper. In its wrapper function, JDBC gives access to different re­la­tion­al databases. JDBC connects in­di­vid­ual databases using special drivers. SQL queries are directed ex­clu­sive­ly to JDBC, not the databases. JDBC converts queries into the database query language, returning Java-com­pat­i­ble results. Ac­cord­ing­ly, the re­quest­ing program always receives data in a uniform format, re­gard­less of the database used.

Wrappers in object-oriented pro­gram­ming

Object-oriented pro­gram­ming uses different struc­tur­al patterns that basically always work in the same way re­gard­less of the pro­gram­ming language used. The Adapter and Decorator design patterns are struc­tur­al patterns and are also called wrappers.

An adapter conceals in­com­pat­i­ble in­ter­faces between in­di­vid­ual classes. By trans­lat­ing one interface into another, an adapter allows the classes to com­mu­ni­cate with each other. This is es­pe­cial­ly important if you want to use classes or entire class libraries in new projects. These libraries use unique, stan­dard­ized in­ter­faces that may not be changed because they have to be valid for a large number of programs. The wrapper – in this case the adapter – is the crucial link in the com­mu­ni­ca­tion.

A decorator enables functions to be added to a class without changing the class itself. To the calling program object, the decorator has the same interface as the original class. In this way, nothing needs to be changed in the calling object. As the wrapper, the decorator passes the calls on to the class. The decorator directly handles new functions that are not included in the class. It returns the results in such a way that they appear to the calling object like results of the decorated class.

Wrappers for designing HTML documents

Wrappers are often used for (re)designing websites in HTML and CSS. Without wrappers, you would have to change multiple stylesheets and make sure they fit together again after making in­di­vid­ual ad­just­ments such as changes to the page margins of a browser window.

An easier approach is to use a DIV container as a wrapper to hold the entire content of the page, as in the following wrapper example:

<html>
    <head>
    ...
    </head>
    <body>
        <div class="wrapper">
        …
        </div>
        </body>
</html>

The actual content of the page is then enclosed inside this wrapper container.

The wrapper is defined as a stylesheet in the as­so­ci­at­ed CSS file:

body {
	margin: 0;
	padding: 0
}
.wrapper {
	width: 500px;
	margin: 25px auto;
}

In this example of a wrapper, the container is assigned a width of 500 pixels via the width parameter. The margins at the top and bottom are defined using the margin parameter, which is set to 25 pixels. The left and right margins are au­to­mat­i­cal­ly derived from the width of the browser window and the width of the container.

By simply changing the wrapper, you can easily adjust the page margins without making further changes to the HTML or CSS code.

TCP wrappers in Linux systems

The inetd back­ground service in Linux and other operating systems based on UNIX run as TCP wrapper. inetd listens to network sockets and accepts con­nec­tion requests. A con­fig­u­ra­tion file specifies which ports to listen to. The requests are evaluated and the inetd service specified in the con­fig­u­ra­tion file is started for the cor­re­spond­ing port. In most cases, these programs are daemons that run in the back­ground.

If the con­nec­tion is ended, inetd au­to­mat­i­cal­ly stops the service. Starting services on demand con­sid­er­ably reduces the load on system resources compared to au­to­mat­i­cal­ly starting network services that may not be required at all. inetd functions as a wrapper that receives network requests from all programs without com­mu­ni­cat­ing directly with the in­di­vid­ual services.

TCP wrappers can also be used to prevent unwanted access from a network. The TCP wrapper is queried by inetd or special server software. Allowed and denied hosts and computers are entered in the files /etc/hosts.allow and /etc/hosts.deny.

Go to Main Menu