Design Patterns Flashcards

1
Q

what problem does the vistor design pattern solve

A

Allows you to define a new operation for some classes of an object without changing the class

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

What is single dispatch

A

Single dispatch is when you call a method on an object and the underlying types method is ran

mammal mammal1 = new dog
mammal1.makenoise()

ouput: woof

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

What is double dispatch

A

when the method resolved is based on the underlying type of two objects which are interacting

i.e
mammal lion = new lion
mammal gazelle = new gazelle

lion.interact(gazelle)

void interact (mammal m){
m.makenoise(this)
}

this allows us to call the make noise for gazelle while passing in lion without changing the lions reference type

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

Describe the vistor pattern

A

define a seperate visitor object that implements an operation to be performed on elements of a structure

element can accept some visitor

calls the visitors visit method and passes in itself

alowing the visitor to provide the appropriate functionality for different types

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

explain the visitor pattern in Scotland yard
advance and AI

A

in Advance we add a visitor which adds the functionality of making a move
where the element is move and the vsiitor has an appropriate method for both single and double move

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

explain the factory design pattern

A

has some abstract creator/factory
which has an abstract factory

and the concrete factories implement this build method where it builds some concrete product

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

Explain the relation of factory design pattern to scotland yard

A

has two concrete factories gamestate and model
which builds an object of gamestate and model

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

what does a static method do/mean

A

It means the method does not belong to the instance of the class but the class itself meaning it can be called without an object

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

What makes an object immutable in java

A

is its state cannot change

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

What is a generic method

A

A method where a type is not specified but must still be consistent
i.e
you could have an append method which adds a value to the end of a list
it may take in a value of type T and return a List of type T

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

What is method overloading

A

When you have two methods of the same name with different parameters

i.e
plus(int x,int y ){return x+y}
plus(double x,double y ){return x+y}

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

what is overriding

A

when you have two methods of the same type but one of them replaces the other one
best example with children classes where the child may have a different in a method

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

define downcasting

A

the conversion of a super class reference to a sub class reference

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

What is polymorphism

A

having all of thedifferent functionality to a method based on the underlying type on the heap

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

What does it mean to make a method or a class abstract

A

It means you cannot make an instance of the class and is used to make a blueprint

if a method is abstract then the class is abstract too

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

What is an interface

A

A completely abstract class

17
Q

what is dynamic dispatch

A

It resolves the method call based on the underlying type on the stack
and its doe at run time

18
Q

What is an anonymouse class

A

a Class with no identifyer

19
Q

why would you make an inner class

A

to group classes together makes code more readable

20
Q

How to make an instance of an inner class

A

make an instance of the outer class and then make an instance of the inner class i.e outer.new inner()

21
Q

What is an exeption

A

an exception is an error acd in the try catch statement allows you to catch exceptions using the type Exception

22
Q

What is an access modifier
and name three

A

keywords that can be added to define who can access a class or a method
private protected public

23
Q

who can access an attribute/method/class if it is public/protected/default/private

A

public: everyone
protected: the class, the package and a subclass outside the package
default: the class, the package
private: the class

24
Q

How do iterators work

A

the class must implement iteratable
which has one method iterator which returns a implemntation of iterator
iterator is a built in interface
has two methdos has next and next

so can say
return new iterator(){
implemnattions of how we want to iterate over the object
}

25
Explain an abstract factory
an abtract factory involves creating a range of products with different types an abstract factory involves a concrete factory with multile build methods which create objects of a certain type typically will have multiple concrete factpry to produce different products of a common type for example if you wanted to create multuiple ranges of tables and chairs i.e wooden or metal etc