preface

The interface class in Java is usually to extract common ground, standard implementation, easy to read, handle the interface class multiple implementation and provide elegant hit concrete implementation, can help us simplify the code, improve readability; Here are a few more comfortable implementations and calls for your reference.

The sample

Enumeration implementation

The interface definition

public interface Breakfast {
    void eat();
}
Copy the code

implementation

Public enum BreakfastEnum implements Breakfast {@override public void eat() { System.out.println(" Beijing people eat bean juice and jiao jiao for breakfast "); }}, Wuhan(" Wuhan ") {@override public void eat() {system.out.println (" Wuhan ") ); }}, Unknown(" Unknown ") {@override public void eat() {system.out.println (" no breakfast! ); }}; private String city; BreakfastEnum(String city) { this.city = city; } private String getCity() { return this.city; Public static void eat(String city) {BreakfastEnum[] values = BreakfastEnum.values(); Arrays.stream(values).filter(e -> city.equals(e.city)).findFirst().orElse(Unknown).eat(); }}Copy the code

Test the

By enumeration class implements the interface, each enumeration is equivalent to an implementation, the implementation method of the code block, the last in the enumeration class provides a static method as a unified entrance, call convenient, concise code, provide general implementation processing without specific implementation scenario, suitable for replacing ifelse more business code, optimization of complex tools, etc., for many methods, Use complex services with caution.

Regular multiple implementations (invocation example)

The business scenario

We have a message service that listens for messages and sends them to the client with a publish mode field

  • 1. Send the message to the specified path according to the advertisement mode field
  • 2. Send messages to all channels

The interface definition

Public interface MessageHandle {** * @param MSG */ void publish(JSONObject MSG); }Copy the code

implementation

/ / @service (" SMS ") public class SmsMessageHandle implements MessageHandle {@override public void Publish (JSONObject MSG) {publish(JSONObject MSG) { } /** * push */ @service ("push") public class PushMessageHandle implements MessageHandle {@override public void Publish (JSONObject MSG) {// publish to app // omit implementation... }}Copy the code

application

@component public class MessageListener {@autoWired private Map<String, MessageHandle> messageHandleMap; @KafkaListener(groupId = "message-server", topics = "message") public void listener(String message, Acknowledgment ack) { JSONObject messageJson = JSON.parseObject(message); // Get the publishing method SMS push... String publishType = messagejson.getString ("publishType"); MessageHandle = messageHandlemap.get (publishType); if(handle ! = null) { handle.publish(messageJson); } // Submit the offset ack.acknowledge(); }}Copy the code
@component public class MessageListener {@autoWired private List<MessageHandle> messageHandleList; @KafkaListener(groupId = "message-server", topics = "message") public void listener(String message, Acknowledgment ack) { JSONObject messageJson = JSON.parseObject(message); Publish (messageJson); for(MessageHandle handle: messageHandleList){} handle.publish(messageJson); // Submit offset ack.acknowledge(); }}Copy the code