Basic Patterns
The most common, basic and important design patterns
one can find in the areas of object-oriented design and
programming.
1
2
Interface
The Interface can be used to better design that offer the same
service to enable the client object to use different classes of service
provider objects with little or no need for altering the client code.
3
4
This design is good as long as you need to calculate the salary
for Category-A employees only and there is only one class of
objects that provides this service.
5
But the fact that the Employee object expects the salary
calculation service provider object to be always of the
CategoryA class type affects the maintainability and results
in an application design that is restrictive in terms of its
adaptability.
If we are to calculate the salary of employees who are part of
Category-B.
Try using interfaces…..
6
9
Practice question
Design a Search interface that declares methods for searching an
item in a list. Design and implement two implementers —
BinarySearch and LinearSearch — to conduct a binary and
linear search of the list, respectively.
Abstract Parent Class
An abstract class is a class with one or more abstract methods.
An abstract method has no implementation.
Abstract methods, with more than one possible implementation,
represent variable parts of the behavior of an abstract class.
An abstract class may contain implementations for other methods,
which represent the invariable parts of the class functionality.
Multiple inheritance ?
12
13
Using Interfaces instead of Abstract Classes
We could design the employee representation as
an interface, instead of designing it as an abstract class, with
both the classes as its implementers.
But doing so would require both the implementers to implement
the methods.
Redundant code.. No code reusability with interfaces.
14
15
16
17
Monitor
The synchronized keyword ensures that only one thread is allowed to
execute the log method on a given FileLogger object at any given point
in time.
18
Immutable (cannot be changed) Objects
Sometimes a class may be designed in such a way that its
instances can be used just as carriers of related data without any
specific behavior.
Such classes can be called data model classes and data objects.
19
Immutable (cannot be changed) Objects
We will not use any
setters in the class.
Only the getters.
Immutable (cannot be changed) Objects
21
22
Immutable Objects
Data model class remains unchanged over its entire lifetime.
Concurrent access to an object creates problems when one thread
can change data while a different thread is reading the same data.
Thread-safe and eliminates any concurrent access related
problems.