Design Patterns
Fundamentals Patterns
Eugeny Berkunsky, Computer Science dept.,
National University of Shipbuilding
eugeny.berkunsky@gmail.com
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?
• Fundamental patterns
• Creational patterns
• Structural patterns
• Behavioral patterns
• Specific patterns
– Concurrency patterns
– MVC
– Enterprise
Fundamental patterns
• Immutable
• Interface
• Abstract Superclass
• Marker interface
• Functional design
• Delegation pattern
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 of mutable objects.
• Many programming languages have the ability to work
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 interface is 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.
Interface
The interface is 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 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.
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 an helper 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
eugeny.berkunsky@gmail.com
http://www.berkut.mk.ua

Fundamental Patterns in Java Introduction

  • 1.
    Design Patterns Fundamentals Patterns EugenyBerkunsky, Computer Science dept., National University of Shipbuilding eugeny.berkunsky@gmail.com http://www.berkut.mk.ua
  • 2.
    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
  • 4.
    Fundamental patterns • Immutable •Interface • Abstract Superclass • Marker interface • Functional design • Delegation pattern
  • 5.
    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?
  • 8.
    Immutable • Immutable inJava/Kotlin: – String – Wrappers: Integer, Double, Character etc. – BigInteger, BigDecimal – java.io.File – java.util.Locale – java.net.URL, java.net.URI – …………
  • 9.
    Immutable • Immutable inJava: – String • Mutable “strings”: – StringBuffer – StringBuilder
  • 10.
    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.
  • 13.
  • 14.
    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)
  • 15.
  • 16.
    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.
  • 17.
    Marker interface • MarkerInterface в Java – Cloneable – Serializable – java.util.EventListener
  • 18.
    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.
  • 22.
    Delegation pattern Motivation: • “Functional”class (a class related to the functionality of the program) should not be a subclass of an helper class. • Why?
  • 23.
    Delegation pattern To implementdelegation, the delegating class must contain a reference (list of references) to the class to which the method execution is delegated.
  • 24.
  • 25.
    Design Patterns Fundamentals Patterns EugenyBerkunsky, Computer Science dept., National University of Shipbuilding eugeny.berkunsky@gmail.com http://www.berkut.mk.ua