YAMLException: bad indentation of a sequence entry at line 9, column 17:
- [Introduction](#Introduction)
^
title: Introduction of Interface in Spring Boot and Java8
description:
Introduction of Interface in Spring Boot and Java8
Author: TYT
Introduction
What is interface?
- Interface is a reference type.
- Contain method signature, default method, and static method.
- Cannot be instantiated, only be implemented by class and extended by interface.
Why use interface?
- Achieve
- Loose coupling
Tight coupling vs. Loose coupling
We have to commit and uncommit for switching payment instrument to pay.
Otherwise, creat more shopping class, Shopping1 and Shopping2, etc. to cater to payment instrument instead of commit and uncommit.
- Loose coupling
Person can controll the payment instrument he/she want, kind of IoC I think.
Creat variable to switch payment instrument easier.
Recommended vedio
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
- Multiple implement and interitance
- Switch implement easily
When use interface
- Specify the "behavior" of a particular data type
- Expect the unrelated class whould implenment the interface
- Storage device (interface) - Optical disc (1965), Floppy disk drive (1971), and USB flash drive (2000), etc.(class)
- Multiple implement and interface
- Class USB flash drive implenments interface storage device and charging equipment.
How to use interface?
- Interface
Use "extends" to extend the interface.
Use "," when mutiple interface is extended.
- Class
Use "implenments" to implenments the interface
Use "," when mutiple interface is implenmented.
Should create all method body which method signature is declared in interface.
Usage
One implement
Instantiate implement in native Java, likes List<int> list = new ArrayList<>()
, Map<String, Object> map = new HasMap<>()
, etc.
Use @Service/ @Component in Spring Boot
Multiple implement
- Inter-class
Use default name (lowercase in the first letter) as variable name.
Name implenment and use @Qualifier to specify default implenment
Use @Primary to specify default implenment
- Intra-class
Instantiate implenment to switch
- Will Run build failed if don't use @Primary, @Qualifer, and default name, e.g. stackoverflow.
- With unit test
-
App code
- Use @Primary/ name implenment, and use @Qualifier/ default name to specify implenment.
-
Unit test code
use default name/ @Qualifier to specify implenment.
Will get return from app code if do not use default name/ @Qualifier to specify implenment.
-
App code still get return from app code's implenment, even unit test use @Primary.
-
Will Run build failed if don't use @Primary, @Qualifer, and default name.
-
If app code and unit test code both use @primary
- Run build will be succussed.
- Run test will be failed.
Misuse and overuse
- Create interface just in case.
- Create interface is only for a class.
- Create all method body which declare in interface.
- Name implenment after interface.
Summary
- Interface characteristics
- Loose coupling.
- Multiple implenment and ingeritance.
- Switch implenment easily.
- Usage
- Create a interface to extend interface.
- Create a class to implenment interface.
- Use @Primary or name implenment, e.g. @Service("serviceName") and @Component("componentName")
- Use default name or @Qualifier to specify implenment when declare interface.
- Misuse and overuse
- Avoid using without planing.
- Create interface is only for a class.
Quiz