Uncategorized

Java8 functional programming vs OOPS

Problem statement

Problem is to display object type on console given a list of objects. Object can be any of rectangle, circle, parallelogram etc. For e.g# if object is a circle display “I am a circle” on console etc.

OOPS approach#

Below is the OOPS approach to solve the problem.We end up creating a bunch of classes and interfaces.  

Fig 1 OOPS design for the above problem


Code structure looks like below. We create a lot of objects even though we are only defining the behavior and no state is required to be maintained.

A simple problem has a lot of boilerplate code with OOPS approach.





IObject.java

package com.gotechmind.oldcode;

public interface IObject {
	void display();
}

Rectangle.java

package com.gotechmind.oldcode;

public class Rectangle implements IObject{

	private int edges;
	
	public Rectangle(int edges) {
		this.edges = edges;
	}
	
	@Override
	public void display() {
		System.out.println("I am a Rectangle number of edges=" + edges);
	}

}

ParalelloGram.java

package com.gotechmind.oldcode;

public class ParalelloGram  implements IObject{

	private int edges;
	
	public ParalelloGram (int edges) {
		this.edges = edges;
	}
	
	@Override
	public void display() {
		System.out.println("I am a ParalelloGram number of edges=" + edges);
	}

}

Circle.java

package com.gotechmind.oldcode;

public class Circle implements IObject{

	private int edges;
	
	public Circle(int edges) {
		this.edges = edges;
	}
	
	@Override
	public void display() {
		System.out.println("I am a Circle number of edges=" + edges);
	}

}

DrawingDemo.java

package com.gotechmind.oldcode;

public class DrawingDemo {
	public static void main(String args[]) {
		IObject[] objects = new IObject[] {	new Circle(0),
				new ParalelloGram(4),
				new Rectangle(4)
		};

		for(IObject object : objects) {
			object.display();
		}

	}
}

Functional programming

Now lets check the solution with java8 functional programming.

  1. We use the Consumer functional interface provided by Java8. The idea is to simple put the input to System.out.println as a consumer.
  2. New code structure is only a single class.

ObjectEnum.java

package com.gotechmind.java8;

import java.util.function.Consumer;

public enum ObjectEnum {
	
	RECTANGLE(4,ObjectEnum::displayRectangle),
	PARALELLOGRAM(4,ObjectEnum::displayParalellogram),
	CIRCLE(0,ObjectEnum::displayCircle);

	public static void displayRectangle(Integer edges) {
		System.out.println("I am a Rectangle number of edges=" + edges);
	}
	
	public static void displayParalellogram(Integer edges) {
		System.out.println("I am a Paralellogram number of edges=" + edges);
	}
	
	public static void displayCircle(Integer edges) {
		System.out.println("I am a Circle number of edges=" + edges);
	}
	
	private Integer edges;
	private Consumer<Integer> displayFunction;
	
	private ObjectEnum(Integer edges, Consumer<Integer> displayFunction) {
		this.setEdges(edges);
		this.setDisplayFunction(displayFunction);
	}

	public Consumer<Integer> getDisplayFunction() {
		return displayFunction;
	}

	public void setDisplayFunction(Consumer<Integer> displayFunction) {
		this.displayFunction = displayFunction;
	}

	public Integer getEdges() {
		return edges;
	}

	public void setEdges(Integer edges) {
		this.edges = edges;
	}
	
}

DrawingDemo.java

package com.gotechmind.java8;

public class DrawingDemo {
	public static void main(String args[]) {
		ObjectEnum[] objects = new ObjectEnum[] { ObjectEnum.CIRCLE
				, ObjectEnum.PARALELLOGRAM
				, ObjectEnum.RECTANGLE };

		for(ObjectEnum objectEnum : objects) {
			objectEnum.getDisplayFunction().accept(objectEnum.getEdges());
		}

	}
}

Git code location

https://github.com/gotechmind/Java8/tree/master/drawing-demo

Conclusion

All boilerplate code is removed and code itself is more readable.

Leave a Reply

Your email address will not be published. Required fields are marked *