interface-oriented programming essay

Source: Internet
Author: User

Today on the Internet to see an interface-oriented programming of the blog, at first, not quite understand, so the side of the test to understand. Summarized as follows:

Test case: Develop an application that simulates the reading and writing of a mobile storage device, i.e., the data exchange between a computer and a USB stick, MP3, and a mobile hard disk. The computer is required to exchange data with these three devices, and there may be new

Three-party mobile storage device, so the computer must be extensible to be able to exchange data with the storage devices that are currently unknown and may occur later.

The first kind: class-oriented programming

1, define a U disk, Flashdisk.java class, realize read and write two methods.

 Package com;  Public class FlashDisk {    publicvoid  read () {        System.out.println ("FlashDisk read" );    }      Public void Write () {        System.out.println ("FlashDisk Write");}    }

2, define a computer , Computer.java class, define a Rwdata method, tune the memory stick read and write

 Package com;  Public class Computer {       publicvoid  rwdata () {        new// start USB drive          flashdisk.read ();        Flashdisk.write ();    }}

3, test, Test.java class

 Package com;  Public class Test {    publicstaticvoid  main (string[] args) {        new // Start your computer        // calling a method to process a USB flash drive     }}

Summary: Through this most basic method of implementation, when adding a MP3 to read and write, need to add to the computer class to MP3 read and write, used in real life, you have to disassemble the computer, the lack of scalability.

The second type: interface-oriented programming

1, define an interface, Imobile.java class, define the method of reading and writing. (The purpose of the interface is to the same standard, so that all appliances are created according to this standard, to achieve unified management, can refer to the reality of the implementation of USB interface principle)

 Package com;  Public Interface imobile {    publicvoid  Read ();      Public void Write ();}

2, Transform U disk, Flashdisk.java class, make it realize Imobile interface

 Package com;  Public class Implements imobile{    publicvoid  read () {        System.out.println ("FlashDisk Read" );    }      Public void Write () {        System.out.println ("FlashDisk Write");}    }

3, the transformation of computer, Computer.java class, through its operation on the interface, operation of all based on this interface development of electrical appliances

 Packagecom; Public classComputer {PrivateImobile usbdrive;//installing a USB slot in your computer     Publicimobile getusbdrive () {returnusbdrive; }     Public voidsetusbdrive (imobile usbdrive) { This. usbdrive = usbdrive;//start the slot and initialize the device plugged into the USB    }    //the computer provides the user with the unified reading and writing operation     Public voidRwdata () {usbdrive.read ();//call the Read-write method of the device that inserted the USB,Usbdrive.write (); }}

4, modify the test, Test.java class

 package   com;  public  class   Test { public  static  void   main (string[] args) {Comput ER computer  = new  computer (); //         start the computer  Imobile FlashDisk = new  flashdisk (); //  take out the USB stick  computer.setusbdrive (flashdisk); //   computer.    Rwdata (); }}

interface-oriented programming structure has been basically formed, the computer has been able to read and write to the U disk, at this time, if you want to add to the MP3 read and write, only need to define a MP3 class, so that it implements the public interface.

1. Define a Mp3,mp3player class so that it implements the Imobile interface

 Package com;  Public class Implements imobile {    @Override    publicvoid  Read () {        System.out.println (" Mp3player Read ");    }    @Override    publicvoid  Write () {        System.out.println ("Mp3player Write ");}    }

2, for the computer to add to the MP3 operation has been completed, modify the test, Test.java class

 Packagecom; Public classTest { Public Static voidMain (string[] args) {computer computer=NewComputer ();//Start your computerimobile FlashDisk=NewFlashDisk ();//take out the USB stickComputer.setusbdrive (FlashDisk);//plug the USB stick into the socket and the computer starts to initialize the USB drivecomputer.                Rwdata (); Imobile Mp3player=NewMp3player ();//take out the MP3 .Computer.setusbdrive (Mp3player);//plug the MP3 into the USB slot and the computer starts to initialize the USB drivecomputer.    Rwdata (); }}

interface-oriented programming essay

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.