[JAVA] Interface usage example

Introduction

This is the interface version of Example of using abstract class. I know the meaning and definition of the interface, but when to use it and what good things are there? Here is an example.

Sudden conclusion. It will be strong against remodeling.

Same as abstract class / generalization / inheritance, it is resistant to modification. Here is an example of a further modification request for the program introduced in Abstract class usage example.

Program to be modified

First, let's review the program introduced in Example of using abstract class.

A program that manages printers. At first, it managed inkjet printers and laser printers and displayed a list of printer information. There was a remodeling requirement to add a dot impact printer as a manageable printer, so I made a program that is strong against remodeling by abstraction (generalization / inheritance).

Class structure: image.png

The member variables mModelName (model name) and mPaper (remaining amount of paper) are generalized, and the getPrinterInfo () function that returns printer information is abstracted.

New remodeling requirements

This time, let's assume that there is a further requirement that "add a 3D printer to the type of printer to be managed and display the printer information as well".

There is nothing you can't do by inheriting AbstractPrinter like when you added a dot impact printer, but since 3D printers do not print on paper, mPaper (remaining amount of paper) is not required. Unused member variables should not interfere with the maintenance of the program.

So why not create an independent 3D printer class? Then, it becomes redundant like a program that does not use abstract classes in Example of using abstract classes.

Then what should I do? Let's solve it with the interface.

Try to modify using the interface

First, create the interface.


//
//Printer interface
//
interface IPrinter{
    
    //Returns printer information for list display
    String getPrinterInfo();
}

Printer Interface Create a 3D printer class by implementing IPrinter.

//
//3D printer
//
class ThreeDPrinter implements IPrinter{

    //Material capacity
    private int mMaterial;
    
    //constructor
    public ThreeDPrinter(int material){
        mMaterial = material;
    }
    
    //Method that returns printer information for list display
    public String getPrinterInfo(){
        
        return "[3D]3D printer under development" + "Printable capacity:" + mMaterial + "liter";
    }
}

Implement the printer interface IPrinter in the AbstractPrinter class as well.

//
//Printer abstract class
//
abstract class AbstractPrinter implements IPrinter{
    //Abbreviation
}

Change the type of the array that stores the printers to be managed from Abstract Printer to I Printer.

    public static void main(String[] args) throws Exception {

        //Register the printer to be managed.
        IPrinter[] printers = {
            new InkjetPrinter("Epson P1", 10),
            new InkjetPrinter("Canon C1", 20),
            new LaserPrinter("Xerox X1", 100),
            new LaserPrinter("Richo R1", 50),
            new LaserPrinter("Richo R2", 200),
            new DotimpactPrinter("NEC N1", 100),
            new DotimpactPrinter("Oki O1", 50),
            new ThreeDPrinter(5),
        };
        
        //List the managed printers.
        for(int i = 0; i < printers.length; i++ ){
            String printerInfo = printers[i].getPrinterInfo();
            System.out.println(printerInfo);
        }
    }

only this. Let's run it.

Execution result


[Inkjet]Epson P1 Number of printable sheets: 10
[Inkjet]Canon C1 Number of printable sheets: 20
[laser]Xerox X1 Number of printable sheets: 100
[laser]Ricoh R1 Number of printable sheets: 50
[laser]Richo R2 Number of printable sheets: 200
[Dot impact]NEC N1 Number of printable sheets: 100
[Dot impact]Oki O1 Number of printable sheets: 50
[3D]3D printer under development Printable capacity: 5 liters

It's done! As in the case of Abstract class usage example, the number of variables to be managed did not increase, and the display process was not changed. https://paiza.io/projects/wSSYJVnwbZfsI0vXHrsGjg

Class structure: image.png

Summary

The interface is also a type. So it can be used as a variable type. ** An interface type variable can contain an object of the class that implements that interface. ** **

//Register the printer to be managed.
IPrinter[] printers = {
    new InkjetPrinter("Epson P1", 10),
    //Abbreviation
    new ThreeDPrinter(5),
};

Both the ThreeDPrinter class and each class derived from AbstractPrinter implements the IPrinter interface, so you can store that object in a variable of type IPrinter. And the IPrinter type variable that defines the getPrinterInfo () function can be called because I don't know the details of the objects contained in it, but I'm sure it implements the getPrinterInfo () function. I will.

//List the managed printers.
for(int i = 0; i < printers.length; i++ ){
    // printers[i]I don't know what's inside,
    // printers[i]Is an IPrinter type
    // getPrinterInfo()Is implemented so you can call it.
    String printerInfo = printers[i].getPrinterInfo();
    System.out.println(printerInfo);
}

Recommended Posts

Interface usage example
[Android] Interface example
Inner class usage example
java (interface)
new interface
[java] interface
Example title
irb usage