Docsity

Using Design Patterns in Object-Oriented Software Engineering, Slides of Banking and Finance

Partial preview of the text

Download Using Design Patterns in Object-Oriented Software Engineering and more Slides Banking and Finance in PDF only on Docsity!

Object-Oriented Software Engineering

Practical Software Development using UML and Java

Chapter 6:

Using Design Patterns

6.1 Introduction to Patterns

The recurring aspects of designs are called design patterns.

  • A pattern is the outline of a reusable solution to a general problem encountered in a particular context
  • Many of them have been systematically documented for all software developers to use
  • A good pattern should —Be as general as possible —Contain a solution that has been proven to effectively solve the problem in the indicated context. Studying patterns is an effective way to learn from the experience of others

6.2 The Abstraction-Occurrence Pattern

  • Context : —Often in a domain model you find a set of related objects ( occurrences). —The members of such a set share common information - but also differ from each other in important ways.
  • Problem : —What is the best way to represent such sets of occurrences in a class diagram?
  • Forces : —You want to represent the members of each set of occurrences without duplicating the common information

Abstraction-Occurrence

  • Solution:

Abstraction-Occurrence

Square variant

6.3 The General Hierarchy Pattern

  • Context : —Objects in a hierarchy can have one or more objects above them (superiors), - and one or more objects below them (subordinates). —Some objects cannot have any subordinates
  • Problem : —How do you represent a hierarchy of objects, in which some objects cannot have subordinates?
  • Forces : —You want a flexible way of representing the hierarchy - that prevents certain objects from having subordinates —All the objects have many common properties and operations
  • Solution:

General Hierarchy

General Hierarchy

Antipattern:

Player-Role

  • Forces : —It is desirable to improve encapsulation by capturing the information associated with each separate role in a class. —You want to avoid multiple inheritance. —You cannot allow an instance to change class
  • Solution :

Player-Role

Example 1:

Player-Role

Antipatterns:

• Merge all the properties and behaviours into a single

«Player» class and not have «Role» classes at all.

• Create roles as subclasses of the «Player» class.

6.5 The Singleton Pattern

• Context :

—It is very common to find classes for which only one

instance should exist ( singleton)

• Problem :

—How do you ensure that it is never possible to create

more than one instance of a singleton class?

• Forces :

—The use of a public constructor cannot guarantee that

no more than one instance will be created.

—The singleton instance must also be accessible to all

classes that require it

6.6 The Observer Pattern

• Context :

—When an association is created between two classes,

the code for the classes becomes inseparable.

—If you want to reuse one class, then you also have to

reuse the other.

• Problem :

—How do you reduce the interconnection between

classes, especially between classes that belong to

different modules or subsystems?

• Forces :

—You want to maximize the flexibility of the system

to the greatest extent possible

Observer

  • Solution: