What are wetalking 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
3.
What are thepatterns?
• Fundamental patterns
• Creational patterns
• Structural patterns
• Behavioral patterns
• Specific patterns
– Concurrency patterns
– MVC
– Enterprise
Immutable
• An objectmay 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.
6.
Immutable
• Often, immutableobjects 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 of mutable objects.
• Many programming languages have the ability to work
with both mutable and immutable objects.
7.
Immutable
• Immutable inJava/Kotlin:
– Strings
– Wrappers: Integer, Double, Character etc.
– … else?
Interface
• An interfaceis a fundamental design pattern
that is a general way of structuring programs
to make them easier to understand.
• In general, an interface is a class contract that
provides a programmer with a simple or more
specific way to access a class from other
classes.
11.
Interface
The interface isthe 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.
12.
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 to gain 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.
Abstract Superclass
Motivations:
• Needto 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)
Marker interface
• Thispattern 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.
Functional design
• Itis 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.
19.
Functional design
• InJava, 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.
20.
Delegation pattern
Motivation:
• Inheritanceis 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.
21.
Delegation pattern
Motivation:
• Ifa 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
To implementdelegation, the delegating class must contain
a reference (list of references) to the class to which the
method execution is delegated.