Flashcards
Chapter 2
Cards in this deck (26)
- 1encapsulation motivation- makes code easier to understand, to change part of it and less error-prone
- encapsulate data and computations to limit the number of contact points between parts of the code
- relates to the principle of information hiding - 2***specific techniques to ensure our code respects these principles of information hiding***1. encoding abstractions as types
2. scope and accessibility
3. escaping references
4. immutability
5. exposing internal data
6. input validation - 3abstraction definitionAn abstraction is a conceptual building block for a software system.
eg data structures (stacks, lists, …) and operations (sorting, iterating…), or ideas (playing cards) - 4enumerated types definition and why use them- a special kind of class declaration. The identifiers
listed in the declaration of the enumerated type are globally available constants
- Use **enumerated types** to represent a value in a collection of a small number of
elements that can be enumerated; - 5Use classes to define how domain concepts are represented in code, as opposed to...encoding instances of these concepts as values of primitive types (an antipattern
called PRIMITIVE OBSESSION†); - 6object definitionan object is a mechanism to group variables together and access their values through the process of dereferencing
- 7scopes are defined...using curly braces
- 8access modifiers are...keywords that control what parts of the code can access certain program elements (e.g., classes, fields,
methods). — public, private (only visible within the scope of the class)
use the narrowest possible scope for class members - 9what is an interface?- “shell” used to hide for encapsulation
- an **interface** to a class consists of the set of its public methods - 10Object diagrams can help...explain or clarify the structure of complex object graphs, or how references are shared;
type of UML diagram that represents objects and how they refer to each other - 11There are three main ways in which a reference to a private structure can escape the scope of its class:returning a reference to an internal object,
storing an external reference internally, or leaking references through shared structures. - 12storing an external reference internally means...for eg having a setter method/a constructor taking as parameter a list given by the client since the reference will already be escaped as soon as it is assigned to the field
- 13There is one situation where leaking a reference to an internal object is harmless:when the object is immutable
- 14Objects are immutable if...their class provides no way to change the internal state
of the object after initialization. These classes are called immutable classes. - 15need a way to show internal data without breaking encapsulation. 2 options:1. **extended interface**: to extend the interface of the class to include access methods that
only return references to immutable objects
2. **returning copies**: to return a different object that has all the same information
as the internal structure we wish to keep encapsulated — need to decide how to deep copy the object graph, or to shallow copy
A common technique for copying objects is to use a **copy constructor**. The idea is to design a constructor that takes as argument an object of the same class, and (usually) to copy matching field values. - 16Input validation can be used to ensure...that the objects of a class are created
and used properly. However, this extra code comes at a cost as it needs to be
documented, tested, and maintained. - 17consequence of input validation:we have shifted the responsibility of the client code from input validation
to error handling. Presumably, if the client code is written so that it is possible
to raise an exception, it should also catch this exception: - 18Use design by contract to...avoid ambiguity in method signatures, and thereby help
prevent the possibility that client code will misuse an instance of a class. - 19main idea of design by contract:for method signatures (and related documentation)
to provide a sort of contract between the client (the caller method) and
the server (the method being called). This contract takes the form of a set of preconditions
and a set of postconditions. - 20how do you specify preconditions and postconditions?preconditions are specified using Java statements in the comments using the Javadoc @pre tag and postconditions are specified using the tag
@post. - 21It is possible to make pre- and postconditions (and any other predicates) checkable in Java using...the assert statement
- 22what does the assert statement do?evaluates its predicate expression and raises an AssertionError if the result is false
- 23design by contract helps prevent...the tedious idiom of defensive programming where corner cases (such as null references) are checked for
everywhere in the code. - 24benefit of design by contract:The technique supports clear blame assignment while debugging: If a precondition check fails, the client (caller method) is to blame. If a postcondition check fails, the actual method being called is to blame. More generally, assert statements are a simple yet powerful tool to increase code quality.
- 25When designing method interfaces, two different design choices:it is important to decide whether the method will be in charge of rejecting illegal values, or whether these
will simply be specified as invalid. - 26AssertionErrors are not meant to...represent the presence of invalid values in a running program. Rather, they point to a design or
implementation flaw in the code.
Information
- Description
- idk Software design Flashcards on Chapter 2, created by Virginie Gagné on 17/04/2022.
- Created by
- Virginie Gagné
- Views
- 1
- Rating