Domain-Driven Design Activity

Similar documents
The class Object. Lecture CS1122 Summer 2008

Java Persistence API (JPA) Entities

C12a: The Object Superclass and Selected Methods

Activity 11: Designing Classes

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

Not overriding equals

Fall 2017 Mentoring 9: October 23, Min-Heapify This. Level order, bubbling up. Level order, bubbling down. Reverse level order, bubbling up

CSC 1351: Final. The code compiles, but when it runs it throws a ArrayIndexOutOfBoundsException

USAL1J: Java Collections. S. Rosmorduc

java.lang.object: Equality

CSE 373. Objects in Collections: Object; equals; compareto; mutability. slides created by Marty Stepp

1.00/1.001 Introduction to Computers and Engineering Problem Solving. Final Exam

Fundamental Java Methods

INSTRUCTIONS TO CANDIDATES

inside: THE MAGAZINE OF USENIX & SAGE August 2003 volume 28 number 4 PROGRAMMING McCluskey: Working with C# Classes

Advanced Topics on Classes and Objects

COM1020/COM6101: Further Java Programming

Polymorphism. return a.doublevalue() + b.doublevalue();

Canonical Form. No argument constructor Object Equality String representation Cloning Serialization Hashing. Software Engineering

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

CMSC 132: Object-Oriented Programming II. Hash Tables

(b) Draw a hash table having 10 buckets. Label the buckets 0 through 9.

Midterm Exam 2 CS 455, Spring 2015

Chapter 11: Collections and Maps

More about inheritance

Java Classes. Produced by. Introduction to the Java Programming Language. Eamonn de Leastar

Reviewing OO Concepts

Creating an Immutable Class. Based on slides by Prof. Burton Ma

CMSC 132: Object-Oriented Programming II. Effective Java. Department of Computer Science University of Maryland, College Park

Java Fundamentals (II)

CS2110: Software Development Methods. Maps and Sets in Java

Compsci 201 Hashing. Jeff Forbes February 7, /7/18 CompSci 201, Spring 2018, Hashiing

Super-Classes and sub-classes

Reviewing OO Concepts

Principles of Software Construction: Objects, Design and Concurrency. Inheritance, type-checking, and method dispatch. toad

Programming Languages and Techniques (CIS120)

TECHNICAL WHITEPAPER. Performance Evaluation Java Collections Framework. Performance Evaluation Java Collections. Technical Whitepaper.

Solutions to Quiz 1 (March 14, 2016)

Outline. 1 Hashing. 2 Separate-Chaining Symbol Table 2 / 13

Object-Oriented Programming Concepts

(f) Given what we know about linked lists and arrays, when would we choose to use one data structure over the other?

Chair of Software Engineering. Languages in Depth Series: Java Programming. Prof. Dr. Bertrand Meyer. Exercise Session 10

CSE 331 Spring 2018 Midterm

MIT AITI Lecture 18 Collections - Part 1

10 Generating Javadocs; HashMap: Overriding equals

COMP 250. Lecture 32. interfaces. (Comparable, Iterable & Iterator) Nov. 22/23, 2017

JAVA Programming Language Homework II Student ID: Name:

Methods Common to all Classes

Exam 2 CSCI 2600 Principles of Software November 3, 2015

CSC 231 DYNAMIC PROGRAMMING HOMEWORK Find the optimal order, and its optimal cost, for evaluating the products A 1 A 2 A 3 A 4

Overloaded Methods. Sending Messages. Overloaded Constructors. Sending Parameters

Java: advanced object-oriented features

Programming Languages and Techniques (CIS120)

CIT-590 Final Exam. Name: Penn Key (Not ID number): If you write a number above, you will lose 1 point

INSTRUCTIONS TO CANDIDATES

Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism.

11 HashMap: Overriding equals ; JUnit; Vistors

