Composite vs Interpreter patterns Flashcards

1
Q

Similarities and differences between composite and interpreter patterns

A

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.

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

Iterator pattern in the Expression Tree in comparison to preivous uses

A
  • 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

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

The symbol class

A
  • 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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Visitor pattern (applicability and examples)

A
  • 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

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

Observer Pattern pitfalls & mitigation

A
  • dangling references: smart pointers
  • Update overhead: push/pull model
  • unknown update topology: limiting roles
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Builder pattern

A
  • Separate construction of a complex object from its representation, so that same construction process can create different representations
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How Command and Factory Method patterns interact in the Expression Tree

Also, define the Abstract Factory Pattern

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

how the state and command pattern interact

A
  • State constraints which commands can be created.

- Each state defines what commands will cause a transition to another state.

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

State pattern

A

allow an object to alter its behavior when its internal state changes
ex: State.h/cpp, Context.h/cpp

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

how does object-oriented design relate to design patterns

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Suggest simple guidelines for deciding whether a class method should be virtual or not.

Also why virtual destructor

A
  • 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).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a design pattern

A

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.

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

Compare and contrast patterns and algorithm

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Static, Automatic, and Dynamic memory

A

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.

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

Levels of exception safety

A

No safety: no guarantee
basic: valid state if an exception is thrown
strong: unchanged state
noThrow: guarantee.

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

When is it not wise to use the adapter pattern

A

When it is easily possible to modify the existing source code because another intermediate class increases complexity

17
Q

Notes:

A

Notes:

Command, State, and Interpreter patterns usually result in many small classes that complicate maintenance.

18
Q
In the Expression Tree application, a user is in succinct mode. Imagine the moment the user hits enter on the keyboard after typing in a valid expression. In what order would we encounter the following patterns while executing from that moment until when the program prints out the correct answer?
Adapter pattern
Template pattern
Interpreter pattern
Composite pattern
Command pattern
Builder pattern
Iterator pattern
Visitor pattern
A
  1. template pattern
  2. Command
  3. Interpreter
  4. Builder
  5. Composite
  6. Iterator
  7. Adapter
  8. Visitor
19
Q

Explain the difference between a deep and a shallow copy and which method(s) control the depth of copy of a user-defined class.

A
  • shallow: default copying method, copies each class member individually, but does not allocate memory which creates problems for classes with dynamically allocated memory
  • deep: memory is allocated, and a copy of another object is done with the new memory
20
Q

Reactor pattern

A

Allows event-driven applications to demultiplex and dispatch service requests that are delivered to an application from one or more clients.

  • non-preemptive
    examples: Reactor.h/cpp, Event_Handler.h/cpp (VerboseHandler, MacroHandler), Observer.h
21
Q

Template and Strategy patterns

A

Template: Provide skeleton for an algorithm, deferring some steps to subclasses

Strategy: Define a family of algorithms, encapsulate each one, and make them interchangeable to let clients and algorithms vary independently. Ex, sorting or memory allocation since there’s less commonality and there are many algorithms.

The template method is preferred if we have more commonality btn algorithms

Ex: Even_Handler.h/cpp (verbose and succinct), Expression_Tree_Event_Handler.h/cpp

22
Q

Bridge Pattern

A

Separate a (logical) abstraction interface from its (physical) implementation