QCM : Behavioral Pattern Mastery — 7 questions

Questions et réponses du QCM

1. In the context of design patterns, what is a 'State' or 'State Transition' primarily understood to be?

A sequence of steps in an algorithm that are fixed and unchangeable
A pattern that allows an object to alter its behavior when its internal state changes
A condition or mode of an object that encapsulates its current data and behavior
A process of changing an object's state from one to another based on events or conditions

A condition or mode of an object that encapsulates its current data and behavior

Explication

A 'State' is a condition or mode of an object that encapsulates its current data and behavior, defining how it responds in a specific situation. A 'State Transition' is the process of changing from one state to another, often triggered by events or conditions.

2. What is the primary benefit of using the State pattern in software design?

It simplifies conditional logic by encapsulating state-specific behaviors.
It allows direct access to all states from a single class.
It eliminates the need for state transitions.
It requires all states to inherit from a single parent class.

It simplifies conditional logic by encapsulating state-specific behaviors.

Explication

The State pattern encapsulates each state into separate classes, which simplifies complex conditional logic by delegating behavior to these classes. This makes code more maintainable and easier to understand.

3. What is the primary role of the Strategy pattern in software design?

To manage object dependencies and facilitate communication among objects
To encapsulate different algorithms so they can be selected and changed at runtime
To define a skeleton of an algorithm, allowing subclasses to override specific steps
To create families of related objects without specifying their concrete classes

To encapsulate different algorithms so they can be selected and changed at runtime

Explication

The Strategy pattern's main purpose is to encapsulate algorithms into interchangeable objects, allowing the system to select and switch between algorithms dynamically at runtime, thereby promoting flexibility and reusability.

4. Which components are part of the State pattern architecture?

Context, State Interface, Concrete States
Mediator, Observer, Context
Strategy, Algorithm, Client
Finite State Machine, Transitions, Events

Context, State Interface, Concrete States

Explication

The core components of the State pattern include the Context (the object whose behavior varies), the State Interface, and the Concrete State classes that implement specific behaviors.

5. How do Visitor pattern and general operation encapsulation differ in their approach to adding new behaviors to object structures?

Visitor pattern embeds operations within object classes, making it easier to add new behaviors directly into objects.
Visitor pattern allows adding new behaviors by defining new visitor classes, without changing the object classes themselves.
Operation encapsulation involves hiding implementation details within classes, but does not necessarily facilitate adding new behaviors.
Visitor pattern separates operations from object structures, enabling adding new operations without modifying existing classes.

Visitor pattern separates operations from object structures, enabling adding new operations without modifying existing classes.

Explication

The Visitor pattern encapsulates operations into separate visitor classes, allowing new operations to be added without modifying the object structure classes. In contrast, general operation encapsulation within classes hides implementation details but does not inherently support adding new behaviors without modifying classes. The key difference is that Visitor pattern explicitly separates operations from object classes, promoting extensibility.

6. Which statement correctly distinguishes the Difference between FSMs and the State pattern?

FSM is a mathematical model, while the State pattern is a design pattern that can be implemented in code.
FSMs are only used in hardware systems, while the State pattern is only for software.
The State pattern does not support transitions, unlike FSMs.
FSMs change the state automatically, but the State pattern requires manual intervention.

FSM is a mathematical model, while the State pattern is a design pattern that can be implemented in code.

Explication

Finite State Machines are mathematical models used to represent states and transitions formally. The State pattern is a software design pattern that implements similar concepts to encapsulate state-specific behavior.

7. Who is credited with popularizing the State pattern in object-oriented design?

Erich Gamma and the Gang of Four
Barbara Liskov
Donald Knuth
Alan Turing

Erich Gamma and the Gang of Four

Explication

The State pattern was popularized as part of the

Révisez avec les flashcards

Mémorisez les réponses avec 10 flashcards sur Behavioral Pattern Mastery.

State — definition?

Represents an object's condition or mode.

State — definition?

Represents an object's condition or mode.

Strategy — role?

Enables selecting algorithms at runtime.

Voir les flashcards →

Approfondir avec la fiche

Consultez la fiche de révision complète sur Behavioral Pattern Mastery.

Voir la fiche →

Cours similaires

Crée tes propres QCM

Importe ton cours et l'IA génère des QCM avec corrections en 30 secondes.

Générateur de QCM