Ooc Lec5
Ooc Lec5
SWE 4301
LECTURE 5
Topic: SOLID & Open-Closed Principle
P REPARED B Y
Maliha Noushin Raida
Lecturer, Department of CSE
1
SWE 4301 Object Orientated Concepts II
Contents
1 SOLID Design Principles in Software Development 3
Page 2 of 9
SWE 4301 Object Orientated Concepts II
How can we create designs that are stable in the face of change and that will last longer
than the first version? Bertrand Meyer gave us guidance as long ago as 1988 when he coined
the now famous open-closed principle. To paraphrase him:
Software entities (classes, modules, functions, etc.) should be open for extension, but closed for
modification.
Page 3 of 9
SWE 4301 Object Orientated Concepts II
• They are “Open For Extension”. This means that the behavior of the module can be
extended. That we can make the module behave in new and different ways as the
requirements of the application change, or to meet the needs of new applications.
• They are “Closed for Modification”. The source code of such a module is inviolate. No
one is allowed to make source code changes to it.
It would seem that these two attributes are at odds with each other. The normal way to
extend the behavior of a module is to make changes to that module. A module that cannot be
changed is normally thought to have a fixed behavior.
Conformance to this principle is what yields the greatest benefits claimed for object
oriented technology; i.e. re-usability and maintainability. Yet conformance to this principle
is not achieved simply by using an object oriented programming language. Rather, it requires
a dedication on the part of the designer to apply abstraction to those parts of the program
that the designer feels are going to be subject to change.
By adhering to OCP, this encourages loose coupling between components that are closed
modifications and new functionality that is added. Usually, this is accomplished when
components depend on interface instead of concrete classes. An interface acts as a layer
of abstraction where well-defined components do not have to worry about the underlying
changes made by the concrete classes that implement the interface. Therefore, this makes
the program easy to maintain.
are concrete. The Client class uses the Server class. If we wish for a Client object to use
Page 4 of 9
SWE 4301 Object Orientated Concepts II
a different Server object, then the Client class must be changed to name the new Server
class.
In this case, the ClientInterface class is an abstract class with abstract member func-
tions. The Client class uses this abstraction; however, objects of the Client class will be
using objects of the derivative Server class. If we want Client objects to use a different server
class, then a new derivative of the ClientInterface class can be created. The Client class
can remain unchanged.
1 enum EmployeeType {
2 Permanent , Contructual , PartTime
3 }
4
5 class Employee {
6 private readonly EmployeeType type ;
7 private int tr an spo rt Al low an ce ;
8
Page 5 of 9
SWE 4301 Object Orientated Concepts II
19 } else {
20 return basic + phr * hw ;
21 }
22 }
23
How do you handle changing requirements and feedback that may affect the open-closed
principle in OOP?
• Use abstraction and inheritance: One way to adhere to the open-closed principle is
to use abstraction and inheritance to create a hierarchy of classes that share common
features but can also have specific behaviors. Abstraction is the process of hiding the
details of how something works and exposing only the essential interface. Inheritance
is the mechanism that allows a subclass to inherit the attributes and methods of a
superclass and override or extend them as needed.
• Use interfaces and polymorphism: Another way to follow the open-closed principle
is to use interfaces and polymorphism to define contracts and behaviors for different
types of objects. An interface is a collection of abstract methods that specify what an
object can do, but not how it does it. Polymorphism is the ability of an object to take
different forms and behave differently depending on the context. For example, you can
create an interface called Flyable that defines a method called fly, and then implement
this interface in different classes like Bird, Airplane, and Superman. This way, you
can use polymorphism to call the fly method on any object that implements Flyable,
without knowing or changing its internal details.
1 enum EmployeeType {
Page 6 of 9
SWE 4301 Object Orientated Concepts II
23 @Override
24 public int calculateSalary ( int basic , int phr , int hw ) {
25 return basic + basic / 4 + tra ns po rtA ll ow anc e ;
26 }
27
28 @Override
29 public int calculateBonus ( int basic ) {
30 return basic / 2;
31 }
32 }
33
34
Page 7 of 9
SWE 4301 Object Orientated Concepts II
68 // Example calculations
69 System . out . println ( " Permanent Employee Salary : " +
perma nentEmpl oyee . calculateSalary (50000 , 200 , 160) ) ;
70 System . out . println ( " Permanent Employee Bonus : " +
perma nentEmpl oyee . calculateBonus (50000) ) ;
71
Page 8 of 9
SWE 4301 Object Orientated Concepts II
Page 9 of 9