+5 votes
408 views
in Web development by (242k points)
reopened
What is a wrapper in programming?

1 Answer

+3 votes
by (1.6m points)
edited
 
Best answer

Wrappers as translators of user input
Wrappers to access databases
Wrappers in object-oriented programming
Wrappers to design HTML documents
TCP wrappers on Linux systems

image

What is a wrapper in programming?

The English word wrapper designates a packaging or wrapping. In the context of software , this term refers to programs or code that surrounds other program components. The wrappers can be used for various reasons: often used to improve the compatibility and interoperability between different software structures or to represent something visually, eg, the wrappers HTML or CSS. They can be Wrappers different software components, products software independent, architectures software , classes OOP or frameworks ..

If within a program you have to use functions or blocks of programs in another programming language, these elements can be "wrapped". The main program only communicates with the wrapper and this is responsible for transmitting the commands to the wrapped program and returning the results to the main program. The wrapper is the only component of the process that is in direct contact with both pieces of software.

In the world of programming and software development , wrappers have an infinite number of uses. In the following examples, we show you what a wrapper is , how it works exactly, and what tasks it takes on..

Index
  1. Wrappers as translators of user input
  2. Wrappers to access databases
  3. Wrappers in object-oriented programming
  4. Wrappers to design HTML documents
  5. TCP wrappers on Linux systems

Wrappers as translators of user input

The forms of the programs or web applications wait for inputs that the program is able to process. In US-based programs, for example, numbers with decimals are understood to have periods instead of commas and are measured in feet and inches. When using this type of software component in your own applications, there is not always the possibility of adapting them to the national customs of each country and, consequently, you almost always get wrong results or even program errors.

In these cases, the wrapper is the perfect solution. Thus, the form will not transmit the inputs directly to the external program, but will send them to the wrapper . This will take care of evaluating the inputs and translating them into valid inputs for the external program without the need for modification..

Wrappers to access databases

As a general rule, databases from different manufacturers can hardly be used together due to compatibility issues between different data tables, queries, or query languages. In these cases, wrappers can also be very useful. The wrappers of all kinds try to recognize the existing inconsistencies between different interfaces of software and surround them .

A classic example of a wrapper is Java Database Connectivity (JDBC), a database interface for the Oracle-Java platform. In its role of wrapper , the JDBC allows access to relational databases from different providers. The JDBC establishes the connection with the different databases through special drivers.

The SQL queries of the programs are directed exclusively to the JDBC and not to the own databases. The JDBC converts the queries to the query language used by the database in question and returns the result in a Java-compatible format. In this way, the program that performs the query always obtains the data in a homogeneous format, regardless of the database that it uses.

Wrappers in object-oriented programming

In object-oriented programming, different structural patterns are used that always work in the same way, regardless of the programming language used. Layout, adapter, and decorator patterns belong to the category of structural patterns and are considered wrappers .

An adapter wraps two incompatible interfaces between different classes. By translating one interface to the other, classes can now communicate with each other again. This is very important when we want to continue using classes or entire class libraries in new projects. These libraries use standardized and unique interfaces that should not be modified as they must be compatible with a large number of programs. The wrapper , the adapter in this case, is the determining intermediate link for communication.

A decorator allows you to extend a class with additional functions without modifying it. For the querying program object, the decorator presents the same interface as the original class. Thus, the object consulting it does not require any modification. In its wrapper function , the decorator passes the queries to the class. New functions, not included in the class, are processed directly by the decorator. It then returns the results so that the querying object perceives them as results from the decorated class.

Wrappers to design HTML documents

It is very common to use wrappers when designing (changing the) HTML and CSS web pages. Without wrappers , even the smallest changes, for example, changing the margins spaces of a browser window, would mean having to modify several style sheets and finally check that they are all still compatible.

It is much easier to put all the content of the page in a DIV container , as the following example shows:

 

  <html> <head> ... </head> <body> <div> ? </div> </body> </html>  

In the wrapper container is the actual content of the page.

In the corresponding CSS file, the wrapper is defined as a style sheet:

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

In this example, the width: parameter is used to attribute a width of 500 pixels to the container. The top and bottom margins are set by the margin parameter : to a value of 25 pixels. The left and right margins are automatically derived from the width of the browser window and the width of the container.

It is enough to modify the wrapper to modify the lateral margins of the page without having to intervene more in the HTML or CSS code.

TCP wrappers on Linux systems

The inetd background service on Linux and other UNIX-based operating systems runs as a TCP wrapper . The inetd element monitors network sockets and accepts connection queries. A configuration file determines the ports to consider. Queries are evaluated and the indicated inetd service for each port in the configuration file is started. These programs are usually Daemon instances running in the background.

When the connection ends, inetd stops the service started automatically again. By starting the service only when needed, you can save a lot of system resources compared to automatically starting network services, which are often not even required. The inetd element works as a wrapper to which all programs direct their network queries without communicating directly with the different services.

TCP wrappers can also be used to prevent unwanted access from a network. In these cases, inetd or a special server software query the TCP wrapper . The hosts and computers to be accepted or rejected are recorded in the /etc/hosts.allow and /etc/hosts.deny files .


...