Week 7 Flashcards

1
Q

What is a Design Pattern?

A

Design Patterns are meant to provide a general reusable solution to one type of problems during software development - they provide an architecture

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

What are the 3 Principles of OO design?

A

Identify the aspects of your application that vary and separate them from what stays the same

Program to an interface, not an implementation

Favor ‘object composition’ over class inheritence

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

What are 3 types of Design Patterns?

A

Creational Patterns - object creation

Structural Patterns - composition of classes and objects

Behavioral Patterns - interaction between objects

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

What is the difference between the Factory Pattern and the Abstract Factory Pattern?

A

Factory Pattern - Uses inheritance (extends class and override a factory method) and gives you a complete object in one shot

Abstract Factory Pattern - Uses composition (defines abstract type for creating a family of products and lets subclasses decide how those products are produced) and returns a family of related classes

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

How does the Builder Pattern work?

A

The Builder specifies the interface for creating parts of the complex (Product) object

The ConcreteBuilder objects create and assemble parts that make up the Product through the Builder interface

The Director object takes responsibility for the construction process of the complex (Product) object, however it delegates the actual creation and assembly to the Builder interface

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

What is the main difference between Factory, Abstract and Builder Patterns?

A

Factory Pattern constructs a complete object in one shot

Abstract Factory Pattern returns a family of related classes

Builder Pattern Constructs complex object step by step

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

What are the benefits of Builder Pattern?

A

Encapsulates the way a complex object is constructed

Allows objects to be constructed in a multistep and varying process (as opposed to one-step factories)

Hides internal representation of the product from the client

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

What is the Singleton Pattern?

A

“Ensures that a class has only one instance, and provides and global point of access to it

Problem Want to have just a single instance of an object

Examples:

Window managers

File systems

Print spoolers

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

What are the 3 ways to create a Singleton Pattern?

A

Static Member - Create a private static variable of the Singleton class. This is the only instance of Singleton class

Private Constructor - Create a private constructor for making sure an outer class can NOT instantiate object from the Singleton class

Static public method - Create a global point of access to get a Singleton instance

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

What is Lazy Initialisation?

A

The Singleton class employs a technique known as Lazy Initialisation to create the singleton, to ensure that the singleton instance is not created until the getInstance() method is called for the first time (i.e created only when needed)

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

How do you create a Thread-Safe Singleton class?

A

Easiest way to create a Thread-Safe singleton class is to make the global access method synchronized, so that only one thread can execute this method at a time

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

How do you avoid extra overhead with Thread-Safe Singleton?

A

To avoid this extra overhad every time, Double Checked Locking Principle is used

Synchronized block is used inside the if condition with an additional check to ensure that only one instance of singleton class is created

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