Design Patterns
Fundamentals Patterns
Eugeny Berkunsky, Computer Science dept.,
National University of Shipbuilding [email protected]
http://www.berkut.mk.ua
What are we talking about?
•Design patterns are reusable solutions to
common problems in software design.
•They represent best practices used by
experienced object-oriented software
developers.
•Design patterns provide a standard
terminology and are specific to particular
scenario
What are the patterns?
•Fundamentalpatterns
•Creationalpatterns
•Structuralpatterns
•Behavioralpatterns
•Specific patterns
–Concurrencypatterns
–MVC
–Enterprise
Immutable
•An object may be immutable either in whole or in part.
•In some cases, an object is considered immutable
from the point of view of the class user, even if its
internal fields change.
•As a rule, an immutable object gets all internal values
during initialization, or the values are set in several
steps, but before the object is used.
Immutable
•Often, immutable objects can be useful because they
avoid some expensive copy and compare operations.
•This simplifies the source code of a program and
speeds up its operation.
•However, in some cases, the immutability of an object
can get in the way, for example, if the object contains a
large number ofmutable objects.
•Many programming languages have the ability towork
with both mutable and immutable objects.
Immutable
•Immutable in Java/Kotlin:
–Strings
–Wrappers: Integer, Double, Character etc.
–… else?
Immutable
•Immutable in Java/Kotlin:
–String
–Wrappers: Integer, Double, Character etc.
–BigInteger, BigDecimal
–java.io.File
–java.util.Locale
–java.net.URL, java.net.URI
–…………
Immutable
•Immutable in Java:
–String
•Mutable “strings”:
–StringBuffer
–StringBuilder
Interface
•An interfaceis a fundamental design pattern
that is a general way of structuring programs
to make them easier to understand.
•In general, an interfaceis a class contract that
provides a programmer with a simple or more
specific way to access a class from other
classes.
Interface
The interfaceis the basis for building more
complex templates:
•Facade-can contain a set of objects and
provide simple, high-level functionality for the
programmer
•Adapter-can be used as a “glue” between two
different APIs and for many other purposes.
Interface
Motivations for use:
•Some object uses another object to obtain data or services from it.
If our object must explicitly specify which class the object belongs
to in order togain access, it becomes difficult to reuse our class
because of the strong coupling.
•We need to change an object that is used by other objects, and it
is impossible for those changes to affect any class other than the
class of the object being changed.
•Unfortunately, constructors cannot be accessed through an
interface because interfaces in Java do not have constructors.
Interface
Abstract Superclass
Motivations:
•Need to ensure that common logic for related classes is
implemented in the same way for each class.
•Need to avoid costs associated with development time and
maintaining redundant code.
•Need to simplify writing related classes.
•Need to specify common behavior, although in many cases
inheritance is not the most appropriate way to implement it (but,
for example, Delegation)
Abstract Superclass
Marker interface
•This pattern is used with languages that provide for
storing information about object types at runtime.
•It provides a way to associate metadata with a class if
the language does not have explicit support for such
metadata.
•Modern programming languages may use annotations
instead.
Marker interface
•Marker Interface в Java
–Cloneable
–Serializable
–java.util.EventListener
Functional design
•It is used to simplify software design.
•Functional design ensures that each module of a
program has only one responsibility and performs it
with a minimum of side effects on other parts of the
program.
•Functionally designed modules have extremely low
coupling.
Functional design
•In Java, it usually means that each method should
perform only one action.
•In addition -each class is designed to perform related
tasks.
•Classes are grouped into packages by functional
purpose: java.util, java.io, java.sql, etc.
Delegation pattern
Motivation:
•Inheritance is a static relation that does not
change over time.
•If it turns out that at different moments an
object should be represented by different
subclasses of the same class, then the given
object cannot be represented by a subclass of
that common class.
Delegation pattern
Motivation:
•If a class tries to hide a method or variable it
inherited from a superclass from other classes,
that class should not inherit from that superclass.
•There is no way to effectively hide methods and
variables inherited from a superclass.
Delegation pattern
Motivation:
•“Functional” class (a class related to the
functionality of the program) should not be a
subclass of anhelper class.
•Why?
Delegation pattern
To implement delegation, the delegating class must contain
a reference (list of references) to the class to which the
method execution is delegated.
Questions?
Design Patterns
Fundamentals Patterns
Eugeny Berkunsky, Computer Science dept.,
National University of Shipbuilding [email protected]
http://www.berkut.mk.ua