Observers Flashcards
What is the primary purpose of the Observer pattern?
- To create multiple instances of the same class
- To allow objects to be notified of changes in another object
- To enforce a strict class hierarchy
- To control the instantiation of a single object
- 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.)
In an Observer pattern, what term describes the object that sends out notifications?
- Listener
- Observer
- Subject
- Controller
- Subject
(Förklaring: Subjektet är det objekt som observers observerar och som notifierar när en förändring sker).
Which of the following best describes a key feature of the Observer pattern?
- It requires close coupling between objects
- It allows an object to notify other objects without knowing their details
- It prevents objects from being notified
- It centralizes all logic in one place
- 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).
What type of dependency does the Observer pattern establish between objects?
- One-to-one
- One-to-many
- Many-to-many
- One-to-none
- One-to-many
(Förklaring: Observer-mönstret skapar ett ett-till-många beroende där ett subjekt notifierar flera observers).
What benefit does the Observer pattern provide in terms of code structure?
- It minimizes the number of classes needed
- It improves code readability by enforcing direct method calls
- It supports loose coupling between objects
- It restricts objects to a fixed set of actions
- 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).
In the Observer pattern, what is the main role of an observer?
- To monitor changes and take action when notified
- To initiate updates in other objects
- To prevent data from being shared
- To handle object creation
- 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 does the Observer pattern improve flexibility in a software system?
- By tightly linking observers to the subject
- By allowing changes in the subject to be independently handled by observers
- By restricting changes to a single observer
- By making all updates mandatory for all objects
- 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).
When is the Observer pattern particularly useful?
- When one object needs to inform several others about a change
- When only one object needs to know about all changes
- When no object dependencies are allowed
- When changes happen at compile time only
- 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).
In a typical Observer pattern setup, how does the subject communicate with its observers?
- By directly invoking methods on each observer
- By notifying all observers through a publish-subscribe mechanism
- By sending data via constructors
- By creating new instances of each observer
- By notifying all observers through a publish-subscribe mechanism
(Förklaring: Genom en publicera-prenumerera-mekanism kan subjektet skicka ut notifieringar till alla observers).
What common issue does the Observer pattern help solve in event-driven applications?
- Ensuring all observers have identical states
- Automatically synchronizing state across multiple objects
- Managing single-instance restrictions in objects
- Preventing objects from interacting with each other
- 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).
What is the main purpose of an event in C#?
- To create a new object
- To store data
- To allow an object to notify other objects of changes
- To create a thread
- 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).
What method is called to notify all observers in the Observer pattern?
- NotifyAll
- Broadcast
- Update
- Push
- Update
(Förklaring: Update-metoden anropas för att informera alla observers när en förändring sker i subjektet).
What is required for an observer to be notified in C# events?
- The observer must implement an event handler
- The observer must inherit from a delegate
- The observer must use the “update” keyword
- The observer must override the event
- The observer must implement an event handler
(Förklaring: En observer måste implementera en handler för att kunna reagera på notifieringar).