# SOLID Principles: Open/Closed Principle

# SOLID Principles: Open/Closed Principle

### This next SOLID tutorial focuses on the 'O' — the open/closed principle. See how to design code to be open for extension but closed for modification.

Join the DZone community and get the full member experience.

Join For FreeHow do you break a Monolith into Microservices at Scale? This ebook shows strategies and techniques for building scalable and resilient microservices.

Previously, we talked about the single responsibility principle. The open/closed principle is the second SOLID principle.

“Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification.”

By employing that principle, the goal is to extend a module’s behavior without modifying its source code.

Imagine a scenario of applying a discount to a product. A discount service will apply the discount specified and give back the discounted price.

Currently, our system has only one kind of discount, which applies to all adults:

```
package com.gkatzioura.solid.ocp;
import java.math.BigDecimal;
import java.math.RoundingMode;
public class Discount {
public BigDecimal apply(BigDecimal price) {
BigDecimal percent = new BigDecimal("0.10");
BigDecimal discount = price.multiply(percent);
return price.subtract(discount.setScale(2, RoundingMode.HALF_UP));
}
}
```

And the discount service shall apply this discount to the price given.

```
package com.gkatzioura.solid.ocp;
import java.math.BigDecimal;
public class DiscountService {
public BigDecimal applyDiscount(BigDecimal price,Discount discount) {
return discount.apply(discountPrice);
}
}
```

However, our company wants to offer a discount to seniors, thus we have the Senior Discount.

```
package com.gkatzioura.solid.ocp;
import java.math.BigDecimal;
import java.math.RoundingMode;
public class SeniorDiscount {
public BigDecimal apply(BigDecimal price) {
BigDecimal percent = new BigDecimal("0.20");
BigDecimal discount = price.multiply(percent);
return price.subtract(discount.setScale(2, RoundingMode.HALF_UP));
}
}
```

This makes things a little more complicated for the discount service, since the service has to apply both the discount for adults and both the discounts for seniors.

```
package com.gkatzioura.solid.ocp;
import java.math.BigDecimal;
public class DiscountService {
public BigDecimal applyDiscount(BigDecimal price,Discount discount) {
return discount.apply(price);
}
public BigDecimal applySeniorDiscount(BigDecimal price,SeniorDiscount discount) {
return discount.apply(price);;
}
}
```

By doing so, we modified the discount service's source code to extend its behavior. Also, for every different discount that the sales department might come up with, the discount service will get extra methods.

In order to follow the open/closed principle, we will create a discount interface.

```
package com.gkatzioura.solid.ocp;
import java.math.BigDecimal;
public interface Discount {
BigDecimal apply(BigDecimal price);
}
```

The default discount will be renamed to AdultDiscount and implement the discount interface.

```
package com.gkatzioura.solid.ocp;
import java.math.BigDecimal;
import java.math.RoundingMode;
public class AdultDiscount implements Discount {
@Override
public BigDecimal apply(BigDecimal price) {
BigDecimal percent = new BigDecimal("0.10");
BigDecimal discount = price.multiply(percent);
return price.subtract(discount.setScale(2, RoundingMode.HALF_UP));
}
}
```

The SeniorDiscount will also implement the Discount interface.

```
package com.gkatzioura.solid.ocp;
import java.math.BigDecimal;
import java.math.RoundingMode;
public class SeniorDiscount implements Discount {
@Override
public BigDecimal apply(BigDecimal price) {
BigDecimal percent = new BigDecimal("0.20");
BigDecimal discount = price.multiply(percent);
return price.subtract(discount.setScale(2, RoundingMode.HALF_UP));
}
}
```

Last but not least, our DiscountService will be refactored in order to apply discounts based on the Discount interface.

```
package com.gkatzioura.solid.ocp;
import java.math.BigDecimal;
public class DiscountService {
public BigDecimal applyDiscounts(BigDecimal price,Discount[] discounts) {
BigDecimal discountPrice = price.add(BigDecimal.ZERO);
for(Discount discount:discounts) {
discountPrice = discount.apply(discountPrice);
}
return discountPrice;
}
}
```

This way, the discount service will be able to apply different discounts without altering its source code.

The same principle can be applied to the Discount. Suppose we want to have a basic discount applied extra when a discount is applied.

```
package com.gkatzioura.solid.ocp;
import java.math.BigDecimal;
import java.math.RoundingMode;
public class BasicDiscount implements Discount {
@Override
public BigDecimal apply(BigDecimal price) {
BigDecimal percent = new BigDecimal("0.01");
BigDecimal discount = price.multiply(percent);
return price.subtract(discount.setScale(2, RoundingMode.HALF_UP));
}
}
```

By extending the BasicDiscount class, we are able to have more discounts with the behavior of the BasicDiscount and also extend this behavior without modifying the BasicDiscount source code.

You can find the source code on GitHub. The next principle is the Liskov Substitution Principle.

How do you break a Monolith into Microservices at Scale? This ebook shows strategies and techniques for building scalable and resilient microservices.

Published at DZone with permission of Emmanouil Gkatziouras , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

## {{ parent.title || parent.header.title}}

## {{ parent.tldr }}

## {{ parent.linkDescription }}

{{ parent.urlSource.name }}