Example explains the role of interfaces in Java

Source: Internet
Author: User

The role of the interface

The function of the interface is simple: the interface is used to mark the class, the different classes belong to different interfaces (through the upward transformation), the management interface is more convenient than the management of various classes, interface embodies the abstract view, what is abstract? Abstraction is the "part that pulls out the image."

Using interfaces to solve problems

Question: Now we are going to write a class to connect to the database for the user, there are two functions: one to return the connection object, the other is to close the database, closed (), the general solution is to write a class for each database, and then depending on the user's use of the database to determine the use

OK, let's see what's wrong with this:

(1). First of all, each class must have duplicate code, resulting in the expansion of the Code;

(2). Secondly, the most important thing is that we do not know what database users use, may be Oracle, may be MySQL, but also may be SQL Server, this problem is difficult to solve.

Solution:

First we define the interface:

public interface DataBase
  {
  java.sql.Connection openDB(String url,String user,String password);
  void close();
  }

We have defined two methods, OPENDB returns the Connection object, close () closes the database;

The implementation is implemented in the class of database interface;

Here's a look at the implementation:

import java.sql.*;
  public class Mysql implements DataBase
  {
  private String url=”jdbc:mysql:localhost:3306/test”;
  private String user=”root”;
  private String password=””;
  private Connection conn;
  public Connection openDB(url,user,password)
  {
    //连接数据库的代码
  }
  public void close()
  {
    //关闭数据库
  }
  }

The class MySQL implements the database interface, below also has realizes the database interface Oraclesql and so on class;

These classes are all attributed to the database interface, how do you use them in your application?

We want to define database object MyDB, through MyDB to manipulate databases, you can not distinguish which class is.

Another problem: Java does not allow us to instantiate the interface, such as database Mydb=new database ();

We can only mydb=new Mysql () or Mydb=new Oracle (). In this way we must also specify which object to instantiate, as if the efforts in the front were in vain!! What do we do, we need a factory:

public class DBFactory
  {
  public static DataBase Connection getConn()
  {
    Return(new Mysql());
  }
  }

The instantiated code becomes: Mydb=dbfactory.getconn ();

The interface is not responsible for any specific operations throughout the process, other programs to connect to the database, only need to construct a DB object OK, regardless of how the factory class changes. This is the meaning----abstraction of the interface.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.