What is bind? [closed]

5

I can not find the meaning of Bind, let me explain what this is. I would like an example of Bind with the same java language.

    
asked by anonymous 13.03.2016 / 19:41

2 answers

8
Bind or binding are terms used in different applications, but usually refer to how Java or any language links certain things.

Data binding

Defines how data in different formats are mapped to and from objects. There are numerous possibilities, where the most common are:

Binding HTTP request

Consists of mapping attributes in HTTP requests to objects or maps. The way this is done depends on the framework.

JSF for example uses special tags like <h:inputText/> :

<h:inputText value="#{userController.username}" />

The above example would map the field in HTML with an attribute String username in class UserController .

Frameworks such as Spring MVC or JAX-RS-compatible generally map each request parameter directly to an attribute. For example:

<input type="text" name="nome" />

It would be connected as follows:

class Usuario { 
    String nome;
    public void setNome(String nome) { ... }
}

class UsuarioController {
    @GET
    @Path("/usuario/incluir)
    public String incluir(Usuario usuario) { ... } 
}

Binding of JSON and XML

Data represented in JSON and XML can also be linked with objects using libraries such as Jackson (JSON) and JAXB (XML).

Example with Jackson:

ObjectMapper mapper = new ObjectMapper();
String json = "{'nome' : 'Maria'}";
Pessoa pessoa = mapper.readValue(json, Pessoa.class);

Binding of databases

The best-known examples are JPA frameworks such as Hibernate and Eclipselink, where you can set up a class so that field data in a table is linked to the attributes of a class, so you can send and retrieve data without writing SQL clauses or retrieve field by field.

Mapping example of a JPA entity:

@Entity
public class Pessoa {
    @Id
    long id;
    String nome;
    String endereco;
    ...
}

Method binding

Another example is method binding or method binding , which means how the language does to determine which is the correct method to call according to several factors :

  • Class hierarchy, for example when a subclass overrides a method or when a class implements the method of an interface.
  • Overloading methods, for example when methods of the same name are given parameters of different types, have a different number of parameters

In some cases, you can determine the correct method at compile time, for example if it is static or final . In other vessels, the correct method is determined at runtime, which in theory is slower since it includes more processing during program execution. In fact, the JVM includes a series of optimizations that make the performance impact practically negligible in most cases.

Other

Other examples of binding include:

  • As MVC web frameworks decide which method to call according to the request parameters and settings.
  • Dependency Injection frameworks decide which class to inject based on criteria such as hierarchy of classes, annotations, and settings.
14.03.2016 / 02:58
3

Dynamic Bind and Staind Bind.

In static bind, you have methods inside your main class and call them directly in main (methods have to be static), eg:

public class Main{
    public static void varType(float var){
        System.out.println("Is an float!");
    }

    public static void varType(int var){
        System.out.println("Is an int!");
    }

    public static void varType(String var){
        System.out.println("Is an string!");
    }

    public static void main(String args[]){
        int x = 0;
        float y = 0;
        String z = "0";
        varType(x);
        varType(y);
        varType(z);
    }
}

In short, calls are made in the static method in the parent methods.

Dynamic bind, an instance of the main class is created to use its methods, eg

public class Main{
    public void varType(float var){
        System.out.println("Is an float!");
    }

    public void varType(int var){
        System.out.println("Is an int!");
    }

    public void varType(String var){
        System.out.println("Is an string!");
    }

    public static void main(String args[]){
        int x = 0;
        float y = 0;
        String z = "0";
        Main m = new Main();
        m.varType(x);
        m.varType(y);
        m.varType(z);
    }
}

Reference: link

    
13.03.2016 / 23:11