java. Two examples to fully illustrate the scalability of polymorphism

Source: Internet
Author: User

Here I will take two examples to fully illustrate the scalability of Java polymorphism, as a good programmer, it is necessary to understand the scalability of the program, very conducive to the development of the program follow-up.

The first example: the use of computer motherboards as an example, on the computer we are familiar with the motherboard, there is not a lot of modules, network cards, sound cards, want to use these features, only when the motherboard run up, and then to the network card and sound card power supply can, but how to achieve this using software language? is not the first motherboard to run up, motherboard and then let the sound card or network card run up, but to do so, the expansion of the sound card and network card running operation is repeated, and there are many modules also have such a function, simply let the motherboard on the module run up, this is one, in case, that day, technology updates, new modules appear, is not to create the object of the module, and then run up, which is obviously not conducive to development, but for this issue, motherboard manufacturers and sound cards, network card manufacturers to discuss, after the use of a PCI interface to connect the motherboard and modules, completely solve the update can not be used or reused problems, And that exactly corresponds to the polymorphism of our Java, using polymorphism, we can greatly improve the program's scalability! The specific code is as follows!

/*
Demand:
Computer running instance,
The computer is running based on the motherboard.
*/


Interface PCI
{
public void open ();
public void close ();
}

Class Mainboard
{
public void Run ()
{
System.out.println ("mainboard run");
}
public void Usepci (PCI p)//pci p = new netcard ()//interface reference points to its own subclass object.
{
if (p!=null)
{
P.open ();
P.close ();

}
}
}


Class Netcard implements PCI
{
public void Open ()
{
System.out.println ("netcard open");
}
public void Close ()
{
System.out.println ("netcard close");
Method ();
}

}
Class Soundcard implements PCI
{
public void Open ()
{
System.out.println ("Soundcard open");
}
public void Close ()
{
System.out.println ("soundcard close");
}
}
/*
Class Mainboard
{
public void Run ()
{
System.out.println ("mainboard run");
}
public void Usenetcard (netcard c)
{
C.open ();
C.close ();
}
}

Class Netcard
{
public void Open ()
{
System.out.println ("netcard open");
}
public void Close ()
{
System.out.println ("netcard close");
}
}
*/

Class DuoTaiDemo5
{
public static void Main (string[] args)
{
mainboard MB = new mainboard ();
Mb.run ();
MB.USEPCI (NULL);
Mb.usepci (New netcard ());
Mb.usepci (New Soundcard ());

}
}
--------------------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------

Second example: Using a common database to explain polymorphism, for our well-known database, there are two types of database connection mode: JDBC and Hibernatelian connection, using the database, there are two essential operations, connection and shutdown, this time, using the interface, the two operations encapsulated together, You need to use that connection mode to change the class name directly! The specific code is as follows!

/*
Requirements: operation of the database.
Data is: User information.
1. Connect to the database. JDBC Hibernate
2. Operate the database.
C Create R read u update d delete
3. Close the database connection.
*/

Interface Userinfodao
{
public void Add (user user);

public void Delete (user user);
}

Class Userinfobyjdbc implements Userinofdao
{

public void Add (user user)
{
1,JDBC connect to the database.;
2, add data using SQL Add statement.;
3. Close the connection.
}
public void Delete (user user)
{
1,JDBC connect to the database.;
2, delete data using SQL Add statement.;
3. Close the connection.
}
}

Class Userinfobyhibernate implements Userinfodao
{
public void Add (user user)
{
1,hibernate connect to the database.;
2, add data using SQL Add statement.;
3. Close the connection.
}
public void Delete (user user)
{
1,hibernate connect to the database.;
2, delete data using SQL Add statement.;
3. Close the connection.
}
}

Class Dboperate
{
public static void Main (string[] args)
{
Userinfobyjdbc UI = new Userinfobyjdbc ();
Userinfobyhibernate UI = new Userinfobyhibernate ();
Userinfodao UI = new Userinfobyhibernate ();
Ui.add (user);
Ui.delete (user);
}
}
--------------------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------



Summarize:

1. These two examples all use the interface to abstract some repetitive operations, in order to allow the different modules to use these operations directly and quickly, directly using polymorphic upward transformation (see my previous blog), can be used to invoke the parent class reference, after all, whether it is a technical update with a new module or replace the existing module, We can all use the reference of the parent class to invoke their common operation directly!



2. Not necessarily with the interface, can also use abstract classes, but, using the interface, can be more extensible, later there are updates, you can directly change the interface, not to change the abstract class, and secondly, the use of interfaces, can be more than inherit! This is also a convenient place.

java. Two examples to fully illustrate the scalability of polymorphism

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.