Clarify Java (1) - Interface and inheritance

  Please respect the original copyright Bromon 

  Academy of Computer Science Institute two brothers and I discussed Java, a meeting, several issues are all on the interface, Interface what is the use?    Why use interface?    When the use of interface?    Am glad that they are not asked me how to connect Java SQL Server, or how to develop J2EE applications, such problems are lethal, and the avoidance of Kyrgyzstan.    This year we have a computer college graduate design issue is done J2ME, the subject of elections in five students are still bitter end Zhaolian study java.util .* this package, this, this…… Alas. 

  Most people think that the significance of the interface replace multiple inheritance.    Java not known as c + + multiple inheritance mechanism, but it can make multiple interfaces.    This is a very far-fetched, and the interface is completely different from the succession of things, the interface can not afford to replace multiple inheritance, and there is no such obligation.    Interface role, in a word, is the symbol of the type (type of class).    The type attributed to the different types of different interfaces, they can be better managed.    OO the essence, I think, is an abstract object, best embodies this point is the interface.    Why do we only discuss design patterns with the abstract ability for the language (for example, c + +, java, c #, etc.), because the study design is, in fact, how reasonable to abstract.    (The famous cowboy is "abstract is like removing part of the" seemingly fun, and it is justified). 

  The most basic model in the design of the factory model (Factory), I have a very simple application, I would like to as much as possible so that I can process in multiple databases transplant, of course, this involves a lot of problems, just how compatible with the different SQL DBMS on a headache.    We might as well put the issue simplistic to consider only how to connect different databases. 

  I have a lot of assumptions categories, namely Mysql.java, SQLServer.java, Oracle.java, DB2.java, they connected to different databases, unified return a Connection object, and they have a close approach to close the connection.    You only need to target the DBMS, choose a different category, can be used, but my user what he would use the database?    I do not know, I hope that the amendments to the code as far as possible, we will be able to meet his needs.    I can abstract the following interfaces: 

  1.   Package org.bromon.test; 
  2.   Public interface DB 
  3.   ( 
  4.   Java.sql. Connection openDB (String url, String user, String password); 
  5.   Void close (); 
  6.   ) 



  The interface definition only two ways, there is no practical significance of the code, the code is specific to this type of interface to give, for example, Mysql.java: 

  1.   Package org.bromon.test; 
  2.   Import java.sql .*; 
  3.   Public class Mysql implements DB 
  4.   ( 
  5.   Private String url = "jdbc: mysql: localhost: 3306/test"; 
  6.   Private String user = "root"; 
  7.   Private String password = ""; 
  8.   Private Connection conn; 
  9.   Public Connection openDB (url, user, password) 
  10.   ( 
  11.   / / Connecting to the database code 
  12.   ) 
  13.   Public void close () 
  14.   ( 
  15.   / / Database closed 
  16.   ) 
  17.   ) 


  Oracle.java similar course, and so on, DB interface to these categories under the category of the application process, we targeted this definition: 

  1.   Org.bromon.test.DB myDB; 



  MyDB used to operate the database, we can leave in fact I which is used by category, this is the so-called "open - closed" principle.    However, the problem lies interface is not the case, myDB = new DB (), so the code is absolutely wrong, we can only myDB = new Mysql () or myDB = new Oracle ().    In trouble, I still need to designate specific examples of the type which is used as the interface with useless.    Therefore, we need a factory: 

  1.   Package org.bromon.test; 
  2.   Public class DBFactory 
  3.   ( 
  4.   Public static DB Connection getConn () 
  5.   ( 
  6.   Return (new Mysql ()); 
  7.   ) 
  8.   ) 


  So examples of the code becomes: myDB = DBFactory.getConn (); 
  This is the pattern of 23 species most basic ordinary factory (Factory), the factory class which is responsible for specific examples of the type, and other procedures are directed at DB this logic interface operation, and that is "against the programming interface."    Responsibility has been shifted to a factory class, of course, you can continue to plant interface definition to continue to throw responsibility, which evolved into abstract factory (Abstract Factory). 

  Interface irresponsible in the whole process of the specific operation, the procedure to other connecting to the database, only need to construct a DB object on OK, regardless of how changes in the factory.    This is the interface - the significance of the abstract. 

  The concept of inheritance Needless to say, well understood.    Why should inherit?    Because you want to reuse code?    This is absolutely not the reason, the succession of significance also lies in the abstract, rather than code reuse.    A target if there is a run () method, target B would like to have this method, it was on Class B extends A.    This is not the practice of the brain.    If an instance of B in A, called the A Run () method, is not to achieve the same purpose?    As follows: 

  1.   Class
  2.   ( 
  3.   A a new A = (); 
  4.   A.run (); 
  5.   ) 



  This is the use of the polymerization to reuse code, assigned to the prototype model is GoF has consistently advocated the practice. 

  What is the significance of it inherited?    In fact, this is caused by historical factors, the most OO languages only the beginning of the succession, no interface, it can only to the succession to achieve abstract, Please note that the original intent of the succession is abstract, rather than code reuse (although successor also has this role), This is a lot of Java Lanshu one of the most serious mistakes, caused by the shadow of them, I have not yet completely free from, ah bad book harmful, especially entry category, pernicious influence of too much.    What time should use inheritance?    Only in the use of abstract classes, other circumstances as far as possible not to use.    An abstract class is not the case, it is merely to provide a template only, which is very illustrative of the problem. 

  The development of software and the source of all evil, the First, rather than duplicate code reuse code, and the second is rotten to inherit, especially since the c + + programmers.    Banning multiple inheritance in Java, the purpose is to stop bad inheritance, it is a very sensible approach, but many people do not understand.    Java to better reflect design, This is a one of the reasons why I Rumi. 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Tags: ,

Releated Java Articles

Comments

Leave a Reply