Defining State of Complex, Immutable, Objects in a Analyzable way

Slides:



Advertisements
Similar presentations
Singleton vs utility class  at first glance, the singleton pattern does not seem to offer any advantages to using a utility class  i.e., a utility class.
Advertisements

Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
CS 106 Introduction to Computer Science I 11 / 09 / 2007 Instructor: Michael Eckmann.
Classes/Objects. Creating Objects Much like creating variables of primitive types –String name; –type name; Object variables hold references, not values.
Chapter 3 Classes and Methods. 2 Knowledge Goals Appreciate the difference between a class in the abstract sense and a class as a Java construct Know.
CS 280 Data Structures Professor John Peterson. Goals Understand “Programming in the small” Java programming Know what’s under the hood in complex libraries.
CS 106 Introduction to Computer Science I 03 / 30 / 2007 Instructor: Michael Eckmann.
Static Code Analysis and Governance Effectively Using Source Code Scanners.
Chapter 4 Objects and Classes.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
Invitation to Computer Science, Java Version, Second Edition.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Copyright © 2002, Systems and Computer Engineering, Carleton University a-JavaReview.ppt * Object-Oriented Software Development Unit.
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
Making Decisions uCode: October Review What are the differences between: o BlueJ o Java Computer objects represent some thing or idea in the real.
The Singleton Pattern SE-2811 Dr. Mark L. Hornick 1.
08 Encapsulation and Abstraction. 2 Contents Defining Abstraction Levels of Abstraction Class as Abstraction Defining a Java Class Instantiating a Class.
GoodOO Programming Practice in Java © Allan C. Milne v
Difficult Specifications in Javari Immutability Inference Jaime Quinonez Program Analysis Group April 20, 2007.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Java Coding 5 – Part 2 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. To object or not…
A Pattern Language for Parallel Programming Beverly Sanders University of Florida.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
UML2-1 UML Exercise A "draw" utility program lets users draw several geometric objects on a diagram. A geometric object may be a Circle, Rectangle, Square.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
XuanTung Hoang 1 Something to discuss Feedbacks on Midterm Exam Final exam and term project  Final exam requires solid knowledge/skills in Java  Be more.
Equality, references and mutability COMPSCI 105 SS 2015 Principles of Computer Science.
Review. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Objects and Classes Objects and Classes –State.
Review for Test2. Scope 8 problems, 60 points. 1 Bonus problem (5 points) Coverage: – Test 1 coverage – Exception Handling, Switch Statement – Array of.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
Copyright © Texas Education Agency, Computer Programming Variables and Data Types.
Memory Management.
CSC 222: Object-Oriented Programming
Classes and Objects.
More Sophisticated Behavior
Computational Thinking, Problem-solving and Programming: General Principals IB Computer Science.
MPCS – Advanced java Programming
CSC 221: Computer Programming I Spring 2010
The Singleton Pattern SE-2811 Dr. Mark L. Hornick.
CSC 221: Computer Programming I Fall 2005
C Basics.
Constructors CMSC 202.
Interfaces and Inheritance
Pointers and References
Abstraction Functions and Representation Invariants
Phil Tayco Slide version 1.0 Created Oct 16, 2017
Ch 4: Writing Classes Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: Classes and Objects.
The method invocation mechanism and the System Stack
Scientific Inquiry Standard B – 1.7.
Chapter 8 Arrays Objectives
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
Recursion Data Structures.
Computer Science Testing.
SWE 619 Software Construction Last Modified, Fall 2015 Paul Ammann
CS100J Lecture 8 Previous Lecture This Lecture Programming Concepts
Creating Objects A variable holds either a primitive value or a reference to an object A class name can be used as a type to declare an object reference.
Topics discussed in this section:
Object Oriented Programming Review
Chapter 8 Arrays Objectives
Interfaces.
Data Structures & Algorithms
Software Engineering and Architecture
Classes and Objects Object Creation
SPL – PS3 C++ Classes.
CS 240 – Advanced Programming Concepts
CMSC 202 Constructors Version 9/10.
String Objects & its Methods
Threads and concurrency / Safety
Presentation transcript:

Defining State of Complex, Immutable, Objects in a Analyzable way “Bloch’s” Builder Defining State of Complex, Immutable, Objects in a Analyzable way

Problem We need to define PersonIdentity objects Representing Names, SSN (CPR), phone numbers, gender, date of birth, address, title, … Moreover, need to define partial knowledge I.e. ‘Per Christensen’ but we do not know date of birth, phone number, etc. And potential with complex constraints Ex. Assign title ‘Mr.’ to male, ‘Miss’ to female, in case no title is assigned And they are immutable objects Objects with complex state to be set

Problem The solution is well known: Constructor parameters…

Usage… Defining John and Ann… Exercise: Liabilities ??? Lots of strings, what if I exchange two of them. (Mathilde birth example! A programmer can annoy thousands of peopl in the blink of an eye). Weird string arrays. No overview of what goes where. Lots of nulls for incomplete data.

Exercise An alternative would be PersonIdentity pi = new PersonIdentity(); pi.setFamilyName(”Hansen”); pi.addGivenName(”Kaj”); pi.setGender(Gender.Male); … Why is this not an acceptable solution? Trick question: It is because I stated that I wanted an Immutable object; one whose state cannot be changed. Setters allows – well – setting the state.

Bloch’s Builder / Effective Java Alternative Construct a builder object, using its setter methods, and finally ask it to build the PersonIdentity

Bloch’s Builder / Effective Java The internal Builder

Bloch’s Builder / Effective Java The build() method Depends on private constructor

The Two-Phase Process The algorithm You create the internal builder object You set all state, using its setters, in temporary state variables, in the internal builder You invoke its build() Which can Verify constraints between state variables Set/alter additional state as defined by requirements Compute the final state space, and… … that invokes the final object’s constructor That copies from the builder’s state into its own state variables

Analysis Benefits Liabilities Improved analyzability of complex object construction (fluent API) Improved reliability (lower probability of wrong coding) Improved support for partial object construction Improved support for constraint checking Liabilities Complex coding of the internal builder Intermediate state objects Lots of named setters Private constructor in the outer/final object Conclusion: Careful evaluate benefit/liabilities

Why do you need to know? This builder is increasingly seen in open source libraries Example: Unirest asJson() builds the http request and executes it Logging frameworks, Mock frameworks, …

Relation to GoF Builder Bloch states that this pattern is equal to GoF’s Builder pattern I do not really agree… The Intent is different. Builder intent: ”… so the same construction process can create different representations” Bloch’s builder intent is to create single representation but solves faced with many constructor parameters

Reference Great book 