Upload
minhmapdeghet
View
224
Download
0
Embed Size (px)
Citation preview
7/31/2019 2 Basic Patterns
1/14
28/05/20
Design PatternsBy VVn 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
2
7/31/2019 2 Basic Patterns
2/14
28/05/20
Interface PatternDescription
An interface defines the signature operations of an enti ty, i t also sets the
communication boundary between two entities, in this case two pieces of
software. It generally refers to an abstraction that an asset provides of
itself to the outside.
The main idea of an interface is to separate functions from
implementations. Any request that matches the signature or interface of an
object may also be sent to that object, regardless of its implementation.
Since it does not matter which implementation of a specific class is used, a
class can be exchanged easily without changing the code of the calli ng class.
The concept of an interface is fundamental in most object orientedprogramming languages. In some, objects are known only through their
interfaces so there i s no way to access an object without going through it's
interface.
3
Interface PatternApplicability / Uses
Use an interface when:
o 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.
o You have to switch the implementation of a module during run-time at
design-time you don't yet know which implementation you will use at
compile-time
4
7/31/2019 2 Basic Patterns
3/14
28/05/20
Interface PatternUML
Interface defines a list of abstract methods that
implementation class must be implement.
5
Interface PatternExample without interface
We must calculate salary differently for each category of
employee.
So, if there are many categories, how about the class Employee?
6
7/31/2019 2 Basic Patterns
4/14
28/05/20
Interface PatternExample solution using interface
How about you ?
7
Abstract PatternDescription
An abstract class is a class that is declared abstract. It may
or may not include abstract methods. Abstract classes cannot be
instantiated, but they can be subclassed.
An abstract method is a method that is declared without an
implementation. Abstract classes cannot be used to instantiate objects; because
abstract classes are incomplete, i t may contain only definition of
methods and derived classes that inherit this implements it's
abstract methods.
8
7/31/2019 2 Basic Patterns
5/14
28/05/20
Abstract PatternApplicabil ity / Uses
When creating a class library which will be widely distributed or
reusedespecially to clients, use an abstract class in preference
to an interface; because, it simplifies versioning.
Use an abstract class to define a common base class for a family
of types.
Use an abstract class to provide default behavior.
Subclass only a base class in a hierarchy to which the class
logically belongs.
9
Abstract Class vs. Interface
A bstract class In terface
Deriv ed classes exhaust their single base classin heritance option.
Classes can implement multiple interfaceswithout using up their base class option. But,th ere are no default implementations.
Cannot be instantiated except as part ofsubclasses. Only derived classes can call anabstract class constructor.
Ca nnot be instantiated.
Defines abstract member signatures which
deriv ed classes must implem ent. Otherwise,th e derived class itself will be abstract.
Defines abstract member signaturesall of
which
im plem enting classes mustim plem ent. Otherwise, a compiler errorresults.
New non-abstract members may be addedthat derived classes will inherit w ithout
br eaking version compatibility.
Extending an interface with new membersbr eaks version compatibility.
Optionally , prov ide default (virtual) m em berim plementation.
All m embers are v irtual and cannot prov ideim plementations.
Ca n include data fields. Cannot include data fields. However, abstractpr operties may be declared. 10
7/31/2019 2 Basic Patterns
6/14
28/05/20
Abstract PatternUML
11
Abstract PatternExample solution using interface
12
7/31/2019 2 Basic Patterns
7/14
28/05/20
Abstract PatternExample solution using abstract
13
Tell me your thought !
Private MethodsDescription
Typically, a class is designed to offer a well-defined and related
set of services to its clients.
Some methods are intended to be used by clients of a class and
these therefore make up the class's public protocol. Other
methods are for internal use only and are concerned withimplementation details.
External client object can not directly access private methods.
This is turn hides the behavior contained in these methods from
client objects.
14
7/31/2019 2 Basic Patterns
8/14
28/05/20
Private MethodsExample
15
Accessor MethodsDescription
The state of an object is defined by the values of its instance
variables.
An object can access its own instance variables directly, but
they are hidden from other objects.
The variables that define the public state of the object must bemade accessible by adding appropriate methods to manipulate
them.
16
7/31/2019 2 Basic Patterns
9/14
28/05/20
Accessor MethodsSolution
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 wi th the same name as the instance variable
o a 'set' method with the same name as the instance variable, but is
followed by a ':' (colon)
For public boolean variables, there are generally several accessor
methods:
o 'set' methods of the form beXxxxx:and beNotXxxxxx:which set thevalue of the variable
o a 'get' method of the form isXxxxxx
17
Constant Data Manager PatternDescription
Constant Data Manager pattern is useful for designing an
efficient storage mechanism for the constant data used by
different objects in an application.
Instead of allowing the constant data to be present in different
object, the Constant Data Manager pattern recommends all suchdata be kept in a separate object and accessed by other objects
in application.
This type of separation provides an easy to maintain, centralized
repository for the constant data in application,
18
7/31/2019 2 Basic Patterns
10/14
28/05/20
Constant Data Manager PatternExample
Using constant by call ClassName.ConstantName as follow example
19
Immutable Object PatternDescription
Immutable objects are simply objects whose state (the object's
data) cannot change after construction.
Classes should be immutable 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 Pattern can be used to ensure that the
concurrent access to a data object by several client objects
does not result in any problem.
20
7/31/2019 2 Basic Patterns
11/14
28/05/20
Immutable ObjectGuidelines
Make a class immutable by following these guidelines
o ensure the class cannot be overridden - make the class final, or use static
factories and keep constructors private
o make fields private and final
o force callers to construct an object completely in a single step, instead
of using a no-argument constructor combined with subsequent calls to
setXXX methods
o do not provide any methods which can change the state of the object in
any way - not just setXXX methods, but any method which can change
state
o if the class has any mutable object fields, then they must be defensively
copied when passed between the class and its caller
21
Immutable ObjectExample
22
7/31/2019 2 Basic Patterns
12/14
28/05/20
MonitorDescription
In multithreaded environment, when method of an object are
accessed simultaneously by more than one thread, it could result
in unpredictable behavior.
The Monitor Object design pattern synchronizes concurrent
method execution to ensure that only one method at a time runs
within an object.
It also allows an objects method to cooperatively schedule their
execution sequences.
23
MonitorUML
24
synchronous communication/call
Simple Monitor
UML Stereotype Annotations
Stereotype used to indicate the pattern
7/31/2019 2 Basic Patterns
13/14
28/05/20
Questions and Answers
25
Summary
Pattern Name Description
Interface
Can be used to design a set of service provider classesthat offer the same service so that a client object can usedifferent classes of service provider objects in a seamlessmanner without having to alter the clientimplementation.
Abstract Parent Class
Useful for designing a framework for the consistent
implementation of the functionality common to a set ofrelated classes.
Private MethodsProvide a way of designing a class behavior so thatexternal objects are not permitted to access the behaviorthat is meant only for the internal use.
Accessor Methods
Provide a way of accessing an objects state usingspecific methods. This approach discourages differentclient objects from directly accessing the attributes of anobject, resulting in a more maintainable class structure.
26
7/31/2019 2 Basic Patterns
14/14
28/05/20
Summary (cont.)
Pattern Name Description
Constant Data ManagerUseful for designing an easy to maintain,centralized repository for the constant datain an application.
Immutable Object
Used to ensure that the state of an objectcannot be changed. May be used to ensure
that the concurrent access to a data objectby several client objects does not result inrace conditions.
Monitor
A way of designing an application object sothat it does not produce unpredictable
results when more than one thread tries toaccess the object at the same time in amultithreaded environment.
27