Sunteți pe pagina 1din 14

28/05/2012

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

communication bounda ry between two en tities, in this case two pieces of


sof tware. It g enerally refers to an a bstrac tion tha t an asset provides of

itself to the outside.

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

Use an interface when:


You want to specify how classes exchange messages. I.e., every time,
when a class should be reused, or used outside a specific context (package) declare the communication interface as an Interface type. You have to switch the i mplementation of a module during run-time at design-time you don't yet know which i mplementation you will use at

compile-time

28/05/2012

Interface Pattern
UML

Interface

d efines

list

of

abstract

methods

tha t

implementation class must be implement.


5

Interface Pattern
Example without interface

We must calculate salary differently for each category of employee.

So, if there are many categories, how about the class Employee?
6

28/05/2012

Interface Pattern
Example solution using interface

How about you ?


7

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

instantiated, but they can be subclassed.

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

abstract classes a re incomplete, i t may contain only defini tion of


methods and derived classes tha t inheri t thi s implements i t's abstract methods.
8

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

to an interface; because, it simplifies versioning.

Use an abstract class to define a common base class for a fa mily of types. Use an abstract class to provide default behavior.

Subclass only a ba se class in a hierarchy to which the class


logically belongs.

Abstract Class vs. Interface


A bstract class In terface Deriv ed classes exhaust their single base class Classes can im plem ent multiple interfaces in heritance option. without using up their base class option. But, t h ere are no default implementations.

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

Tell me your thought !


13

Private Methods
Description

Typically, a class is designed to offer a well-defined and related set of services to its clients.

Some method s are intended to be used by clients of a class and


these therefore make up the class's public protocol. Other methods a re for internal use only and are concerned with implementation details.

External client object can not directly access private methods.


This is turn hides the behavior contained in these methods from client objects.
14

28/05/2012

Private Methods
Example

15

Accessor Methods
Description

The state of an object is defined by the values of its instance variables.

An object can access i ts own instance variables directly, but


they are hidden from other objects. The variables tha t d efine the public sta te of the objec t must be made accessible by adding appropria te methods to manipula te

them.

16

28/05/2012

Accessor Methods
Solution

So called, accessor methods, provide a mechanism for getting and


setting the instance variables of an object. For public non-boolean instance variables, there should be two accessor methods:
o

a 'get' method with the same name as the instance variable

a 'set' metho d with the sa me name a s the instance variable, bu t is


followed by a ':' (colon)

For public boolean variables, there are generally several accessor


methods:
o

'set' metho ds of the form beXxxxx: and beNo tXxxxxx: which set the

value of the variable


o

a 'get' method of the form isXxxxxx


17

Constant Data Manager Pattern


Description

Constant Da ta Manager pa ttern is useful for designing an efficient storage mechani sm for the constant da ta used by

different objects in an application.

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

Constant Data Manager Pattern


Example

Using constant by call ClassName.ConstantNa me as follow example

19

Immutable Object Pattern


Description

Immutable objects are si mply objec ts whose sta te (the objec t's data) cannot change after construction.

Classes should be i mmutable unless there's a very good reason


to make them mutable....If a class cannot be made immutable, limit its mutability as much as possible.

Immutable Object Pa ttern can be used to ensure tha t the

concurrent access to a da ta object by several client objects


does not result in any problem.

20

10

28/05/2012

Immutable Object
Guidelines

Make a class immutable by following these guidelines


o

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

force callers to construc t an o bjec t co mpletely in a single step, instead


of using a no-argumen t construc to r combined with su bsequ en t calls to

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

copied when passed between the class and its caller


21

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.

It also allows an objects method to coopera tively schedule their


execution sequences.

23

Monitor UML

Simple Monitor

synchronous communication/call

UML Stereotype Annotations


Stereotype used to indicate the pattern
24

12

28/05/2012

Questions and Answers

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

Constant Data Manager

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

S-ar putea să vă placă și