Chapter 4 - Object State Flashcards
static/compile-time perspective
looking at a software system in terms of the software elements declared in the source code and the relations between them
dynamic perspective
- looking at a software system in terms of objects - corresponds to the set of all values and references held by the variables in a program at different points in time
- can be represented by object, state and sequence diagrams
object state
the particular pieces of information the object represents at a given moment
concrete state
collection of values stored in the object’s fields
abstract state
arbitrarily defined subset of the concrete state space
state space
- set of all possible states for a variable/object
- with fields of reference types, the cardinality of the state space increases dramatically
stateless objects
objects that do not store any values (eg. function objects which have no fields besides constants)
state diagrams
- used to represent how objects can transition from one abstract state to another as a reaction to external events (like method calls)
- represent a dynamic view of a software system
TEMPORARY FIELD antipattern
information should not be stored in an object unless it uniquely contributes to the intrinsic value represented by the object
ways to model absent values
- optional types
- NULL OBJECT design pattern
Optional types
- to make a value of type T optional for a variable, declare variable to be of type Optional<T></T>
- to represent absent variable -> Optional.empty()
- to represent a value that can never be null -> Optional.of(value)
- to represent a value which can be null -> Optional.ofNullable(value)
- to get a value wrapped by instance of Optional -> Optional.get()
- to check if a value is null -> !variable.isPresent()
NULL OBJECT design pattern
- relies on polymorphism so only applicable to situations where a type hierarchy is possible
- create a class to represent a NULL object
default methods
methods that have an implementation in the interface which is applicable to instances of all implementing types (don’t need to provide implementation for them in implementing classes but can override them)
final keyword
- prevents changing the value of a field after initialization -> can only be assigned value in declaration of variable or in constructor
- does not make referenced objects immutable
identity
- refers to fact that we are referring to a particular object, represented by object ID
- two different objects with same value will still have different IDs