Observers Flashcards

1
Q

What is the primary purpose of the Observer pattern?

  1. To create multiple instances of the same class
  2. To allow objects to be notified of changes in another object
  3. To enforce a strict class hierarchy
  4. To control the instantiation of a single object
A
  1. To allow objects to be notified of changes in another object

(Förklaring: Observer-mönstret tillåter objekt att bli notifierade om förändringar i andra objekt utan att vara direkt beroende av dem.)

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

In an Observer pattern, what term describes the object that sends out notifications?

  1. Listener
  2. Observer
  3. Subject
  4. Controller
A
  1. Subject

(Förklaring: Subjektet är det objekt som observers observerar och som notifierar när en förändring sker).

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

Which of the following best describes a key feature of the Observer pattern?

  1. It requires close coupling between objects
  2. It allows an object to notify other objects without knowing their details
  3. It prevents objects from being notified
  4. It centralizes all logic in one place
A
  1. It allows an object to notify other objects without knowing their details

(Förklaring: Observer-mönstret främjar lös koppling, vilket betyder att subjektet inte behöver känna till detaljer om sina observers).

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

What type of dependency does the Observer pattern establish between objects?

  1. One-to-one
  2. One-to-many
  3. Many-to-many
  4. One-to-none
A
  1. One-to-many

(Förklaring: Observer-mönstret skapar ett ett-till-många beroende där ett subjekt notifierar flera observers).

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

What benefit does the Observer pattern provide in terms of code structure?

  1. It minimizes the number of classes needed
  2. It improves code readability by enforcing direct method calls
  3. It supports loose coupling between objects
  4. It restricts objects to a fixed set of actions
A
  1. It supports loose coupling between objects

(Förklaring: Observer-mönstret skapar lös koppling mellan objekt, vilket gör koden lättare att underhålla och förändra).

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

In the Observer pattern, what is the main role of an observer?

  1. To monitor changes and take action when notified
  2. To initiate updates in other objects
  3. To prevent data from being shared
  4. To handle object creation
A
  1. To monitor changes and take action when notified

(Förklaring: En observer övervakar förändringar i ett annat objekt och agerar när notifieringar tas emot).

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

How does the Observer pattern improve flexibility in a software system?

  1. By tightly linking observers to the subject
  2. By allowing changes in the subject to be independently handled by observers
  3. By restricting changes to a single observer
  4. By making all updates mandatory for all objects
A
  1. By allowing changes in the subject to be independently handled by observers

(Förklaring: Observer-mönstret gör att observers kan hantera förändringar självständigt utan att subjektet behöver veta något om dem).

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

When is the Observer pattern particularly useful?

  1. When one object needs to inform several others about a change
  2. When only one object needs to know about all changes
  3. When no object dependencies are allowed
  4. When changes happen at compile time only
A
  1. When one object needs to inform several others about a change

(Förklaring: Observer-mönstret är användbart när ett objekt behöver notifiera flera andra om en förändring, t.ex. i ett GUI).

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

In a typical Observer pattern setup, how does the subject communicate with its observers?

  1. By directly invoking methods on each observer
  2. By notifying all observers through a publish-subscribe mechanism
  3. By sending data via constructors
  4. By creating new instances of each observer
A
  1. By notifying all observers through a publish-subscribe mechanism

(Förklaring: Genom en publicera-prenumerera-mekanism kan subjektet skicka ut notifieringar till alla observers).

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

What common issue does the Observer pattern help solve in event-driven applications?

  1. Ensuring all observers have identical states
  2. Automatically synchronizing state across multiple objects
  3. Managing single-instance restrictions in objects
  4. Preventing objects from interacting with each other
A
  1. Automatically synchronizing state across multiple objects

(Förklaring: Observer-mönstret hjälper till att synkronisera tillstånd mellan flera objekt automatiskt, vilket gör att användargränssnittet kan reagera på förändringar).

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

What is the main purpose of an event in C#?

  1. To create a new object
  2. To store data
  3. To allow an object to notify other objects of changes
  4. To create a thread
A
  1. To allow an object to notify other objects of changes

(Förklaring: Events tillåter ett objekt att notifiera andra objekt när något inträffar, t.ex. en användarinteraktion eller en datauppdatering).

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

What method is called to notify all observers in the Observer pattern?

  1. NotifyAll
  2. Broadcast
  3. Update
  4. Push
A
  1. Update

(Förklaring: Update-metoden anropas för att informera alla observers när en förändring sker i subjektet).

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

What is required for an observer to be notified in C# events?

  1. The observer must implement an event handler
  2. The observer must inherit from a delegate
  3. The observer must use the “update” keyword
  4. The observer must override the event
A
  1. The observer must implement an event handler

(Förklaring: En observer måste implementera en handler för att kunna reagera på notifieringar).

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