Tecniche di Progettazione: Design Patterns

Similar documents
Tecniche di Progettazione: Design Patterns

Tecniche di Progettazione: Design Patterns

Factory Method. Comp435 Object-Oriented Design. Factory Method. Factory Method. Factory Method. Factory Method. Computer Science PSU HBG.

Tecniche di Progettazione: Design Patterns

Laboratorio di Progettazione di Sistemi Software Design Pattern Creazionali. Valentina Presutti (A-L) Riccardo Solmi (M-Z)

Tecniche di Progettazione: Design Patterns

SDC Design patterns GoF

Tecniche di Progettazione: Design Patterns

Cloning Enums. Cloning and Enums BIU OOP

Tecniche di Progettazione: Design Patterns

Ingegneria del Software Corso di Laurea in Informatica per il Management. Design Patterns part 1

Without violating encapsulation, capture and externalize an object's internal state so that the object can be restored to this state later.

Prototype. Design Pattern. from: Design Patterns (Gamma, Helm, Johnson, and Vlissides) p Justin Hendrix CSPP Winter 2010

Tecniche di Progettazione: Design Patterns

Chapter 13 Abstract Classes and Interfaces

CSE 331 Lecture 4. Comparing objects; cloning

getimage getimage Parameters: Returns: See Also:

Tecniche di Progettazione: Design Patterns

Design Patterns: Prototype, State, Composite, Memento

Ingegneria del Software Corso di Laurea in Informatica per il Management. Design Patterns part 1

to avoid building a class hierarchy of factories that parallels the class hierarchy of products; or

Tecniche di Progettazione: Design Patterns

Exercises Software Development I. 08 Objects II. Generating and Releasing Objects (Constructors/Destructors, this, Object cloning) December 3rd, 2014

List ADT. Announcements. The List interface. Implementing the List ADT

Design Patterns. An introduction

Object-Oriented Design

Tecniche di Progettazione: Design Patterns

Design Pattern- Creational pattern 2015

Creational Patterns. Factory Method (FM) Abstract Factory (AF) Singleton (SI) Prototype (PR) Builder (BU)

Abstract Classes and Interfaces

Design Patterns Reid Holmes

Design Pattern and Software Architecture: IV. Design Pattern

Design Patterns Reid Holmes

Motivations. Objectives. object cannot be created from abstract class. abstract method in abstract class

Trusted Components. Reuse, Contracts and Patterns. Prof. Dr. Bertrand Meyer Dr. Karine Arnout

Programming II (CS300)

Design Patterns. Manuel Mastrofini. Systems Engineering and Web Services. University of Rome Tor Vergata June 2011

DESIGN PATTERN - INTERVIEW QUESTIONS

Keywords: Abstract Factory, Singleton, Factory Method, Prototype, Builder, Composite, Flyweight, Decorator.

Programming II (CS300)

I, J. Key-value observing (KVO), Label component, 32 text property, 39

Tecniche di Progettazione: Design Patterns

Chapter 13 Abstract Classes and Interfaces. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. Pearson Education Limited

Chapter 5 Object-Oriented Programming

Basic Principles of OO. Example: Ice/Water Dispenser. Systems Thinking. Interfaces: Describing Behavior. People's Roles wrt Systems

The Object clone() Method

administrivia today UML start design patterns Tuesday, September 28, 2010

GoF Design Pattern Categories

Chapter 13. Interfaces and Inner Classes

Object-Oriented Oriented Programming Command Pattern. CSIE Department, NTUT Woei-Kae Chen

