Documente Academic
Documente Profesional
Documente Cultură
Design Patterns
By V Vn Hi Faculty of Information Technologies HUI
Basic Patterns
Session objectives
Interface Pattern Abstract Class Pattern Private Methods Pattern Accessor Methods Pattern Constant Data Manager Pattern Immutable Object Pattern Monitor Pattern
28/05/2012
Interface Pattern
Description
An interface defines the signature opera tions of an en ti ty, i t also sets the
The
main
idea
of
an
interface
is
to
separa te
functions
f rom
implemen ta tions. Any request tha t ma tc hes the signa ture or interface of an object may also be sent to that object, regardless of its implementation. Since it does no t ma tter which implemen ta tion of a specific class is used, a class can be exchanged easily without changing the code of the calling class. The concept of an interface is fundamen ta l in most objec t o riented
progra mming languages. In so me, o bjec ts are known only throug h their
interfaces so there is no way to access an objec t withou t going through it's
interface.
3
Interface Pattern
Applicability / Uses
compile-time
28/05/2012
Interface Pattern
UML
Interface
d efines
list
of
abstract
methods
tha t
Interface Pattern
Example without interface
So, if there are many categories, how about the class Employee?
6
28/05/2012
Interface Pattern
Example solution using interface
Abstract Pattern
Description
An abstract class is a class tha t i s declared abstract. It may or may not include abstract methods. Abstrac t classes cannot be
An abstract method is a method tha t is declared wi thout an implementation. Abstract classes cannot be used to instantia te objec ts; because
28/05/2012
Abstract Pattern
Applicability / Uses
When crea ting a class library which will be widely distributed or reusedespecially to clients, use an abstrac t class in preference
Use an abstract class to define a common base class for a fa mily of types. Use an abstract class to provide default behavior.
Cannot be instantiated except as part of Ca nnot be instantiated. subclasses. Only deriv ed classes can call an a bstract class constructor.
Defines abstract m em ber signatures which Defines abstract m em ber signaturesall of deriv ed classes must im plem ent. Otherwise, which im plem enting classes must t h e derived class itself will be abstract. im plem ent. Otherwise, a com piler error r esults. New non-abstract m embers may be added Extending an interface with new m embers that deriv ed classes will inherit without br eaks version compatibility. br eaking version compatibility. Optionally , prov ide default (v irtual) m em ber All m embers are v irtual and cannot prov ide im plementation. im plementations. Ca n include data fields. Cannot include data fields. However, abstract pr operties may be declared. 10
28/05/2012
Abstract Pattern
UML
11
Abstract Pattern
Example solution using interface
12
28/05/2012
Abstract Pattern
Example solution using abstract
Private Methods
Description
Typically, a class is designed to offer a well-defined and related set of services to its clients.
28/05/2012
Private Methods
Example
15
Accessor Methods
Description
them.
16
28/05/2012
Accessor Methods
Solution
'set' metho ds of the form beXxxxx: and beNo tXxxxxx: which set the
Constant Da ta Manager pa ttern is useful for designing an efficient storage mechani sm for the constant da ta used by
Instead of allowing the constant da ta to be present in different object, the Constant Da ta Manager pa ttern recommend s all such data be kept in a sepa ra te objec t and accessed by other objec ts
in application.
This type of separa tion provides an ea sy to maintain, centralized repository for the constant data in application,
18
28/05/2012
19
Immutable objects are si mply objec ts whose sta te (the objec t's data) cannot change after construction.
20
10
28/05/2012
Immutable Object
Guidelines
ensure the cla ss cannot be overridden - make the class final, or u se sta tic
factories and keep constructors private make fields private and final
o o
setXXX methods
o
do no t provide any metho ds which can change the sta te of the o bjec t in any way - no t just setXXX metho ds, bu t any metho d which can change
state
o
if the class has any mu table objec t fields, then they must be defensively
Immutable Object
Example
22
11
28/05/2012
Monitor
Description
In multi threaded envi ronment, when method of an objec t are accessed si mul taneously by more than one thread, it could result
in unpredictable behavior.
The Moni tor Object design pa ttern sync hronizes concurrent method execution to ensure tha t only one method a t a ti me runs within an object.
23
Monitor UML
Simple Monitor
synchronous communication/call
12
28/05/2012
25
Summary
Pattern Name Description Can be used to design a set of service provider classes that offer the same service so that a client object can use different classes of service provider objects in a seamless manner without having to alter the client implementation.
Interface
Useful for designing a framework for the consistent Abstract Parent Class implementation of the functionality common to a set of related classes.
Private Methods
Provide a way of designing a class behavior so that external objects are not permitted to access the behavior that is meant only for the internal use. Provide a way of accessing an objects state using specific methods. This approach discourages different client objects from directly accessing the attributes of an object, resulting in a more maintainable class structure.
26
Accessor Methods
13
28/05/2012
Summary (cont.)
Pattern Name Description
Useful for designing an easy to maintain, centralized repository for the constant data in an application.
Used to ensure that the state of an object cannot be changed. May be used to ensure that the concurrent access to a data object by several client objects does not result in race conditions.
Immutable Object
Monitor
A way of designing an application object so that it does not produce unpredictable results when more than one thread tries to access the object at the same time in a multithreaded environment.
27
14