Sunday, 20 October 2019

REST and SOAP APIs


REST APIs
  • Is an Architectural style that follows six constraints defined by Roy Fielding in 2000. Those constraints are – Uniform Interface, Client-Server, Stateless, Cacheable, Layered System, Code on Demand.
  • Follows HTTP methods and media type is a chosen by the client/server. Most used media type is JSON.
  • Uses annotations like @Path, @RestController, @Mapping etc
  • REST can use SOAP protocol but SOAP cannot use REST.

SOAP APIs
  • Is a protocol that follows a strict standard to allow communication between the client and the server
  • SOAP uses only XML for exchanging information in its message format.
  • Not only tied to HTTP transport but can also be usable by SMTP.
  • Uses @WebService on interface level.
  • SOAP has SSL( Secure Socket Layer) and WS-security.
That technology for Server APIs is extremely depends on usecase. Many developers found SOAP cumbersome and hard to use. For example, working with SOAP in JavaScript means writing a ton of code to perform extremely simple tasks because you must create the required XML structure absolutely every time.

References:
  • https://smartbear.com/blog/test-and-monitor/understanding-soap-and-rest-basics/

Friday, 11 October 2019

Observer Pattern in java

Observer pattern is also refered as Publisher/Subscriber pattern.
Examples:
  • JMS in Java
  • Event listeners like onClick

Applications:
  • When you subscribe to any website
  • Follow feature on Quora/Facebook/Instagram
  • Whatsapp Group

// Subject interface
package example.java.designpatterns;

public interface Subject {
 public void register(Observer observer);
 public void unregister(Observer observer);
 public void notifyObservers();
 public Object getUpdate(Observer observer);
}



// Observer interface

package example.java.designpatterns;

public interface Observer {
 public void update();
 public void setSubject(Subject s);
}




// Class implementing Subject interface
package example.java.designpatterns;

import java.util.ArrayList;
import java.util.List;

public class MyTopic implements Subject {
 private String message;

 private List listeners;

 public MyTopic() {
  listeners = new ArrayList<>();
 }

 public void register(Observer observer) {
  listeners.add(observer);
 }

 public void unregister(Observer observer) {
  listeners.remove(observer);
 }

 public void notifyObservers() {
  for (Observer observer : listeners) {
   observer.update();
  }
 }

 public void pushMessage(String s) {
  System.out.println("MyTopic: Adding message: " + s);
  this.message = s;
  notifyObservers();
 }

 public Object getUpdate(Observer observer) {
  return this.message;
 }
}




// Class implementing Observer interface

package example.java.designpatterns;

public class MyTopicSubscriber implements Observer {
 private Subject topic;
 
 private String name;
 
 public MyTopicSubscriber(String name) {
  this.name = name;
 }
 
 public void update() {
  String message = (String) topic.getUpdate(this);
  System.out.println("MyTopicSubscriber: " + name + " Recieved: " + message);
 }
 public void setSubject(Subject subject) {
  this.topic = subject;
 }
}