Flashcards
Chapter 4

Flashcards do conjunto (37)
- 1dynamic perspective definition what diagrams to use?The dynamic perspective corresponds to the set of all values and references held by the variables in a program at different points
in time. It is what we see in a debugger while stepping through the execution of the code.
rely on object diagrams, state diagrams and sequence diagrams - 2object state definition concrete vs abstract state stateless vs stateful objectsInformally, the state of an object refers to the particular pieces
of information the object represents at a given moment.
The concrete state of an object is the collection of values stored in the object’s fields. Easier to think in terms of abstract state since state space can be veeeery big.
abstract states are arbitrary partitions of the state space, they can really be defined as anything
some objects do not store any values: stateless objects - 3state space definitionthe set of possible states for a variable or object
As soon as objects have fields of reference types, the cardinality
(or size) of the state space explodes dramatically - 4state diagram are useful for what?useful to represent how objects can, during their lifetime,
transition from one abstract state to another as a reaction to external events
(typically, method calls). They represent a dynamic view of a software system. - 5benefits of state diagramsthey allow us to self-evaluate the
impact of design decisions on the complexity of the abstract state space that must be
considered when working with an object.
they support systematically exploring the abstract behavior of an object of a given class - 6a sign that state diagram does not represent a good model of the state space:If the names assigned to states include verbs or generally feel like they are describing actions (for example,
draw card) - 7object life-cycle definitionThe state diagram model of an object can also be referred
to as its life cycle, because it describes the “life” of an object, from its initialization
to its abandonment and eventual destruction by the garbage collector - 8state a good design principle concerning objects life-cyclesA good design principle
to avoid objects with complex life cycles is thus to minimize the state space of objects
to what is absolutely necessary for the object to conduct its business. - 9design antipattern concerning invalid and useless statesIn some cases, eliminating some states from the life cycle of an object may seem like reducing the versatility
of a class (what if we need this one day?). This is an argument that can be made in
some situations. However, it is always important to consider the cost (in terms of
software developer time spent understanding code, writing tests, and fixing bugs).
Often, this kind of SPECULATIVE GENERALITY† is not worth the cost. - 10another common bad practice when designing objects -> design antipatternto cache stateful information
in instance variables (fields), typically for convenience, presumed performance efficiency,
or both.
e.g. put size as a field in Deck and decrease de field when we draw.
to the extent possible, information should not be stored in an object unless it uniquely contributes to the intrinsic value represented by the object. → TEMPORARY FIELD† antipattern - 11motivation for nullabilityin many situations, the domain concept we are trying to model will require that we
make a provision for the fact that there may not be a value. - 12optional types how to make a value optional? how to represent the absence of a value? how to create an instance of Optional that represents an actual value? how to get the value wrapped by an instance of Optionalsupported by the Optional library class declare this variable to be of type Optional.
Optional.empty()
Optional.of(value) if value is not (ever) expected to be null, and Optional.ofNullable(value) if value can be null (in which case Optional.empty() will be stored instead)
call get(). - 13consequence of using optional types and solutionthe two fields no longer have the types likely to be desired by the client
code. While the client will probably be interested in working with values of type
Rank and Suit, the fields of the class now store values of type Optional
and Optional.
Unwrap the optional within getRank() and getSuit() - 14second solution for avoiding the use of null references to represent absent values, which avoids the issue of unpacking wrapper objects- solution
uses a special object to represent the null value. For this reason, this idea is called
the **NULL OBJECT design pattern.**
- relies on polymorphism, so it is only applicable to situations where a type hierarchy
is available. - 15NULL OBJECT definitionThe main idea of NULL OBJECT is to create one special object to represent the absent value, and to test for absence using a polymorphic method call.
- 16null object class diagram

