Design Patterns Flashcards
what problem does the vistor design pattern solve
Allows you to define a new operation for some classes of an object without changing the class
What is single dispatch
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
What is double dispatch
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
Describe the vistor pattern
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
explain the visitor pattern in Scotland yard
advance and AI
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
explain the factory design pattern
has some abstract creator/factory
which has an abstract factory
and the concrete factories implement this build method where it builds some concrete product
Explain the relation of factory design pattern to scotland yard
has two concrete factories gamestate and model
which builds an object of gamestate and model
what does a static method do/mean
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
What makes an object immutable in java
is its state cannot change
What is a generic method
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
What is method overloading
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}
what is overriding
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
define downcasting
the conversion of a super class reference to a sub class reference
What is polymorphism
having all of thedifferent functionality to a method based on the underlying type on the heap
What does it mean to make a method or a class abstract
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
What is an interface
A completely abstract class
what is dynamic dispatch
It resolves the method call based on the underlying type on the stack
and its doe at run time
What is an anonymouse class
a Class with no identifyer
why would you make an inner class
to group classes together makes code more readable
How to make an instance of an inner class
make an instance of the outer class and then make an instance of the inner class i.e outer.new inner()
What is an exeption
an exception is an error acd in the try catch statement allows you to catch exceptions using the type Exception
What is an access modifier
and name three
keywords that can be added to define who can access a class or a method
private protected public
who can access an attribute/method/class if it is public/protected/default/private
public: everyone
protected: the class, the package and a subclass outside the package
default: the class, the package
private: the class
How do iterators work
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
}