1/29
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
Encapsulation
To hide the internal implementation of an abstraction behind an interface that tightly controls how an abstraction can be used
What is the principle of Information Hiding (encapsulation context)?
Means that our encapsulated structures should reveal the least amount of information as possible while still being able to be used as intended
Immutability
The inability for an item to ‘mutate’, or change
Modular Programming
The general programming concept of separating a program into distinct pieces or ‘modules’
Escaping Reference
Concept of private members being publicly available outside the scope of the object
Rule of Narrowest Scope
A general rule of the thumb that relates to giving a field or method the minimum level of visibility required for a program to work as intended
What access modifier should instance variables generally be set to?
Private
What access modifier should methods in an API generally be set to?
Public
What access modifier should helper methods used only within a class be set to?
Private
A class is well encapsulated when it is impossible to change data stored by an object without _________________.
Going through one of its methods

Where is the escaping reference in this code?
The setCards(List<Card> pCards> function allows for an externally defined List of cards to be set as the private member of the Deck class, which is an escaping reference because the client code has prior access to this reference

Where is the escaping reference in this code?
The constructor allows a user-created List<Card> object to be instantiated as the private aCards field of the Deck class, which is an escaping reference

Where is the escaping reference in this code?
This one is tricky: notice that the function asks for a List of Lists, and the method is adding a List private member to this user-supplied List of Lists. Because the List of Lists has direct access to its elements, this is an escaping reference
What are the 2 primary tools for achieving encapsulation and avoiding escaped references?
Accessibility modifiers and immutability
How do immutable objects help achieve encapsulation?
Immutable objects neutralize the threat of escaping references, because they can not be modified anyways

What is the issue with this code?
The constructor doesn’t stop null values from being passed in as parameters and setting the aRank and aSuit members to null as a result

What is the issue with this code?
The draw() function does not check if the deck is empty before attempting to remove a card from the list

What is the issue with this code?
The getCards() methods returns a reference to the private member variable aCards, which is an escaping reference

What is the issue with this code?
main is able to access the List<Card> aCards private member variable of the Deck class, which is an escaping reference

What is the issue with this code?
The setCards() function allows client code to supply a List of Card objects as the argument, which is then set to the private member aCards, which is an escaping reference

What is the issue with this code?
A private member variable is added to a client-code supplied List of Card Lists, which is technically an escaping reference since the user has access to the Lists.

What is the issue with this code?
In the 4th line the cards List is assigned to the return value of allCards.get(0). This return value is the private data member of the ‘deck’ object created in line 3, which is an escaping reference

Is this class immutable?
Yes, because the fields are set in the constructor (no setters), and cannot be reset
Why is only being able to modify an object through its methods a good idea, besides for encapsulation?
To ensure the object is used correctly, so that client code can not corrupt the fields and/or set them to invalid values

Can the fields here be corrupted?
Yes, the constructor does not check for null values
What does the unmodifiableList provide?
Returns an unmodifiable view of the list, that does not support operations like adding, removing, or updating elements
What is the idea behind Design by Contract?
The idea that code will produce the right post-conditions if the right pre-conditions are met
What does the assert statement do?
Assert is a Java mechanism for enforcing that code is not used without pre-conditions being met (literally - will raise an AssertionError otherwise)
What are javadocs and what are their use?
Javadocs are a documentation of what the pre-conditions of code are. It is a very lightweight way to Design by Contract, as it does not actually check if the pre-conditions are met but assumes that client code is responsible for using the code correctly.

What is the issue with this code?
Both javadocs and explicit input validation are being used - only one or the other should be used individually