Chapter 4: Creating Flexible Interfaces Flashcards

1
Q

What is an interface?

A

An interface is the means objects use to talk. Design deals not only with what objects an object knows (its responsibilities) and who the object knows (its dependencies) but also how it talks to another.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Mention two concepts on how we can uses an interface

A

It can refer to:

  • The methods within a class and their public or private exposition
  • The one that spans across classes and that is independent of any single class. In this sense an interface is a set of methods that define it.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the features of the public interface of a class?

A
  • Reveal its primary responsibility
  • Are expected to be invoked by others
  • Will not change in a whin
  • Are safe for others to depend on
  • Are thoroughly documented in the tests
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the features of the private interface of a class?

A
  • Handle implementation details
  • Are not expected to be sent by other objects
  • Can change for any reason whatsoever
  • Are unsafe for others to depend on
  • May not even be referenced in the tests
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a public interface within the class?

A

It is a contract that articulates the resposibilities of the class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the relationship between Single Responsibility approach and the public interface?

A

Methods in the public interface of a class should read like a description of responsibilities.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the relationship between Managing Dependencies and the class’ interface?

A

The idea of depends on classes that change less than yours do can be applied to the methods in a class. Methods in the public interface are stable and private ones have more likelihood of change.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are Domain Objects?

A

They are classes that are easily identified in the requirements because they are nouns in the application that have both data and behavior. They stand for big, visible real-world things.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Why message-based perspective is better than class-based ones?

A

Message-based perspective yield more flexible applications than class-based perspective.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Why the sequence diagram is suitable for the message-based design?

A

The sequence diagram allows checking the interaction between objects and the messages passed between them.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Why is it important to ask for “What” instead of telling “How”?

A

The “What” allows to create reusable classes with well-defined public interfaces.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the context of a class?

A

The things a class know about other objects make up its context.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Why does a context is important?

A

The context that an object expect has a direct effect on how difficult it is to reuse. Object with a simple context are easy to use and test.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does seeking context independence means?

A

It means creating objects with a simple context where they aren’t holding onto other objects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does trusting other objects means?

A

It means thinking about what instead of how. When we trust another object will do its work, we can just ask (the what) the things we want and the context will be much simplier.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the purpose of a sequence diagram?

A

Act as the vehicle for exposing, experiment with, and ultimately defining the public interfaces.

17
Q

What is the focus of the message-based approach?

A

It is to answer the question: “I need to send this message, who should respond to it?”

18
Q

What are Private, Protected and Public key words?

A

They are access modifiers

19
Q

How does the Private keyword work?

A

It denotes the least stable kind of method and provides the most restricted visibility.
Private methods must be called with an implicit receiver (that is ‘self’) or, inversely, may never be called with an explicit receiver.

20
Q

In what cases a private method can be sent?

A

If we have a private method called fun_factor in the Trip class, we can send:
- fun_factor, defaulting to self (the implicit receiver) from within instances of Trip and its subclasses
But we may not send:
- self.fun_factor from within Trip
- a_trip.fun_factor from another object

21
Q

How does the Protected keyword work?

A

It also indicates an unstable method like private ones but with slightly different visibility restrictions. Protected methods allow explicit receivers as long as the receiver is self or an instance of the same class or subclass of self

22
Q

In what cases a protected method can be sent?

A

If we have a protected method called fun_factor in the Trip class, we can send:

  • self.fun_factor
  • a_trip.fun_factor, but only from within a class where self is the same kind of thing (class or subclass) as a_trip
23
Q

What do you do if a class doesn’t offert a public interface or an ill-defined one?

A

We must strive for minimize the context our interfaces require from others. We can always create our own public interfaces if other classes don’t provide one or they are ill-defined.

24
Q

What is the Law of Demeter?

A

It is a set of coding rules that results in loosely coupled objects.

25
Q

When are we violating Demeter?

A

Demeter restricts the set of objects to which a method may send messages; it prohibits routing a message to a third object via a second one of a different type.
When we have a long chaining list of methods (know as train wrecks) we might be violating Demeter.

26
Q

How do we avoid the train wrecks Demeter violation?

A

We can use Delegation but beware, using delegation to hide tight coupling is not the same as decopling de code.

27
Q

Why does a train wrecks represent a Demeter violation?

A

Because we are are trying to get a distant behavior through the chain of objects.