Clarify Java (1) ---- Interface and Inheritance

zhaozj2021-02-16  58

Bromon original, please respect the copyright

The brothers of the computer college student and I discussed Java. When I met, a few questions were all about the interface. What is the use? Why use the interface? When should I use? Fortunately, they are not asking me how Java connects SQL Server, or how to develop J2EE applications, such issues have killing, avoiding Ji. This year, the undergraduate undergraduate, a graduation design project is J2ME. The student who selection this topic is still painting in the late May, this package, this ... 唉.

Most people believe that the meaning of the interface is that the replacement multiple inheritance. It is well known that Java does not have a mechanism such as C , but it is capable of playing multiple interfaces. This statement is very strong, interface and inheritance is completely different, the interface has no ability to replace multiple inheritance, and there is no obligation. The role of the interface is, which is the class of the flag class (Type of class). Attributing different types of classes to different interfaces, can manage them better. OO's essence, I thought it was an abstraction of the object, and the most embodied is the interface. Why do we discuss design patterns only for languages ​​with abstract abilities (such as C , Java, C #, etc.), because of the design mode, it is actually reasonable to abstract. (Cowboy's famous is "Abstract is part of the like", seemingly ridiculed, actually even).

The most basic in design model is factory mode. In my recent simple app, I want to make my program to transplant in multiple databases. Of course, this involves many problems. How is the order? Compatible with SQL of DBMS is a headache. We may wish to simplify the problem first, just consider how to connect different databases.

Suppose I have a lot of classes, named Mysql.java, SQLServer.java, Oracle.java, DB2.java, which connects different databases, and returns a Connection object, and there is a Close method for closing the connection. Just need for your dbms, choose different classes, you can use it, but what database he will use? I don't know, I hope that I can meet the needs of the code as little as possible. I can abstract the following interface:

Package org.bromon.test;

Public Interface DB

{

Java.sql.connection OpenDB (String Url, String User, String Password);

Void close ();

}

This interface only defines two ways, without any actual code, the specific code is given by the class of the actual interface, such as mysql.java:

Package org.bromon.test;

Import java.sql. *;

Public Class MySQL IMPLEMENTS DB

{

Private string URL = "JDBC: mysql: localhost: 3306 / test";

Private string user = "root";

Private string password = ""

PRIVATE CONNECTION CONN;

Public Connection OpenDB (URL, User, Password)

{

/ / Connect the code of the database

}

Public void close ()

{

// Close the database

}

}

There are of course Oracle.java, etc., interface DBs give these classes, and we define objects in the application:

Org.bromon.test.db mydb;

Use myDB to operate the database, you can actually do what kind of class I am using, this is the so-called "open-closed" principle. But the problem is that the interface cannot be instantiated, mydb = new db (), such code is absolutely wrong, we can only myDB = new mysql () or mydb = new oracle (). Trouble, I still need to specify which class is specified, using the interface and useless. So we need a factory:

Package org.bromon.test;

Public Class DBFactory

{

Public Static DB Connection GetConn ()

{

Return (new mysql ());

}

}

Therefore, instantiated code becomes: mydb = dbfactory.getConn ();

This is the most basic common factory (Factory) in the 23 models. The factory class is responsible for which class is specifically instantiated, while other program logic is operated for the DB interface, which is "Programming" for Interface. Responsibility is shocked to the factory class, of course, you can continue to define the factory interface and continue to throw your responsibility, which evolves into an abstract factory (Abstract Factory).

The interface is not responsible for any specific operation during the entire process. If other programs are connected to the database, just construct a DB object is OK, regardless of how the factory category changes. This is the meaning of the interface - abstract.

The concept of inheritance does not have to say more, it is very understandable. Why inherit? Because you want to reuse code? This is definitely not a reason, the meaning of inheritance is also abstract, not code reuse. If the object A has a run () method, the object B also wants this method, so someone will Class B Extends A. This is the practice of the brain. If an A, a Run () method that is called A, is it possible to achieve the same purpose? as follows:

Class B

{

A a = new a ();

A.Run ();

}

This is the use of class aggregation to reuse the code, which is the prototype of delegated models, which is the practice of GOF.

So what is the meaning of inheritance? In fact, this is caused by historical reasons. The most beginning OO language is only inherited, there is no interface, so it can only be inherited, please pay attention to the intention of the inheritance is abstract, not code reuse (although inheritance also has this role), This is one of the most serious mistakes of Java's bad books. The shadows they have caused, I have not completely get rid of it, bad alone, especially entry, and too large. When should I use inheritance? Only in abstract classes, try not to use in other cases. The abstraction class is not instantiated. It only provides a template, which is very capable of explaining problems.

Software developed, one is repeating code rather than reuse code, the second is to inherit, especially C programmers. Java banned multiple inheritance, the purpose is to stop rotten inheritance, and it is very sensible, but many people don't understand. Java can make better design, which is one of the reasons for me.

转载请注明原文地址:https://www.9cbs.com/read-21722.html

New Post(0)