public Candy() { ingredients = new ArrayList<String>(); ingredients.add("sugar");

COSC This week. Cloning. Cloning: Be Cautious. Cloning: the wrong type. Cloning: use super.clone()

Software Design Patterns. Background 1. Background 2. Jonathan I. Maletic, Ph.D.

4.1 Introduction Programming preliminaries Constructors Destructors An example... 3

Modellistica Medica. Maria Grazia Pia, INFN Genova. Scuola di Specializzazione in Fisica Sanitaria Genova Anno Accademico

Software Construction

Object-Oriented Oriented Programming

The Object Class. java.lang.object. Important Methods In Object. Mark Allen Weiss Copyright 2000

Idioms and Design Patterns. Martin Skogevall IDE, Mälardalen University

Logistics. Final Exam on Friday at 3pm in CHEM 102

Design Patterns. Memento Pattern. Kasım 2017

Chapter 13 Abstract Classes and Interfaces

EPL 603 TOPICS IN SOFTWARE ENGINEERING. Lab 6: Design Patterns

Tirgul 1. Course Guidelines. Packages. Special requests. Inner classes. Inner classes - Example & Syntax

CS 112 Programming 2. Lecture 10. Abstract Classes & Interfaces (1) Chapter 13 Abstract Classes and Interfaces

CMSC 132: Object-Oriented Programming II

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Object-Oriented Design

DESIGN PATTERNS FOR MERE MORTALS

Design Patterns and Frameworks Command

Design Patterns. Comp2110 Software Design. Department of Computer Science Australian National University. Second Semester

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

TDDB84: Lecture 6. Adapter, Bridge, Observer, Chain of Responsibility, Memento, Command. fredag 4 oktober 13

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

Prototype Pattern Creational

OODP Session 4. Web Page: Visiting Hours: Tuesday 17:00 to 19:00

Ryerson University Department of Electrical & Computer Engineering COE618 Midterm Examination February 26, 2013

Objects and Serialization

Problem other classes messing with my stuff!

Creational Design Patterns

Data abstractions: ADTs Invariants, Abstraction function. Lecture 4: OOP, autumn 2003

(c) Bartosz Walter 1

The Strategy Pattern Design Principle: Design Principle: Design Principle:

Development and Implementation of Workshop Management System Application to Explore Combing Multiple Design Patterns

Motivations. Chapter 13 Abstract Classes and Interfaces

The static keyword Used to denote fields and methods that belong to a class (but not to any particular object).

Prototype Description. Interpreter. interpreter Calculator Design Rationales. Prototype Participants. Interpreter with Factory Method.

Inheritance and Interfaces

CMSC 132: Object-Oriented Programming II

Prototype Pattern Creational

A Reconnaissance on Design Patterns

Modellistica Medica. Maria Grazia Pia, INFN Genova. Scuola di Specializzazione in Fisica Sanitaria Genova Anno Accademico

Design Patterns. Manuel Mastrofini. Systems Engineering and Web Services. University of Rome Tor Vergata June 2011

CHAPTER 6: CREATIONAL DESIGN PATTERNS

SOLID DESIGN PATTERNS FOR MERE MORTALS

Factory Method Pattern Creational. » Define an interface for creating an object but lets subclasses decide the specific class to instantiate

Using Design Patterns in Java Application Development

Design Pattern What is a Design Pattern? Design Pattern Elements. Almas Ansari Page 1

Socket attaches to a Ratchet. 2) Bridge Decouple an abstraction from its implementation so that the two can vary independently.

Transcription:

Tecniche di Progettazione: Design Patterns GoF: Memento Prototype Visitor 1 Design patterns, Laura Semini, Università di Pisa, Dipartimento di Informatica.

Memento 2 Design patterns, Laura Semini, Università di Pisa, Dipartimento di Informatica.

Memento Intent Without violating encapsulation, capture and externalize an object s internal state so that the object can be restored to this state later. Motivation When we want to store off an object s internal state without adding any complication to the object s interface. Perhaps for an undo mechanism

Memento pattern Memento: a saved "snapshot" of the state of an object or objects for possible later use useful for: writing an Undo / Redo operation ensuring consistent state in a network Persistency: save / load state between executions of program 4

Applicability Use this when you want to save state on a hierarchy s elements. When the hierarchy s interface would be broken if implementation details were exposed.

Structure

Participants Memento stores the state of the Originator Originator Creates the memento Uses the memento to restore its internal state CareTaker Keeps track of the Memento(s) Never uses the Memento s Interface to the Originator

Collaboration Caretaker requests a memento from an Originator. Originator passes back memento. Originator uses it to restore state.

Collaboration 9 Design patterns, Laura Semini, Università di Pisa, Dipartimento di

Consequences (good) Preserves Encapsulation Boundaries It simplifies Originator

Consequences (bad) Might be expensive Difficulty defining interfaces to keep Originator encapsulated Hidden costs in caring for mementos Caretaker could have to keep track of a lot of information for the memento

Storing Incremental Changes If storing state happens incrementally, then we can just record the changes of what s happened in a new memento object. This helps with memory difficulties.

Exercise: re-engineer the commandmemento example 13 Design patterns, Laura Semini, Università di Pisa, Dipartimento di

Holte Pratic Prototype 14 Design patterns, Laura Semini, Università di Pisa, Dipartimento di Informatica.

Prototype Pattern A creational pattern Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype

