Composite vs Interpreter patterns Flashcards
Similarities and differences between composite and interpreter patterns
Similarities:
- an interpreter pattern is like a specialized composite pattern
- they both have terminal and non-terminal derived classes
- Both use a recursive hierarchy of objects
Differences
- The interpreter pattern takes in a string, analyses, and returns an object
- An interpreter pattern has context
Examples:
Composite Pattern: Expression_Tree.h/cpp, Component_node.h/cpp, Composite_Unary/Binary_Node.h/cpp, all Composite_(operator)_Node.h/cpp
Interpreter Pattern: interpreter.h/cpp.
Iterator pattern in the Expression Tree in comparison to preivous uses
- Similarity: Allows traversal through a container using beign(), end(), dereference, and so on
- Difference: Iteration more complex and iteration needs to maintain state, also we have 4 types of iterators in Expression Tree.
ex: Expresion_Tree_Iterator.h/cpp, Expression_Tree_Iterator_Impl.h/cpp, (order)_Order_Expression_Tree_Iterator_impl.h/cpp
The symbol class
- Allows Symbols and Component_Nodes to be developed separately
- Allows for the translation of symbols into component nodes by the builder pattern (i.e calling build on a symbol builds a corresponding component node)
Visitor pattern (applicability and examples)
- Centralizes operations on an object structure so that they can vary independently but still behave polymorphically.
Ex: evaluation.h/cpp, print.h/cpp, stats.h/cpp
Observer Pattern pitfalls & mitigation
- dangling references: smart pointers
- Update overhead: push/pull model
- unknown update topology: limiting roles
Builder pattern
- Separate construction of a complex object from its representation, so that same construction process can create different representations
How Command and Factory Method patterns interact in the Expression Tree
Also, define the Abstract Factory Pattern
Command Pattern (Encapsulate the request for a service to be executed at some point in the future)
Factor Pattern (Creates Command objects): Controls and constrains the creation of an object by only allowing construction by another class.
- makes it easier to extend new commands
- each command has a corresponding factory method
Abstract Factory Pattern: creates families of related objects without specifying subclass names.
how the state and command pattern interact
- State constraints which commands can be created.
- Each state defines what commands will cause a transition to another state.
State pattern
allow an object to alter its behavior when its internal state changes
ex: State.h/cpp, Context.h/cpp
how does object-oriented design relate to design patterns
- OOP is a programming methodology that organizes code into objects and relationships of objects. Therefore, design patterns use this programming concept to suggest successful methods of constructing classes/objects to solve a certain recurring scenarios in programming.
Suggest simple guidelines for deciding whether a class method should be virtual or not.
Also why virtual destructor
- We make methods virtual if we anticipate that derived classes might require inherited methods and functionality and, most importantly, if they are expected to be overridden.
- When a class has virtual methods and derived classes, we make the destructor virtual to guarantee that the derived class object will be destructed properly (where both base and derived class destructors are called).
What is a design pattern
A design pattern is a reusable solution at the architectural level for a problem that is recurring. They are independent of a programming language and have pros and cons.
Compare and contrast patterns and algorithm
Similar
- Both are ways of solving problems that occur in software engineering and are also used simultaneously
Differe
A design pattern
- Is a reusable (general) solution at an architectural level for a recurring (general) problem.
- They can’t be translated directly to code and are abstract.
An algorithm, on the other hand,
- Is a highly specific, procedural description of how to compute a solution to a specific problem.
- Also, algorithms are less about reusability and more about efficiency and accuracy.
- Can be translated directly into code
Static, Automatic, and Dynamic memory
Static: allocated at the beginning of a program and exists through the execution of the program. Holds global variables, static variables, and anything that uses static.
Automatic: allocated at runtime in the program’s execution stack and deallocated when the scope of the code ends. Ex: Local variables, functions, parameters, if-else blocks
Dynamic memory: Allocated at runtime in the programs heap using new. Delete needed.
Levels of exception safety
No safety: no guarantee
basic: valid state if an exception is thrown
strong: unchanged state
noThrow: guarantee.