Most people think that the interface is about the replacement of multiple inheritance. Known as Java does not c + + multiple inheritance mechanism, but can implement multiple interfaces. Actually, this is very far-fetched, interfaces and inheritance are completely different things, the interface can not afford to replace the multiple inheritance, there is no such obligation. The role of interfaces, in a word, is a sign class category (type of class).
Attributed to different types of classes of different …
Most people think that the interface is about the replacement of multiple inheritance. Known as Java does not c + + multiple inheritance mechanism, but can implement multiple interfaces. Actually, this is very far-fetched, interfaces and inheritance are completely different things, the interface can not afford to replace the multiple inheritance, there is no such obligation.
The role of interfaces, in a word, is a sign class category (type of class). Attributed to different classes of different types of interfaces, you can better manage them. The essence of Java, I think, is an abstract object, best exemplifies this point is the interfaces. Why do we discuss the design mode only has the abstract ability for the language (such as c + +, java, c #, etc.), it is because the design study model, in fact, is how reasonable to abstract.
Design mode is the most basic factory pattern (Factory), in my most recent application of a very simple, I would like to try to make my program can be transplanted between multiple databases, of course, it involves a lot of problems, one is how to compatible with different DBMS’s SQL on a headache. Let us first simplify the problem, only consider how to connect different databases.
Suppose I have a lot of classes, namely Mysql.java, SQLServer.java, Oracle.java, DB2.java, they are connected to different databases, unification returns a Connection object, and has a close method for closing the connection. Just for your DBMS, select the type of different, you can use up, but my users to use what he Hui database? I do not know I hope is that as Shao’s Xiugaidaima, Jiuneng meet his needs. I can abstract the following interface:
public interface DB
java.sql.Connection openDB (String url, String user, String password);
void close ();
This interface defines only two methods, there is no meaningful code, the specific code for this interface from the implementation class to give, for example Mysql.java:
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 to the database code
public void close ()
/ / Close the database
A similar course Oracle.java so, the interface DB belongs to these classes of classes, in the application we define the object:
Use myDB to operate the database, I actually can not control which type is used, this is the so-called “open – close” principle. But the problem is that interfaces can not be instantiated, myDB = new DB (), this code is absolutely wrong, we can only myDB = new Mysql () or myDB = new Oracle (). Trouble, I still need to specify the specific instance of that class which, as with the interface with the useless. So we need a factory:
public class DBFactory
public static DB Connection getConn ()
Return (new Mysql ());
So the code becomes instantiated: myDB = DBFactory.getConn (); this is the most basic mode of 23 species of common plants (Factory), factory class is responsible for specific examples of what type, while others are aimed at DB this program logic interface operation, which is “for interface programming.” Responsibilities have been shifted to the factory class, and of course you can continue to define the factory interface, continue to throw responsibility, which evolved into the abstract factory (Abstract Factory).
The whole process of the interface responsible for any specific action, other applications to connect to the database, then only need to construct a DB object, regardless of the changes in the factory class. This is the meaning of interface —- abstraction. Needless to say, the concept of inheritance, well understood. Why inheritance? Because you want to reuse code? This is not grounds for the meaning of inheritance is abstract, not code reuse. If the object A has a run () method, object B would also like to have this method, so it was on the Class B extends A. This is not the practice by the brain. If the B in the instance of an A, called A, Run () method, is not to achieve the same purpose? As follows:
A a = new A ();
This is the type of polymer used to reuse code, is assigned mode shape. So what is the meaning of succession? Fact, this is caused by historical factors, Qing Yidingzhuyi, inherited intention is to abstract, not Daima reuse (Suiranjicheng Ye have this), which is kicking the tires when the Zuirong Yi Java and worst error committed one, in the process of programming errors caused by the shadow, may be useful for your programming career severely affected. When should I use inheritance? Used only in an abstract class, try not to use other cases. Abstract class is not instantiated, it is only just a template, which is illustrative.