You are on page 1of 54

Design Patterns

Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
What is a Design Pattern?

• Name
• Problem
• Solution
• Consequences

Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Classification

• Patterns:
 Creational
 Structural
 Behavioral

Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Design Patterns

• Material for this lecture is taken from


 Design Patterns: Elements of Reusable Object-
Oriented Software
• by Gamma, Helm, Johnson, Vlissides
• ISBN: 0321700694, 9780321700698
 Head First Design Patterns
• By Freeman, Bates, Sierra, Robson
• ISBN: 978-0596007126, 0596007124

Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Creational Patterns

• The creational patterns aim to separate a system from how its


objects are created, composed, and represented. They
increase the system's flexibility in terms of the what, who,
how, and when of object creation
 Abstract factory
 Builder
 Factory method
 Prototype
 Singleton

Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Simple Factory

Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Simple Factory

Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Simple Factory

Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Simple Factory

Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Simple Factory

Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Factory Method

Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Factory Method

Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Factory Method

Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Factory Method

Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Factory Method

Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Factory Method

Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Factory Method
• The creator classes

Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Factory Method
• The product classes

Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Factory Method
• Intent
 Define an interface/abstract class for creating an object, but let
subclasses decide which class to instantiate
• Applicability
 A class can’t anticipate the class of objects it must create
 A class wants its classes to specify the objects it creates
• Structure

Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Factory Method
• Disadvantage
 Clients might have to subclass the Creator Class just to be able to
create a particular ConcreteProduct object
• Benefit
 Creating objects inside the class with a factory method is always more
flexible than creating an object directly
• Related patterns
 Abstract Factory is often implemented with factory methods
 Factory methods are usually called within Template Method
 Prototypes don’t require subclassing Creator, but they often require an
Initialize operation on Product class

Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Abstract Factory

Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Abstract Factory

Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Abstract Factory

Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Abstract Factory

Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Abstract Factory

Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Abstract Factory

Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Abstract Factory

Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Abstract Factory

Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Abstract Factory

Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Abstract Factory

Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Abstract Factory
• Intent
 Provide an interface for creating families of related or dependent
objects without specifying their concrete classes
• Applicability
 A system should be independent of how its products are created,
composed or represented
 A family of related products is designed to be used together and you
need to enforce this constraint
• Structure

Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Abstract Factory
• Advantages
 Isolates clients from implementation classes
 It makes exchanging product families easy
• Disadvantages
 It is difficult to support new kinds of products
• Related patterns
 Abstract Factory classes are often implemented with Factory Method
pattern
 But they can also be implemented with Prototype
 A concrete factory is often a Singleton

Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Prototype
• Intent
 Specify the kind of objects to create using a prototype and create new
objects by copying this prototype
• Applicability
 Use Prototype pattern when a system should be independent of how
its products are created, composed and represented
 When the classes to instantiate are specified at runtime by dynamic
loading
 To avoid building a class hierarchy of factories that parallels the class
hierarchy of products
 When instances of a class can have one of only very few combinations
of state

Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Prototype
• Structure

Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Prototype
• Advantages:
 Adding and removing products at runtime
 Specify a new object by varying values
 Reduced subclassing
• Disadvantages
 Each subclass of Prototype needs to implement Clone operation,
which might be difficult
• Clone is difficult when classes under consideration already exist
• Clone is difficult when their internals include objects that don’t support copying or
have circular references
• Implementation
 Prototype registry
 Initializing clones

Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Prototype

Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Prototype

Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Prototype

Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Prototype

Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Prototype

• Related patterns
 An Abstract Factory might store a set of prototypes from which to
clone and return product objects

Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Singleton
• Intent
 Ensure a class only has one instance and provide a global point of
access to it
• Applicability
 There must be exactly one instance of a class and it must be accessible
to clients from a well known access point
 Enforce singleton with a private constructor
 Structure

Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Singleton
• Advantages
 Controlled access to sole instance
 Reduced name space

• Implementation

Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Singleton
• Implement Singleton by creating an enum type with one
element

• Advantages
 Provides the serialization machinery for free
 Single instance guarantee even in the face of sophisticated and
serialization or reflection attacks

Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Builder
• Intent
 Separate the construction of a complex object from it’s representation
so that the same construction process can create different
representations

• Applicability
 The algorithm for crating a complex object should be independent of
the parts that make up the object and how they are assembled
 The construction process must allow different representations for the
object is constructed

Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Builder
• Structure

Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Builder
• Advantages
 It lets you vary a product’s internal representation
 It isolates code for construction and representation
 It gives you finer control over the construction process

• Related patterns
 Abstract Factory is similar to Builder in that it too may construct
complex objects
• The primary difference is that the Builder pattern focuses on constructing a
complex object step by step
 A composite is what the builder often builds

Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Builder

Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Builder

Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Builder

Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Builder

Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Builder

Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Builder

Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
Assignment
• SSE 2.0
• As a result of the positive feedback from our customers, we decided to invest some additional
effort into further developing our SSE engine. The following new requirements have been defined
for the second release:
• The spreadsheet engine will support the following additional cell types:
 Text cells – cells that contain a string of characters (for labels, table headings, etc);
 Reference cell – this type of cell contains a reference to another cell, of any type. The display value of a
reference cell is the display value of the referenced cell.
• In addition, SSE should support the following new features:
 Formula cells can reference any other type of cell (including reference and other formula cells) as their
operands;
 Cell merging – SSE should allow the merging of any number of horizontally adjacent cells. When displayed,
the newly formed super-cell will span the entire horizontal space that had been previously occupied by the
original cells. In other words the super-cell will occupy all corresponding cell coordinates in the table, so that
by using (e.g. in a formula) the coordinates of any of the old cells, the user actually references the new
super-cell. The super-cell inherits the type and display value of the leftmost cell that had been merged.
• Implement the above requirements into the current version of the SSE, updating the existing design
as appropriate.
• Provide at least a UML class diagram along with your solution. (must not be in electronic form).

Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania
To be checked
• Quality OO design
• Functional requirements
• UML
• Code style

Information Type: Working Standard, Disclosure Range: , Information Owner: iulia.jianu, NTT DATA Romania

You might also like