DP Flashcards

1
Q

What is a design pattern and why do we use them?

A

Solution to a problem that repeatedly occurs in software development

A design pattern shows you how to structure your classes and how classes should talk to each other

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

What are the 3 categories of design patterns

A

Creational - different ways to create objects
Structural - relationships between objects
Behavioural - communication between objects

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

What are 5 creational design patterns?

A
  • Singleton
  • Factory Method
  • Abstract Factory
  • Builder
  • Prototype
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are 6 structural design patterns?

A
  • Adapter
  • Bridge
  • Composite
  • Decorator
  • Facade
  • Flyweight
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are 11 behavioural design patterns

A
  • Chain of responsibility
  • Command
  • Interpreter
  • Iterator
  • Mediator
  • Memento
  • Observer
  • State
  • Strategy
  • Template method
  • Visitor
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is singleton design pattern

A
  • creational design pattern
  • only 1 instance of the class
  • Check satic Singleton instance == null, create instance
  • might be used for database connection manager
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the factory design pattern?

A
  • Creational design pattern
  • Provides an interface for creating objects in a superclass

Ie., Shape check the name of the shape ie., rectangle and then return a shape of that type

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

What is the benefits of design patterns

A

Improves the quality of our code

Makes it more maintainable and scalable

  • Help developers write code that is easy to understand and modify and reduce the time and effort to develop a solution
  • Reduce the risk of bugs
  • Lead to a more consistent code base
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a creational design pattern for?

A

Object creation

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

What is a structural design pattern for?

A

relationships between objects

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

What is a behavioural design pattern for?

A

Communication between objects

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

Which design pattern ensures that only one instance of a class can be created?

A

The Singleton design pattern

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

What is the problem with this implementation of the Singleton design pattern?

public class Database {

private static Database singletonDatabase;

private Database(){};

public static Database getInstance(){
    if(singletonDatabase == null){
        return singletonDatabase = new Database();
    }
    return singletonDatabase;
} }
A

It is not thread safe so will not work in a multi threaded environment

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

Write a code example of the Singleton design pattern that would work in a multi-threaded environment?

A

public class Singleton {
private Singleton() {}

private static class SingletonHolder {    
    public static final Singleton instance = new Singleton();
}

public static Singleton getInstance() {    
    return SingletonHolder.instance;    
} }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the factory method pattern?

A

The factory method pattern is when a factory method is created in its own class to create objects

A parameter can be passed to this method and it will return the object of this type

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

When could the factory method design pattern be a good choice?

A

when you only know at runtime which object to create
or
object creation is expensive
or
the types being created will expand over time

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

When could you use the abstract factory design pattern?

A

When you want to encapsulate a group of individual factories that have a common theme without specifying their concrete classes

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

What is the builder design pattern?

A

The builder design pattern allows you to construct complex objects step by step

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

What category is the builder design pattern?

A

Creational because it allows an object to construct a more complex object by specifying its type and content

20
Q

What is a proxy design pattern?

A

A proxy design pattern creates a proxy object acts like a placeholder for an object that might or might not be needed

21
Q

Give an example of the proxy design pattern?

A

A university internet restricting access to certain sites. The proxy first checks the host you are connecting to, if it is not part of restricted site list, then it connects to the real internet. This example is based on Protection proxies.

22
Q

What category is the proxy design pattern from?

A

Structural

23
Q

What is the proxy design pattern useful for?

A

Security

24
Q

What does the Adapter design pattern do?

A

Connect two incompatible interfaces that could otherwise not be connected

25
Q

What category is the adapter design pattern from?

A

Structural

26
Q

Give 2 examples of an Adapter design pattern?

A

In Java, Action Listener

In a program converting speed records from MPH to KM/H

27
Q

What design pattern would you use if you needed to work with incompatible types?

A

Adapter

28
Q

Describe the decorator design patter?

A

The decorator pattern lets us add additional behaviours to objects at run time by placing the objects inside a wrapper that contains the behaviours

29
Q

Give a real life example of the decorator pattern?

A

Decorating a christmas tree

30
Q

What are decorators an alternative too?

A

Subclassing for extending functionality because it relies on composition instead of inheritance

31
Q

What is the ideal level of coupling and cohesion?

A

Loose coupling, high cohesion

32
Q

What does the iterator let us do?

A

Traverse a collection of objects to access elements without knowing how the data is stored internally

33
Q

What are the 4 components of the builder pattern?

A

Director
Builder (interface)
ConcreteBuilder (implementation)
Product.

34
Q

When should you use the builder pattern?

A

When there are multiple parameters in the constructor

35
Q

Give a real life example of the builder pattern?

A

Building a house because it has many steps roof, interior, basement and structure

If we use the builder pattern we can build a house with many different properties

36
Q

What does UML stand for?

A

Unified Modeling Lanaguae

37
Q

What are UML diagrams used for?

A

To model systems

38
Q

How do you show inheritance in a UML diagram?

A

An arrow with the head at the parent to show the child extends the parent

Ie., Rectangle -> Shape

39
Q

What does a dashed arrow mean in a UML diagram?

A

The side with the arrow head is referenced somewhere in the tail’s code

Ie., Shape –> Document means document is referenced somewhere in the Shape code

40
Q

What does a diamond at one end and arrow at the other end mean in a UML diagram?

A

The class at the diamond side is composed of the class at the arrow side

Ie., Shape *-> Size means the shape is composed of its size

41
Q

How do you show a variable or method is private in a UML diagram?

A

-

42
Q

What is the single responsibility principle?

A

Classes should have a single responsibility

43
Q

What type of pattern is the memento pattern?

A

Behavioural

44
Q

What do we use the memento pattern for?

A

To undo

45
Q

What are the 3 parts of the memento pattern?

A

Originator - object who’s state needs to be saved
Memento - maintains the state of the originator
Caretaker - the object that triggers the save and restore of the state