1/93
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
What is the main intent of the Proxy design pattern?
To provide a surrogate (placeholder) for another object to control access to it.
What is another name for the Proxy design pattern?
Surrogate.
Why might a web page use a proxy for images or videos?
To allow the page to load and be visible immediately while waiting for media content to load.
What problem does the Proxy pattern solve in programming?
It simplifies the implementation and avoids affecting other modules while controlling access to another object.
In the Proxy pattern, what is the role of the ImageProxy
class?
It contains all the same methods as Image, performs placeholder behavior, and forwards calls to the actual Image if it exists.
What are the four types of proxies mentioned?
Remote proxy, virtual proxy, protection proxy, and smart reference.
What is a remote proxy?
A local object that represents a network object (e.g., placeholders for web resources).
What is a virtual proxy?
A proxy that creates expensive objects on demand.
What is a protection proxy?
A proxy that adds permission checks before allowing access to the object.
What is a smart reference in the context of the Proxy pattern?
A proxy that adds behavior such as counting accesses to the object.
What is the structure of the Proxy pattern?
It involves a Proxy and a Subject. The Proxy has an identical interface to the Subject and controls access to it.
What is the role of the Proxy class (ImageProxy
)?
Maintains a reference to the real subject, controls access, and optionally creates or deletes it on demand.
What does the Subject interface define in the Proxy pattern?
A common interface for the RealSubject and Proxy so the Proxy can be used in place of the RealSubject.
What is the RealSubject in the Proxy pattern?
The actual object being represented by the proxy
What is the collaboration between Proxy and RealSubject?
The Proxy forwards requests to the RealSubject when appropriate.
What are the consequences of using the Proxy pattern?
Introduces indirection; remote proxies hide object location, virtual proxies manage creation, protection proxies enforce access control, and smart references add functionality.
What is the benefit of copy-on-write in the Proxy pattern?
Saves memory and computation by only copying/loading the object when it is actually needed.
Where is the Proxy pattern commonly used?
In web/remote object retrieval and rich text documents with embedded media.
How does the Proxy pattern differ from the Adapter pattern?
Adapters provide different interfaces, while Proxies control access and can add extra functionality.
How is a Proxy similar to a Decorator?
Both may have similar implementations, but Decorators add responsibilities whereas Proxies control access.
Where can additional reading on the Proxy pattern be found?
GoF (Gang of Four).
What is the main intent of the Command design pattern?
To encapsulate a request as an object.
What are alternative names for the Command pattern?
Action, Transaction
What problem does the Command pattern solve in UI systems?
It allows UI elements like buttons to make customizable requests to different receivers by assigning them commands.
What is the benefit of making requests into objects in the Command pattern?
These objects can be passed around, stored, or manipulated like any other object.
What is a common method in Command pattern classes and what does it do?
Execute(); it performs the command’s action.
What does a ConcreteCommand store to perform its task?
A reference to a Receiver (e.g., MenuItem, Document, etc.).
When should the Command pattern be used?
When you want to:
parameterize objects by actions
queue/execute requests later
support undo
log operations
use transactions
What makes the Command pattern suitable for implementing Undo functionality?
Command.Execute() can store the state before executing.
What is the structure of the Command pattern?
It includes Command, ConcreteCommand, Client, Invoker, and Receiver.
What is the role of the Command interface?
Declares the interface for all commands
What does the ConcreteCommand class do?
Binds a Receiver to an action and implements the Execute() method.
What does the Client do in the Command pattern?
Creates a ConcreteCommand and sets its Receiver.
What does the Invoker do in the Command pattern?
Stores commands and calls their Execute() methods when needed.
What does the Receiver do in the Command pattern?
Knows how to perform the requested operation.
What is a collaboration sequence in the Command pattern?
Client → creates ConcreteCommand → assigns Receiver → Invoker stores command → calls Execute() → command acts on Receiver.
What is a major consequence of using the Command pattern?
It decouples the object that invokes a request from the one that knows how to execute it.
How are Commands treated in object-oriented programming?
As objects, allowing features like subclassing and composition.
What is a composite command?
A command that iterates through a list of commands and calls Execute() on each one.
How does the Command pattern support extensibility?
New commands can be added without changing existing classes.
Where is the Command pattern commonly used?
UI callbacks (menus, buttons), database queries, delegates (in C++, C#, etc.).
What patterns are related to the Command pattern?
Composite (for composite commands)
Memento (to store command state for undos)
Prototype (commands can be cloned)
What is the intent of the Chain of Responsibility pattern?
To decouple the sender and receiver of requests.
What is a real-world example of the Chain of Responsibility pattern in UI?
A help system where hovering over different parts of the interface shows context-specific tooltips, and if no specific help is found, it shows help from a parent element.
How does the Chain of Responsibility pattern work?
A request is passed along a chain of handlers until one handles it; the sender doesn’t know who will handle it.
What two things must each object have to implement Chain of Responsibility?
An interface to handle requests and a reference to the next object in the chain (successor).
When should you use the Chain of Responsibility pattern?
When more than one object may handle a request, and you want to dynamically determine the receiver without specifying it.
What is the structure of the Chain of Responsibility pattern?
It includes a Handler, ConcreteHandler(s), and a Client.
What does the Handler do in the pattern?
Declares an interface for handling requests and may implement the link to the successor.
What does the ConcreteHandler do?
Handles requests it's responsible for, may access and forward to its successor if it can't handle the request.
What is the Client’s role in this pattern?
Initiates the Handle() request to a ConcreteHandler.
How do ConcreteHandlers collaborate in this pattern?
A ConcreteHandler may collaborate with itself or forward requests to its successor.
What is a key benefit of Chain of Responsibility?
Reduced coupling between sender and receiver.
What flexibility does Chain of Responsibility add?
You can change or add handlers without modifying the sender or affecting others in the chain.
What is a potential downside of using Chain of Responsibility?
The request might not be handled at all if no handler is appropriate, requiring null checking.
Where is Chain of Responsibility commonly used?
UI systems and graphics/game engines for input handling.
What pattern is Chain of Responsibility often used with?
Composite; in such cases, the parent components act like handlers’ successors.
What is the intent of the Visitor design pattern?
To represent an operation to be performed on existing classes, without changing those classes.
What challenge does the Visitor pattern solve in an image editor scenario?
It avoids multiple nested loops and duplicated code when saving shapes in different formats by separating operations from object structure.
How does the Visitor pattern avoid changing shape or format classes when adding new save formats?
By implementing each format as a Visitor subclass and allowing shapes to accept visitors.
What is the purpose of the Accept(Visitor v) method in ConcreteElement classes?
It calls a specific Visit method on the Visitor, allowing the Visitor to perform operations based on the element’s type.
How would a Circle use the Visitor pattern to support saving?
Circle implements Accept(Visitor), which calls Visitor.SaveCircle(this), allowing format-specific logic to be defined in Visitor subclasses like PNGVisitor.
Why is the Visitor pattern useful in the image editor example?
It allows new file formats to be added without changing the shape classes.
When should you use the Visitor pattern?
When you want to perform operations across a class structure without modifying the classes and you have many different operations to perform.
What does the Visitor interface declare?
Visit methods for each class of ConcreteElement in the object structure (e.g., VisitCircle, VisitText).
What does a ConcreteVisitor implement?
The specific operations for each type of element (e.g., PNGSaver implements SaveCircle, SaveText, etc.).
What is the role of the Element in the Visitor pattern?
Defines the Accept(Visitor v) operation.
What is the client’s role in the Visitor pattern?
Creates the ConcreteVisitor and initiates traversal of the object structure.
What is a major benefit of using the Visitor pattern?
Adding new operations is easy—just create a new Visitor subclass.
How does the Visitor pattern help organize code?
It groups related operations together in the Visitor rather than scattering them across many classes.
What is a drawback of the Visitor pattern when adding new types of elements?
Each new element type requires updates to every existing Visitor, which can be cumbersome.
Why is Visitor useful across class hierarchies?
It can operate on elements that don’t share a common parent class, allowing flexible traversal.
What is the main intent of the MVC pattern?
To decouple visual (UI) and state (data) classes in an application.
Why is decoupling important in UI applications?
It keeps code organized and allows for better maintenance and scalability by separating concerns.
In a bank terminal app, how can opening a deposit prompt be broken into MVC parts?
User presses button: Controller
UI is updated: View
Internal state changes: Model
What makes an application an MVC application?
UI code, state code, and input handling code are separated into View, Model, and Controller components.
Do all classes in an MVC application need to be strictly categorized into Model, View, or Controller?
No, but UI classes should not handle state or user interaction logic.
What is the role of the Model in MVC?
Stores the application state and logic; it's the source of truth for all on-screen data.
What is the purpose of the View in MVC?
Displays data from the Model to the user and presents a UI representation of the state.
Should the View interact with the Model directly?
No, it should only be updated by the Model indirectly and manipulated via the Controller.
What is the Controller’s role in MVC?
Handles user input, validates it, and manipulates the Model accordingly.
How do the MVC components collaborate?
Controller manipulates Model
Model updates View
User uses Controller and sees View
What is one benefit of decoupling the View and the Model?
You can have multiple views represent the same data (e.g., charts, lists).
What pattern is commonly used with MVC to keep Views in sync with Models?
Observer pattern.
What is the main intent of the Interpreter design pattern?
To interpret a grammar, such as those used in regular expressions or programming languages.
What is a grammar in the context of programming?
A set of rules that defines valid strings in a language, including rules for structure, tokens, and expressions.
What is a syntax tree?
A hierarchical tree structure built from grammar rules, used to represent the structure of a valid sentence or expression.
What is the role of repetition and recursion in grammars?
Repetition handles patterns like lists, and recursion allows an expression to include itself for complex nested structures.
How does a grammar help in interpreting or validating data?
It allows a system to check whether a string matches the rules (validation) or extract structured data from it (interpretation).
What is the purpose of the AbstractExpression class in the Interpreter pattern?
It defines the interface for interpreting text and is the base for all terminal and non-terminal expressions.
What is the difference between TerminalExpression and NonterminalExpression?
TerminalExpression interprets basic, literal symbols; NonterminalExpression interprets combinations of expressions (rules).
When should you use the Interpreter pattern?
When you need to interpret a simple language using a grammar and performance is not a critical concern.
What is the role of the Context class in the Interpreter pattern?
It holds global information needed by the expressions during interpretation, such as input strings or output objects.
What are some known uses of the Interpreter pattern?
Regular expressions, file readers, and compilers.