From UML to Java and return (object-oriented modeling II)

Similar documents
An introduction to Java II

Bruce Eckel, Thinking in Patterns with Java, cf. José Valente de Oliveira 10-1

w3.ualg.pt/~jvo/poo

Interfaces Java. Overview. n Java interfaces. q Introduction. q Sintaxe. q UML notation. q Multi-inheritance of interfaces

Design Patterns. Gunnar Gotshalks A4-1

Java Classes & Primitive Types

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

Lecture 13: Design Patterns

Pattern Resources. Lecture 25: Design Patterns. What are Patterns? Design Patterns. Pattern Languages of Programming. The Portland Pattern Repository

Goals of Lecture. Lecture 27: OO Design Patterns. Pattern Resources. Design Patterns. Cover OO Design Patterns. Pattern Languages of Programming

CHAPTER 6: CREATIONAL DESIGN PATTERNS

Java Classes & Primitive Types

JCF: user defined collections

Design Patterns. An introduction

CHAPTER 6: CREATIONAL DESIGN PATTERNS

More on Design. CSCI 5828: Foundations of Software Engineering Lecture 23 Kenneth M. Anderson

26.1 Introduction Programming Preliminaries... 2

Crash course on design patterns

be used for more than one use case (for instance, for use cases Create User and Delete User, one can have one UserController, instead of two separate

Patterns. Erich Gamma Richard Helm Ralph Johnson John Vlissides

Object Oriented Programming. Michał Bereta

CSC7203 : Advanced Object Oriented Development. J Paul Gibson, D311. Design Patterns

Classes and Objects. Again: Distance between points within the first quadrant. José Valente de Oliveira 4-1

James Newkirk

Advanced Object Oriented PHP

Programmazione. Prof. Marco Bertini

Programming Languages and Techniques (CIS120)

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

Applying Design Patterns to accelerate development of reusable, configurable and portable UVCs. Accellera Systems Initiative 1

Introducing Design Patterns

Towards a Java Framework for Knowledge Representation and Inference

17. GRASP: Designing Objects with Responsibilities

PATTERNS AND SOFTWARE DESIGN

Responsibilities. Using several specific design principles to guide OO design decisions.

Chapter 10: Performance Patterns

Design Patterns. Definition of a Design Pattern

Bugsquashing: Command - Pattern. Andreas Fetzer

Patterns of learning

Tackling Design Patterns Chapter 3: Template Method design pattern and Public Inheritance. 3.1 Introduction... 2

CPSC 310 Software Engineering. Lecture 11. Design Patterns

C++ INTERFACE CLASSES STRENGTHENING ENCAPSULATION

6.3 Patterns. Definition: Design Patterns

Object Oriented Methods with UML. Introduction to Design Patterns- Lecture 8

Outline. Object Oriented Programming. Course goals. Staff. Course resources. Assignments. Course organization Introduction Java overview Autumn 2003

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

Design Patterns For Object Oriented Software Development Acm Press

Model-View-Controller

JOURNAL OF OBJECT TECHNOLOGY Online at Published by ETH Zurich, Chair of Software Engineering. JOT, 2002

Object-Oriented Software Development Goal and Scope

Object-Oriented Design

As a programmer, you know how easy it can be to get lost in the details

Software Engineering - I An Introduction to Software Construction Techniques for Industrial Strength Software

Programming Languages and Techniques (CIS120)

Chapter 12 (revised by JAS)

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

Design Patterns. CSC207 Fall 2017

The Object Recursion Pattern

SSJ User s Guide. Package stat Tools for Collecting Statistics. Version: December 21, 2006

Outline. Logistics. Logistics. Principles of Software (CSCI 2600) Spring Logistics csci2600/


Learning patterns of application architecture by looking at code

Coordination Patterns

Programming Languages and Techniques (CIS120)

Facade and Adapter. Comp-303 : Programming Techniques Lecture 19. Alexandre Denault Computer Science McGill University Winter 2004

Software Design And Modeling BE 2015 (w. e. f Academic Year )

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

ADAPTER. Topics. Presented By: Mallampati Bhava Chaitanya

Creating an object Instance variables

Using Design Patterns in Java Application Development

ITI Introduction to Computing II

Computer Science II. OO Programming Classes Scott C Johnson Rochester Institute of Technology

CMSC 202H. Classes and Objects: Reusing Classes with Composition

Design Patterns. CSC207 Fall 2017

ITI Introduction to Computing II

Outline. Design Patterns. Observer Pattern. Definitions & Classifications

6.170 Lecture 15 Design Patterns

Using a Declarative Chain of Responsibility Pattern to Write Robust, Self- Correcting Distributed Applications

Automating Regression Testing of Java Programs the JSnoopy Way

GRASP Design Patterns A.A. 2018/2019

25.1 Introduction Façade Design Pattern Identification Problem Structure Participants...

Tuesday, October 4. Announcements

Introduction to Inheritance

DOWNLOAD OR READ : OBJECT ORIENTED CONCEPT INTERVIEW QUESTIONS ANSWERS PDF EBOOK EPUB MOBI

Introduction To Design Patterns

Building Java Programs

Design Patterns: Part 2

WS01/02 - Design Pattern and Software Architecture

A Proposal For Classifying Tangled Code

Applying the Observer Design Pattern

Programming Languages and Techniques (CIS120)

Design Patterns (DP) In the beginning. It s not a course about DP (just a little) A lot of good design and efficient implementation is based on DP

Programming Languages and Techniques (CIS120)

Design Patterns. CSC207 Winter 2017

Review Software Engineering October, 7, Adrian Iftene

Patterns for polymorphic operations

Alternator. An Object Behavioral Design Pattern. John Liebenau

What is Design Patterns?

Design patterns. OOD Lecture 6

Node. Node getleft() Returns the left Node child of this Node (might be null)

Programming Languages and Techniques (CIS120)

Transcription:

From UML to Java and return (object-oriented modeling II) Bruce Eckel, Thinking in Java, 4th edition, PrenticeHall, New Jersey, cf. http://mindview.net/books/tij4 jvo@ualg.pt José Valente de Oliveira 6-1 Previously on OOP Problem: given a square, generate the tangential inner circumference UML Ponto Circunferência Quadrado 1

What class should be responsible for a given task? Q1: What should be the class responsible for checking whether two points really define a square? Re: Square; it is its class invariant. Q2: What should be the class responsible for computing the distance between two points? Re: Point. Why? Q3: What should be the class responsible for computing the center and radius of the circunference, given a square? Re: Two hypothesis: Square or Circunference. In general, we decide in favour of the class that have all the required information to solve the task. In this case, we decide by Square. jvo@ualg.pt José Valente de Oliveira 3-3 An initial class diagram Quadrado lado origem validapontos geracircunferencia Ponto x y soma Circunferencia centro raio 2

From class diagram to code Quadrado lado validapontos geracircunferencia origem Ponto x y soma centro Circunferencia raio A possible client Call method String tostring() from class Circunferencia 3

class Circunferencia public class Circunferencia { private Ponto centro; private float raio; public Circunferencia(Ponto c, float r) { if (r<=0) { System.err.println("ERRO: Circunferencia com raio negativo."); System.exit(1); centro=c; raio=r; public String tostring() { return centro.tostring() + " " + raio; Class Quadrado - Constructor public class Quadrado { private Ponto origem; private int lado; public Quadrado (Ponto A, Ponto B) { if (validapontos(a, B)== false) { System.err.println("Erro: os pontos "+ A + " e " + B + " nao definem um quadrado"); System.exit(1); int x = Math.min(A.getX(), B.getX()); int y = Math.min(A.getY(), B.getY()); origem = new Ponto(x, y); lado = Math.abs(A.getX()-B.getX()); public boolean validapontos(ponto A, Ponto B) { int lado1 = Math.abs(A.getX()-B.getX()); int lado2 = Math.abs(A.getY()-B.getY()); return lado1 == lado2; origem 4

Class Quadrado conclusion public class Quadrado { // public Circunferencia geracircunferencia() { float raio = lado / 2.0f; Ponto centro = origem.soma(new Ponto((int) raio, (int) raio)); Circunferencia c = new Circunferencia (centro, raio); return c; The previous class Ponto, with minor changes class Ponto { private int _x_, _y_; public Ponto() { _x_ = 0; _y_ = 0; public Ponto(int x, int y) { setx(x); sety(y); public int getx() { return _x_; public int gety() { return _y_; public void setx(int x) { assert x>0; _x_ = x; public void sety(int y) { assert y>0; _y_ = y; public double dist(ponto that) { int dx = this.getx() - that.getx(); int dy = this.gety() - that.gety(); return Math.sqrt(dx*dx+dy*dy); 5

Refining class Ponto class Ponto { // public Ponto soma(ponto that) { int x = this.getx() + that.getx(); int y = this.gety() + that.gety(); return new Ponto(x, y); Ponto A = new Ponto (1, 2); Ponto B = new Ponto (2, 0); Ponto C = A.soma(B); System.out.println(C); // (3, 2) public String tostring() { return "("+getx() + "," + gety()+")"; Documenting the code with an UML generator jvo@ualg.pt http://www.objectaid.com/ 3-12 6

The initial client What changes are needed to run this new client code? 7

Changes version 1.0 Quadrado q = new Quadrado (P1, P2); public class Circunferencia { //Circunferencia c = q.geracircunferencia (q); private Ponto centro; Circunferencia c = new Circunferencia (q); private float raio; // public Circunferencia (Quadrado q) { raio = q.getlado() / 2.0f; centro = q.getorigem().soma (new Ponto((int) raio, (int) raio)); public class Quadrado { private Ponto origem; private int lado; // public int getlado () { return this.lado; public Ponto getorigem () { return this.origem; Changes version 2.0 Quadrado q = new Quadrado (P1, P2); public class Circunferencia { //Circunferencia c = q.geracircunferencia (q); private Ponto centro; Circunferencia c = new Circunferencia (q); private float raio; // public Circunferencia (Quadrado q) { Circunferencia c = q. geracircunferencia (); centro = c.getcentro(); raio = c.getraio(); 8

Object diagrams jvo@ualg.pt José Valente de Oliveira 3-17 Object Diagrams Several ways of representing objects in UML: write only the class name preceded by a colon and underlined : Student write the name of specific object with it s class onestudent : Student multiple objects : : Student 9

A possible object diagram (A class diagram) The object diagram for the given problem instance jvo@ualg.pt José Valente de Oliveira 3-19 Object Diagram Captures instances and associations at a given point in execution time 10

Class Diagram Where are we in OOM? Modeling and UML, an Introduction Classes and relationship between classes: association, composition, and agregation Introduction to UML class and object diagrams A complete example Notion of design pattern The Expert pattern jvo@ualg.pt José Valente de Oliveira 3-22 11

Patterns: a first notion Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides, Design Pattern Elements of Reusable Object-Oriented Software, Addison-Wesley, 1995 jvo@ualg.pt José Valente de Oliveira 3-23 Pattern Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice, (Christopher Alexander et al, A Pattern Language: Towns, Buildings, Construction, 1977) jvo@ualg.pt José Valente de Oliveira 3-24 12

Pattern Pattern is a named and well-known problem/solution pair that can be applied in new contexts, with advice on how to apply it in novel situations and discussion of its tradeoffs, implementations, variations, and so forth. (Craig Larman, Applying UML and Patterns, 1995) jvo@ualg.pt José Valente de Oliveira 3-25 The Expert pattern (padrão Especialista) Name: Problem: Solution: (advice) Expert What is a basic principle by which to assign responsibilities to objects? Assign a responsibility to the class that has the information needed to fulfill it. jvo@ualg.pt José Valente de Oliveira 3-26 13

The Expert pattern (padrão Especialista) Discussão: O Expert leva-nos habitualmente a programas onde o objeto no programa tem o comportamento que é normalmente encontrado no objeto do mundo real. O Expert promove o encapsulamento, uma vez que o objeto usa os seus próprios dados para realizar as suas tarefas. jvo@ualg.pt José Valente de Oliveira 3-27 Design patterns A design pattern captures design expertise abstracted from existing design examples Design patterns allow to reuse design expertise Design patterns allow one to study how experts do design jvo@ualg.pt José Valente de Oliveira 3-28 14