Applicability when a system should be independent of how its products are created, composed, and represented and any of the following is the case: when the classes to instantiate are specified at run-time avoid building a class hierarchy of factories that parallels the class hierarchy of products when instances of a class can have one of only a few different combinations of state. It may be more convenient to have the proper number of prototypes and clone them rather than instantiate the class manually each time with appropriate state. 16 Design patterns, Laura Semini, Università di Pisa, Dipartimento di Informatica.

Structure & Participants Prototype(Graphic) -declares an interface for cloning itself. ConcretePrototype (Staff,WholeNote, HalfNote) -implements an operation for cloning itself. Client(GraphicalTo ol) - creates a new object by asking a prototype to clone itself.

Problem

Prototype solution

Benefits of Prototype Pattern Hides the complexities of making new instances from the client. Provides the option for the client to generate objects whose type is not known. In some circumstances, copying an object can be more efficient than creating a new object.

Implementation of Prototype Pattern It is built on the method.clone()

java.lang Class Object protected Object clone() throws CloneNotSupportedException Creates and returns a copy of this object. The precise meaning of "copy" may depend on the class of the object. The general intent is that, for any object x, the expression: x.clone()!= x will be true, and that the expression: x.clone().getclass() == x.getclass() will be true, but these are not absolute requirements. While it is typically the case that: x.clone().equals(x) will be true, this is not an absolute requirement. By convention, the returned object should be obtained by calling super.clone. If a class and all of its superclasses (except Object) obey this convention, it will be the case that x.clone().getclass() == x.getclass(). 22 Design patterns, Laura Semini, Università di Pisa, Dipartimento di Informatica.

java.lang Class Object protected Object clone() throws CloneNotSupportedException By convention, the object returned by this method should be independent of this object (which is being cloned). To achieve this independence, it may be necessary to modify one or more fields of the object returned by super.clone before returning it. Typically, this means copying any mutable objects that comprise the internal "deep structure" of the object being cloned and replacing the references to these objects with references to the copies. If a class contains only primitive fields or references to immutable objects, then it is usually the case that no fields in the object returned by super.clone need to be modified. 23 Design patterns, Laura Semini, Università di Pisa, Dipartimento di Informatica.

Clone and deep/shallow copy Clone can be implemented either as a deep copy or a shallow copy: In a deep copy, all objects are duplicated, In a shallow copy, only the top-level objects are duplicated and the lower levels contain references. 24 Design patterns, Laura Semini, Università di Pisa, Dipartimento di Informatica.

Deep vs shallow copy 25 Design patterns, Laura Semini, Università di Pisa, Dipartimento di Informatica.

java.lang Class Object protected Object clone() this method creates a new instance of the class of this object and initializes all its fields with exactly the contents of the corresponding fields of this object, as if by assignment the contents of the fields are not themselves cloned. Thus, this method performs a "shallow copy" of this object, not a "deep copy" operation. 26 Design patterns, Laura Semini, Università di Pisa, Dipartimento di Informatica.

java.lang Interface Cloneable A class implements the Cloneable interface to indicate to the Object.clone() method that it is legal for that method to make a field-for-field copy of instances of that class. Invoking Object's clone method on an instance that does not implement the Cloneable interface results in the exception CloneNotSupportedException being thrown. By convention, classes that implement this interface should override Object.clone (which is protected) with a public method. Note that this interface does not contain the clone method. Therefore, it is not possible to clone an object merely by virtue of the fact that it implements this interface. 27 Design patterns, Laura Semini, Università di Pisa, Dipartimento di Informatica.

Point class Point implements Cloneable{ private int x; private int y; } @Override public Point clone() { return (Point)super.clone(); } 28 Design patterns, Laura Semini, Università di Pisa, Dipartimento di Informatica.

Line: shallow copy class Line implements Cloneable { private Point start; private Point end; public Line() { //Careful: This will not happen for the cloned object SomeGlobalRegistry.register(this); } @Override public Line clone() { return (Line)super.clone(); } } 29 Design patterns, Laura Semini, Università di Pisa, Dipartimento di Informatica.

Line: deep copy. @Override public Line clone() { Line line = (Line)super.clone(); //since Point is cloneable. Otherwise we will //have to instantiate and populate it's fields manually line.start = this.start.clone(); line.end = this.end.clone; return line; } 30 Design patterns, Laura Semini, Università di Pisa, Dipartimento di Informatica.

Ex. Animal farm

Prototype Pattern Example code

Prototype Pattern Example code

Prototype Pattern Example code

Prototype Pattern Example code

Prototype Pattern Example code