More Java Basics. class Vector { Object[] myarray;... //insert x in the array void insert(object x) {...} Then we can use Vector to hold any objects.

CS 3410 Ch 20 Hash Tables

Programming Languages and Techniques (CIS120e)

Programming II (CS300)

(b) Draw a hash table having 10 buckets. Label the buckets 0 through 9.

Programming Languages and Techniques (CIS120)

For this section, we will implement a class with only non-static features, that represents a rectangle

CS/ENGRD 2110 FALL Lecture 6: Consequence of type, casting; function equals

CSE 331 Final Exam 3/12/12

Introduction to Programming Using Java (98-388)

The Liskov Substitution Principle

CS 310: Hashing Basics and Hash Functions

Distributed Systems Recitation 1. Tamim Jabban

CSE115 Introduction to Computer Science I Coding Exercise #7 Retrospective Fall 2017

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

CSE 331 Software Design & Implementation

Computer Science II (20082) Week 1: Review and Inheritance

Prelim 2. CS 2110, 16 November 2017, 7:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

JAVA WRAPPER CLASSES

Equality (2.1) Advanced Topics on Classes and Objects. Equality (1) Equality (2.2): Common Error

Classes Classes 2 / 35

Programming Languages and Techniques (CIS120)

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

CSE wi Midterm Exam 2/8/18 Sample Solution

Hash tables. hashing -- idea collision resolution. hash function Java hashcode() for HashMap and HashSet big-o time bounds applications

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Programming Languages and Techniques (CIS120)

CSE 331 Final Exam 6/7/16

Distributed Systems Recitation 1. Tamim Jabban

Formal Specification and Verification

ITI Introduction to Computing II

Collections. Powered by Pentalog. by Vlad Costel Ungureanu for Learn Stuff

CS 360 Programming Languages Introduction to Java

Example: Count of Points

CS2110: Software Development Methods. Maps and Sets in Java

Abstract Data Types (ADTs) Queues & Priority Queues. Sets. Dictionaries. Stacks 6/15/2011

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed

STANDARD ADTS Lecture 17 CS2110 Spring 2013

CSE 331 Software Design & Implementation

CSE wi Midterm Exam 2/8/18. Name UW ID #

Expected properties of equality

CSE331 Winter 2014, Midterm Examination February 12, 2014

Transcription:

Domain-Driven Design Activity SWEN-261 Introduction to Software Engineering Department of Software Engineering Rochester Institute of Technology

Entities and Value Objects are special types of objects Normal Java equality semantics are not adequate with dealing with Entities and VOs So, what does this mean equality semantics? Good question! The Java == operator only tests that the two object references are the same. But having the "same location in the heap" is meaningless for these types of objects The following slides explain the equality semantics of both of these object types Starting with Value Objects 2

Value Objects have value semantics. Value Object components represent values in the real world: money, measurements, positions, and so on Value objects must be equal based upon the internal data of the value. For example, a coordinate Position is based upon an x,y pair of integers Value objects must be immutable. Once set in a constructor no attribute may change No mutator methods; ie, no setters 3

Value Objects are equal when their internal data are both equal Let's give an example: 4 public void make_multiple_positions() { Position p1 = new Position(2, 3); Position p2 = new Position(2, 3); if (p1!= p2) { // The two distinct objects have different identities. if (p1.equals(p2)) { // The two value objects are equal. The default behavior is the same as the == operator but that is not appropriate for value objects.

Unfortunately the default equals method uses reference identity. Doh! The solution is easy: override the equals method with this type's equality semantics. public class Position { private int x; private int y; // more code here 5 public boolean equals(object obj) { if (obj == this) return true; if (!(obj instanceof Position)) return false; final Position that = (Position) obj; return this.x == that.x && this.y == that.y; Equality is based upon all attributes.

Entities have identity semantics. Entity components represent things in the real world: people, orders, products, and so on What identifies these types of things? In an Enterprise application the system would store entities in a database. The database assigns a unique ID to each entity object. When you don't have a database you choose an attribute that is unique and unchanging. This is often called a natural key. 6

Provide an id for an Entity class. public class Circle { private String id; private Position center; private int radius; public Circle(String id) { this.id = id; public String getid() { return id; // more code here Let the client of the Circle specify a unique id. public boolean equals(object obj) { if (obj == this) return true; if (!(obj instanceof Circle)) return false; final Circle that = (Circle) obj; return this.id.equals(that.id); 7

So now that we have semantic equality, we need a semantic hash code. In Java there is a close relationship between the equals and hashcode methods. If you override one you must override the other. Use the attributes that make up the equality check when building the hash code. If two objects are "equal" then they must also have the same hash code: x.equals(y) => x.hashcode() == y.hashcode() This is critical when you use objects as keys in a HashMap or stored in HashSet collections. See Java API hashcode docs for explanation. See ProgramCreek blog for another explanation. 8

Value Objects with primitive attributes can calculate its own hash code with simple arithmetic. public class Position { private int x; private int y; // more code here public boolean equals(object obj) { if (obj == this) return true; if (!(obj instanceof Position)) return false; final Position that = (Position) obj; return this.x == that.x && this.y == that.y; public int hashcode() { return x * 31 + y; Java 8 now supplies a helper method: return Objects.hash(x, y); 9

Entities should use the ID to calculate a hash code. public class Circle { private String id; private Position center; private int radius; // more code here public boolean equals(object obj) { if (obj == this) return true; if (!(obj instanceof Circle)) return false; final Circle that = (Circle) obj; return this.id.equals(that.id); public int hashcode() { return id.hashcode(); 10

Your exercise is to build the code for this model. Implement the methods indicated in these two Model classes: 11 Place the two source files into a single zip file and deposit it in the Domain-driven design - individual dropbox.