Sparkles

that were shone when I got tempered!

OCP – Open Close Principle

with 7 comments

This is firstly described by Bertrand Mayer as “software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification”.

This is basically for designing a code as can be acted more robust when you need to add a new feature.

It makes the code more strong by making it Open for Extension and Closed for modification.

How to make a class open for extension and closed for modification at the same time?

You can make a class closed for modification by open it for extension.

So, in the Bad OCP version, the implementation logic for each sub class is included in the centralized control hub. But in the Good OCP version the implementation is delegate to each sub classes. So no need to change the centralized code each time you need to add a new feature.

Bad OCP Design

AreaCalculator .java

package ocp.bad;

import ocp.bad.shapes.Circle;
import ocp.bad.shapes.Rectangle;
import ocp.bad.shapes.Shape;

public class AreaCalculator {

public double caclculateArea(Shape[] shapes){
double totalArea = 0d;

for (Shape shape : shapes) {

double area = 0d;

if(shape instanceof Rectangle){
area = ((Rectangle) shape).getHeight() * ((Rectangle) shape).getWidth();
System.out.println("Area of the rectangle : "+area);
}else if (shape instanceof Circle){
area = ((Circle) shape).getRadius() * ((Circle) shape).getRadius() * Math.PI;
System.out.println("Area of the circle : "+area);
}
// When more shapes added more else-if need to be added s and changes of the code here.

totalArea += area;
}
System.out.println("Returning totalArea : "+totalArea);
return totalArea;
}

}

Shape.java

package ocp.bad.shapes;

public class Shape {}

Rectangle.java

package ocp.bad.shapes;

public class Rectangle extends Shape {

	public Rectangle( double width, double height) {
		this.width 	= width;
		this.height = height;
	}

	public double getWidth() {
		return width;
	}

	public double getHeight() {
		return height;

	}

	private double width = 0d;
	private double height = 0d;

}

Circle.java

package ocp.bad.shapes;

public class Circle extends Shape {

	public Circle(double radius) {
		this.radius = radius;
	}

	public double getRadius() {
		return radius;
	}

	private double radius = 0d;

}

Good OCP Design

Here, the implementation logic (what it differs has been delegated to its own sub classes). So, no need to change the centralized code each time you need to add a new feature.

AreaCalculator .java

package ocp.good;

import ocp.good.shapes.Shape;

public class AreaCalculator {

	public double caclculateArea(Shape[] shapes){
		double totalArea = 0d;

		for (Shape shape : shapes) {

			totalArea += shape.calculateArea();
			// When more shapes added no need to change here. Implementation has deligate to the new objects through abstraction
		}
		System.out.println("Returning totalArea : "+totalArea);
		return totalArea;
	}

}

Shape.java

package ocp.good.shapes;

public abstract class Shape {
	public abstract double calculateArea();
}

Rectangle.java

package ocp.good.shapes;

public class Rectangle extends Shape {

	public Rectangle( double width, double height) {
		this.width 	= width;
		this.height = height;
	}

	@Override
	public double calculateArea() {
		return width * height;
	}

	private double width = 0d;
	private double height = 0d;

}

Circle.java

package ocp.good.shapes;

public class Circle extends Shape {

	public Circle(double radius) {
		this.radius = radius;
	}

	@Override
	public double calculateArea() {
		return radius * radius * Math.PI;
	}

	private double radius = 0d;

}

References :

Advertisements

Written by Namal Fernando

July 28, 2013 at 4:20 pm

7 Responses

Subscribe to comments with RSS.

  1. This article will help the internet people for setting up new weblog or even a blog from start to end.

    www.royalpartybus.ca

    August 15, 2014 at 6:03 am

  2. I think this is among the most important info for me. And i am glad reading your
    article. But want to remark on some general things, The site style is
    perfect, the articles is really nice : D. Good job,
    cheers

    night stands for bedrooms

    September 2, 2014 at 6:21 pm

  3. excellent put up, very informative. I’m wondering why the other experts of this sector do not
    realize this. You should proceed your writing. I’m sure, you’ve a huge
    readers’ base already!

    navy

    September 4, 2014 at 3:22 pm

  4. Good way of telling, and fastidious post to obtain data on the topic of my presentation topic,
    which i am going to deliver in college.

    How Can I Protect Myself

    September 24, 2014 at 6:37 pm

  5. I have been exploring for a bit for any high quality articles or weblog posts on this sort of area .
    Exploring in Yahoo I ultimately stumbled upon this web site.
    Studying this info So i am happy to exhibit that I have a very good uncanny feeling I discovered exactly what
    I needed. I most definitely will make certain to do
    not omit this site and give it a glance regularly.

    template wordpress

    October 18, 2014 at 6:09 am

  6. Excellent post. I was checking constantly this blog and I am
    impressed! Extremely helpful information specially the last part :
    ) I care for such information much. I was looking for this certain info for a very long time.
    Thank you and best of luck.

    Blog

    November 4, 2014 at 2:12 pm

  7. I am now not positive the place you are getting your information, however
    great topic. I needs to spend a while studying much more or working out
    more. Thank you for fantastic info I was searching for this info for my mission.

    Mace

    November 7, 2014 at 12:55 am


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Ruth's Reflections

Contemplations from quakey quirky Christchurch

TED Blog

The TED Blog shares interesting news about TED, TED Talks video, the TED Prize and more.

Ziplok

Learn and discover simple things

Meihta Dwiguna Saputra's Knowledge Base

~In learning you will teach and in teaching you will (re)learn~

The Java Blog

Thoughts, tips and tricks about the Java programming language

Sparkles

that were shone when I got tempered!

%d bloggers like this: