[ZT] clarify the essence of Java language interface and inheritance

xiaoxiao2021-03-06  24

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. In fact, this is a very strong, interface and inheritance is completely different, and 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 DB gives these classes, in the application, we define the object: org.bromon.test.db mydb; use myDB to operate the database, you can actually Which class I am using 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 in 23 modes, factory class is responsible for which class, while other program logic is for DB The interface is operated, 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 code, which is the prototype of delegated models. It is the practice of GOF's consistently advocacy. 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-39540.html

New Post(0)