- 17code for creating a null objectpublic interface CardSource {
CardSource NULL = new CardSource() {
public boolean isEmpty() {
return true;
}
public Card draw() {
assert !isEmpty();
return null;
}
public boolean isNull() {
return true;
}
}; - 18final fields and variables -- motivationBecause object state is just an abstraction of the combination of values taken by the fields of
an object, the way to realize the principle in practice is to limit the number of ways
in which the field values can be updated. - 19why use final keyword, def exception?prevent changing the value of a field after initialization, so that the value of the field
remains constant
the fields can be assigned a value only once, either in the initializer part of
their declaration, or directly in the constructor. Else, leads to a compilation error.
reference types - 20object identity definitionIdentity refers to the fact that we are referring to a particular object, even if this object is not in a variable. In terms of programming environments, the identity of an object usually refers to its “memory location”, or “reference/pointer to”.
- 21object equalityIn the general case, equality between two objects must be programmer-defined because the meaning of equality cannot always be inferred from the design of the object’s class.
- 22how to specify equality?by overriding the equals(Object) method of the Object class. The default implementation of the equals method defines equality as identity.
A crucial constraint when overriding equals is that any class that overrides
equals must also override hashCode so that the following requirement is respected - 23object uniquenessObjects of a class are
unique if it is not possible for two distinct objects to be equal. If the objects of a class
can be guaranteed to be unique, then we no longer need to define equality, because
in this specific case, equality become equivalent to identity and we can compare
objects using the == operator. - 24FLYWEIGHT pattern motivationprovides a way to cleanly manage collections of low-level
immutable objects. Although sometimes used to address performance concerns, FLYWEIGHT
is also valuable to ensure the uniqueness of objects of a class. - 25context for using flyweightwhen instances of a class are heavily shared in a software system
- 26flyweight ideamanage the creation
of objects of a certain class, called the flyweight class. Instances of the flyweight
class are called flyweight objects. The crucial aspect of the FLYWEIGHT is to control
the creation of flyweight objects through an access method that ensures that no duplicate
objects (distinct but equal) ever exist. - 27The three main components necessary to realize flyweight idea1. A **private constructor** for the flyweight class, so clients cannot control the creation
of objects of the class;
2. A static **flyweight store** that keeps a collection of flyweight objects;
3. A **static access method** that returns the unique flyweight object that **corresponds**
to **some identification key**. The access method typically checks whether the requested
flyweight object already exists in the store, creates it if it does not already
exist, and returns the unique object. - 28The combination of the flyweight store and corresponding access method is sometimes referred to as...the flyweight factory
- 29concern when implementing flyweightwhether to
pre-initialize the flyweight store, or whether to do this lazily, by creating objects as
they are requested through the access method. The answer is context-dependent. - 30when is it most convenient to use flyweight?when used to manage immutable flyweight objects. In any case, it is crucial to ensure that the portion of the
flyweight objects’ state that defines the objects’ identity cannot be mutated. - 31SINGLETON pattern motivation and contextprovides a way to ensure that there is only one instance
of a given class at any point in the execution of the code. The context for
this design pattern is the need to manage an instance that holds, in one place, a cohesive
amount of information that different parts of the code need. - 32three elements involved in singleton solution template1. A private constructor for the singleton class, so clients cannot create multiple
objects;
2. A global variable for holding a reference to the single instance of the singleton
object.
3. An accessor method, usually called instance(), that returns the singleton instance.
The accessor method is optional, because it is also possible to implement
the pattern by declaring the global instance to be a public constant. - 33difference between flyweight and singletonThe SINGLETON pattern differs from FLYWEIGHT in that it attempts to guarantee
that there is a single instance of a class, as opposed to unique instances of a class.
Singleton objects are typically stateful and mutable, whereas flyweight objects are
preferably immutable. - 34typical mistake with singletonto store a reference
to an instance of the class in a static field called INSTANCE or something like it,
without taking proper care to prevent client code from independently creating new
objects. - 35A particular state management concern is introduced by the use of nested classes in Java. Nested classes can be divided into two broad categories...static nested classes
and inner classes. In turn, there are two categories of inner classes: anonymous
classes and local classes. - 36inner class definitioninner classes are declared within another class, and
used to provide additional behavior that involves an instance of the enclosing class,
but which for some reason we do not want to integrate into the enclosing class. - 37what does the compiler do when it creates the definition of the anonymous class?it also (invisibly)
adds fields to the anonymous class, and copies references to each of the
local variables referenced in the code of the anonymous class’s method into a field.
Thus, once an object of the anonymous class is created, the references to the local
variables are now stored in fields of the same name in the anonymous class.
To prevent unexpected behavior, Java prevents referencing external
variables that are reassigned within anonymous classes
Informações
- Descrição
- idk Software design FlashCards sobre Chapter 4, criado por Virginie Gagné em 16-10-2022.
- Criado por
- Virginie Gagné
- Visualizações
- 0
- Avaliação