Java 17 Recipes


-7. Modifying Interfaces Without Breaking


Download 3.2 Mb.
Pdf ko'rish
bet100/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   96   97   98   99   100   101   102   103   ...   245
Bog'liq
Java 17 Recipes

5-7. Modifying Interfaces Without Breaking 
Existing Code
 Problem
You’ve got a utility class that implements an interface, and many different classes within 
the utility library implement that interface. Suppose that you want to add a new method 
to the utility class and make it available for other classes via its interface. However, 
changing the interface likely breaks some existing classes that already implement that 
interface.
 Solution
Add the new method and its implementation to the utility class interface as a default 
method. By doing so, each class that implements the interface automatically uses the 
new method and is not forced to implement it since a default implementation exists. The 
following class interface contains a default method, which can be used by any class that 
implements the interface.
public interface TeamType {
List
getPlayers();
void setPlayers(List
players);
void setName(String name);
void setCity(String city);
String getFullName();
default void listPlayers() {
getPlayers().stream().forEach((player) -> {
Chapter 5 ObjeCt-Oriented java


172
System.out.println(player.getFirstName() + " " + player 
.getLastName());
});
}
}
The interface TeamType contains a default method named listPlayers(). This 
method does not need to be implemented by any classes implementing TeamType since 
there is a default implementation within the interface.
 How It Works
In previous releases of Java, interfaces could only contain method signatures and 
constant variables. It was not possible to define a method implementation within an 
interface. This works well in most cases, as interfaces are a construct that is meant to 
enforce type safety and abstract implementation details. However, it is beneficial to 
allow interfaces to contain a default method implementation in some circumstances. 
For instance, if many classes implement an existing interface, lots of code can be broken 
if that interface were to be changed. This would create a situation where backward 
compatibility would not be possible. In such a case, it would make sense to place a 
default method implementation into an interface rather than forcing all classes to 
implement a new method that is placed within the interface. This is why default methods 
became a necessity and were included in the Java 8 release.
To create a default method (a.k.a. a defender method) within an interface, use the 
keyword default within the method signature, and include a method implementation. 
An interface can contain zero or more default methods. In the solution to this recipe, 
the listPlayers() method is a default method within the TeamType interface, and 
any class implementing TeamType automatically inherits the default implementation. 
Theoretically, any classes that implement TeamType would be completely unaffected by 
the addition of the listPlayers() default method. This enables you to alter an interface 
without breaking backward compatibility, which can be of great value.
Chapter 5 ObjeCt-Oriented java


173

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   96   97   98   99   100   101   102   103   ...   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling