OO design patterns Flashcards
How can you de-couple creating an instance of a class (since it’s dependent on a concreate class)?
By creating an abstract factory class
What are object patterns?
Patterns produced by experienced developers, that when followed produce code with less bugs/issues.
What is encapsulation?
Wrapping data and it’s methods into a unit by restricting direct access to an objects internal state.
- only allowing interaction through well defined interfaces.
What is inheritance?
Reusing code from superclasses. A subclass uses the functionality from a superclass and is able to extend it for modification.
How can you ensure loose coupling?
make every public method final. If something is public there’s no reason it should be overridden, unless you’ve allowed this when defining the class.
What are the OO benefits (achieved by following SOLID)
- lower coupling
- good cohesion (single responsibility)
- code reusability
- code is easier to understand, test and maintain
What are the two steps for OO development and explain them?
- OO Analysis: Before programming starts create a list of classes
- OO design: identify data/methods in each class and relationships between classes. Can be done whilst coding.
What is the role of documentation and list some examples:
- makes it easier to communicate ideas
- use it to control what’s in requirements
- helps to understand code
- UML diagrams, requirements/specification, database diagrams, user guides, use cases, actor models
What are patterns vs components?
- patterns: general description of approaches to a problem
- components are reusable code which solves the approach
List a number of OO design patterns:
- Model View Controller
- Command Pattern
- Wrapper classes
- Memento
- flyweight
- Chain of Responsibility
- Singleton (and multiton)
- Double Lock Checking
- Factory Patterns
- abstract factor
- Builder Pattern
Describe the MVC OO design pattern:
- An architectural pattern
- separates concerns/code into 3 components
- model (represents data and logic of application, manages state of application and handles core functionality)
- view (handles the app’s UI, displays data to the user and sends user inputs to the controller)
- controller (intermediary between model and view. Processes user inputs, interacts with model and updates view accordinly)
Describe what each of the Model, View and Controller components would be implemented as:
Model: classes and objects
View: GUI framework / web templates
Controller: classes and methods
Describe in detail what the model component in the MVC does:
- handles persistence: communicates with DB
- handles logic
- handles applications internal state
- validates data
Describe in detail what the view component in the MVC does:
- Renders UI on screen
- presents information to the user
Describe in detail what the controller component in the MVC does:
- Processes user inputs
- passes events to the model
- instructs the view to render
What are the benefits of the MVC OO design pattern?
- Clear separation of concerns
- single responsibility
- easier to port to any platform/change UI/ have multiple UIs
- concurrent development
Describe the command pattern
an object is used to encapsulate all information needed to perform an action or trigger an event at a later time
Describe the structure of a command pattern:
- Interface or abstract class (describes contract for executing a command, includes execute() method.
- concrete command class (method that defines execute() method)
- receiver class (performs the actual work when command is executed)
What are the key benefits of the Command Pattern?
- Open/Closed Principle: Adding new commands doesn’t require changes to existing code, just extension.
- lower coupling: decouples classes that invokes the operation from the object that knows how to execute the operation
Describe the factory class object pattern:
- constructs instances of a class, used when object type is not known at compile time but will be decided on at runtime
Give an example of a scenario when you’d use a factory class pattern:
For handling different image file types:
How to handle the string when the input image file could be JPG, PNG, etc…
- have abstract class Image()
- have classes that extend Image, ImagePNG, ImageJPG
- have factory class or Image class use a method getInstance(), that has logic in to create specific object with correct subclass
public static Image getInstance(String fileName){
if (fileName.endsWith(“.png”)) {
return( (Image)new ImagePNG(fileName));
}
if (fileName.endsWith(“.jpg”)) {
return( (Image)new ImageJPG(fileName));
What are the benefits of using the factory class object pattern
- Supports open closed principle, add subclass if another object type is needed
- encapsulation (of logic required to create objects)
- code reusability
Describe a singleton (class) design pattern:
- there is only at most one instance of a class throughout the lifetime of an application
- access to the singleton is provided through a static method (getInstance) which checks if the one instance has already been created, if it hasn’t it creates it, if it has it returns the existing one.
What is lazy initialisation?
The instance of a singleton class is only created when the object is needed (not when the application is loaded up.