OO Design Principles
-
Upload
dai-herring -
Category
Documents
-
view
22 -
download
0
description
Transcript of OO Design Principles
04/19/23 lecture 08, OO Design Principle 1
OO Design Principles
04/19/23 lecture 08, OO Design Principle 2
Principle 2
The Open-Closed Principle: Software Entities Should be Open For
Extension, Yet Closed For Modification
04/19/23 lecture 08, OO Design Principle 3
Open-Closed Principle The open-closed principle (OCP) says that we
should attempt to design modules that never need to be changed.
To extend the behavior of the system, we need to add code. We do not modify old code
Modules that conform to OCP meet two criteria:• Open for Extension – the behavior of the module can be extended to
meet new requirements• Closed for modification – the source code of the module is not allowed
to change
How can we do this• Inheritance • Abstraction• Polymorphism• Interface
04/19/23 lecture 08, OO Design Principle 4
Open-Closed Principle It is not possible to have all the methods of a
software system satisfy the OCP, but we should minimize the number of modules that do not satisfy it.
The OCP is really the heart of OO design Conformance to this principle yields the
greatest of reusability and maintainability.
04/19/23 lecture 08, OO Design Principle 5
OCP Example In the Video Store example, we use
inheritance to represent the different categories of a Movie.
When a new category of a movie comes in, you don’t need to modify the existing code. Just add another subclass of class Movie.
Without a subclass, what construct should be used to support different categories of a Movie class?