OOP Final Flashcards
polymorphic
one name and many different meanings
Overloading
ad hoc polymorphism
one name that refers to two or more different implementations. Must share the same method name.
i.e. occurs within the context of the parent class/child class relationship
Overriding
inclusion polymorphism
A child class redefining a method inherited from a parent class. Must maintain the same method signature
i.e. single function name (or method name) has several alternative implementations
Overloaded method binding
distinguished at compile time based on their type signatures
Polymorphic variable
variable that can hold different types of values during the course of execution
Pure polymorphism
when a polymorphic variable is used as a parameter
Generics (Templates)
A way of creating general tools or classes by parameterizing on types
i.e. leaves types of methods & classes unspecified to be used in wider range of situations
Inheritance & composition
Types of software reuse
Goal of polymorphism
Facilitates reuse of standard software components that leads to:
rapid development
reliability
ease of use
inheritance
is-a relationship
composition
has-a relationship
Using Composition
Everything must be redefined while leveraging an existing software component in the creation of a new application
i.e. Set and List are unique data structures but Set can be reimplemented completed using List class
Composition class hierarchy
no explicit or implicit claims about substitution.
i.e. Set and List are distinct and neither can be substituted in situations where the other is required
Using Inheritance
Declare a new class as a child of an existing class. Thus, all data areas and functions associated with the original class are automatically associated with the new data abstraction.
Composition Advantages
- simpler, and clearly indicates what operations are provided
- Easier to change underlying details (change data representation)
Inheritance Advantages
- shorter code from code reuse
- possibly increased functionality
- slight advantage in execution time
Inheritance Disadvantages
- Can be difficult to understand what behavior is provided
- May introduce unintended usage of behavior
i.e. programmer must understand a class constructed using inheritance and the parent class (yo-yo problem - flipping back and forth between two or more classes)
Polymorphism Mechanisms
Overloading
Overriding
Polymorphic Variable
Generics
Static Polymorphism
Compile time polymorphism
compiler identifies which method is being called at the compile time
Dynamic Polymorphism
Run-time Polymorphism
method being called is determined at runtime and not at compile time
Implementing Static Polymorphism
Method & operator overloading
Implementing Dynamic Polymorphism
Method overriding
virtual
override
if the same method is present in both superclass and subclass then the subclass method must use keywords in its method signature to be called during run-time
Static Polymorphism advantages
- more flexibility in
Dynamic Polymorphism advantages
- Flexibility & Extensibility
- Late Binding
- Support for Inheritance
- Easier maintenance and updates
late binding
The decision about which method to call is deferred until runtime. This allows for more dynamic behavior, where the appropriate method to invoke is determined based on the actual type of the object during runtime.
Static Polymorphism advantages
- Better performance since compilers knows at compile time which method to call
- Early error detection
- Simplicity
- No runtime overhead
Choosing between runtime and compile time polymorphism
The choice between runtime and compile-time polymorphism depends on the specific requirements of the program. In many cases, a combination of both types of polymorphism is used to leverage their respective benefits in different parts of the code.
i.e. a system might use runtime polymorphism for managing a polymorphic collection of objects, while using compile-time polymorphism for efficiency in certain performance-critical sections.
Design Pattern Goal
- Reduce information flow that must pass over this boundary
- Make process of connecting simple
Pattern description
name
synopsis
forces
solution
counterforces
related patterns
synopsis
A short description of the problem the pattern design to solve
ex. Used to connect a client who needs a service described using once interface with a provider who implements the desired functionality but uses a different interface
name
used to describe a pattern.
ex. The adapter pattern
forces
a description of the requirements for a pattern or the considerations that must be taken into account in the use of the solution
Counterforces
A description of reasons that might need to be considered in deciding to not use a pattern
Related Patterns
this section describes related patterns that user might want to consider in the solution to their problem
solution
the resolution
ex. the adapter implements the interface as specified by the client and passes the work on to the service provider
Iterator design pattern
Problem: provide client access to elements in a collection without exposing the structure of the collection
solution: allow client to manipulate that returns the current value and move to the next element in the collection
Description: Iterator is used to traverse a container of data to access the container’s elements without the need to know the underlying structure
Creational Pattern
These type of pattern support the creation of objects. Because in certain situations there are more elegant ways than using the new operator.
Software Factory design pattern
Problem: simplify the creation of many different types of classes which share a similar parent class
Solution: Hide creation within a method, have the method declare a return type that is more general than its actual return type
Description: Abstract Factory provides an interface for creating families of related or dependent object without to specify concrete classes
Singleton design pattern
Problem: You want to ensure there is never more than one instance of a given class
Solution: Make the constructor private, have a method that returns just one instance which is held inside the class itself
Description: This pattern ensures that only one instance of an object is created and that this instance is globally accessible
Singleton Examples
Logger class
Configuration Management
Database Connection Pool
Print Spoolers
Decorator design pattern
Problem: Allow functionality to be layered around abstraction, but still dynamically changeable
Solution: Combine inheritance and composition by making an object that both subclasses from another class and holds an instance of the class
Description: The decorator pattern allows to add functionality to an object at run time without altering its structure
Adapter design pattern
Used to connect a client (Object that needs a service) with a server (object that provides a service)
ex. Client requires a certain interface while the server provides necessary functionality it does not support the interface
Runtime vs Compile time (4 each)
Runtime
- flexibility and extensibility
- late binding
- support for inheritance
- easier maintenance and updates
Compile time
- performance
- early error detection
- simplicity
- no runtime overhead