Head First Design Patterns Flashcards

1
Q

Strategy Design pattern story

A

made a duck super class which all other classes inherit from and implement their own methods and then they wanted to make the duck fly but then the problem arises all the ducks now fly but there is a rubber duck that shouldn’t fly so he thought ok i will make the method and override in all the subclasses but then there is a wooden duck who doesn’t quack so he thought ok i will just make an interface for fly and quack and only ducks who can fly or quack will implement them but then there will be a problem of duplicate code so now we fix the problem we take what varies like flying and quacking and we create a whole new set of classes for these behaviors alone so we will make an interface for each behavior and each implementation of the behavior will implement this interface that is in contrast to what we were doing before as we were relying on an implementation either from the parent class or the subclass own implementation and now the key is that we delegated the behavior of the flying and quacking to another class instead of implementing it in the super or subclass so now we have two instance variables inside of duck class and two methods that call fly or quack of the two interfaces and then in each subclass is responsible to initialize an objects of the behavior that it wants and then in the duck class we create setters for the behavior so we can set it dynamically during runtime

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

what do we preceive about inheritance?

A

that it’s a great purpose for reuse but it doesn’t work for maintenance

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

what is the one thing that is constant in all programming?

A

that overtime the application grows and changes or it will die

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

what is the design principle for inheritance?

A

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

take what varies and encapsulate it so it won’t affect the rest of your code

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

what are the benefits of encapsulating the parts the vary?

A

fewer unintended consequences from code changes and more flexibility in your system

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

design pattern that help us while designing the duck behaviors?

A

program to an interface not an implementation

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

why did we use an interface and not an abstract class isn’t the whole point to use polymorphism?

A

program to an interface really means program to a supertype so that the object at runtime isn’t locked into the code so that the class declaring these objects doesn’t have to know about their implementations

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

what do we break when we manually in the constructor initialize the object?

A

we are programming to an implementation not an interface

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

how to set the reference of the object or the behavior and functionality of it dynamically during runtime?

A

buy implementing setter and getter methods

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

how should u start thinking about the set of behaviors?

A

as they are a family of algorithms that as an algorithm represent something that a class would do

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

what is better Has-A or IS-A

A

HAS-A

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

what is the design principle for using compostion?

A

favor composition over inheritance

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

why is composition better than inheritance?

A

as composition gives you alot more flexibility not only does it encapsulate a family of alogrithms but it also lets u change the behavior at runtime as long as the objects implements the correct interface

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

should u spend time on reuse or maintainability?

A

as most of the coding is done after the software is ready due to change so we should focus on maintainability

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

what is the definition of the strategy design patterns?

A

defines a family of algorithms, encapsulates each one , and makes them interchangeable.
strategy lets the algorithm vary independently from clients that use it

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

what is a common misconception about design patterns?

A

that by knowing good OO Basics u will be able to construct reusable ,flexible , maintainable systems

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

Observer Design Pattern Story?

A

we have a weather data application that has 3 things , weather station which deals with physical objects to get weather data , weather object that gets data from weather station and displays it and the display and we want to implement a method called measurmentChanged to update the 3 displays with the new weather and we know that whenever the weather changes measurmentChanged function gets called we don’t know how or even care we just get the info and we know that in the future developers can add more displays

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

definition of the observer pattern?

A

the observer pattern defines one to many dependency between objects so that when one object changes state , all of it’s dependents are notified and updated automatically

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

what do u mean by loose coupling?

A

two objects are loosely coupled when they can interact but have very little knowledge of eachother

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

what is the design principle for loose coupling?

A

strive for loosely coupled designs between objects that interact

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

what should u never depend on while using the observer pattern?

A

the order for the observers

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

what is the open-closed principle?

A

classes should be closed for modification but open for extension

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

should u apply open-closed principle everywhere in your code?

A

no just to the parts that might change otherwise ur code will become complicated

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

what is the story for the decorator pattern?

A

i have a coffee shop with every beverage having the same condiments or different ones and u want to calculate each beverage price if u use inheritance what will happen when the price of milk changes? u will have to change it in every class so we use composition we will start with the beverage and decorate it with condiments at runtime and rely on delegation to calculate the cost

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

what is the definition of the decorator pattern?

A

it attaches additional responsibilities to an object dynamically .Decorators provide a flexible alternative to subclassing for extending functionality

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

how should you use inhertiance?

A

not to get behavior but to achieve type matching

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

what is wrong with “new”?

A

technically there is nothing wrong with new but if ur code has a alot of conditions and depending on these conditions u use the new keyword that means it’s time to use factory design pattern

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

what is the story for factory design pattern?

A

u have a pizza shop and when u order a pizza u have a condition upon which u create an object of pizza like cheese or ham pizza and each pizza knows how to prepare itself but then u want to add more pizza will have to reopen the class and modify it once again so we will move the creation of the pizza to another class and it will be responsible for creating the pizza we want but now we want each franchize to be responsible for the pizza creation process we we make the createPizza method abstarct in the pizza store

29
Q

is the simple factory a real pattern?

A

no it’s just an idiom not a a pattern like the Factory Design Pattern

30
Q

what is a factory method?

A

it handles the object creation and encapsulates it in a subclass this decouples the client code in the superclass from from the object creation code in the subclass

31
Q

what is the factory method pattern?

A

defines an interface for creating an object but lets subclasses decide which class to instantiate. Factory method lets a class defer instantiation to subclasses

32
Q

why do we use the factory pattern?

A

1 - there is logic behind instantiating the object

2 - polymorphism

33
Q

what is the abstract factory pattern?

A

it provides an interface for creating families or related or dependent objects without specifying their concrete classes

34
Q

what is the use case for Abstract Factory pattern?

A

we want to build a couple of UI components that are specific to different operating systems like windows , mac , linux so the Abstract factory pattern gets us the component of the specific operating system that we want

35
Q

what is the difference between abstract factory and factory?

A

abstract factory returns a collection of objects not just one

36
Q

what is the definition of the singleton patterns?

A

ensures that a class has only one instance and provides a global access to it

37
Q

why shouldn’t you use the singleton pattern?

A

1 - if anything leaks to the global space u can’t reason that no one else will change it
2 - u r assuming that u will only need one instance of it like a chat u need only one instance but then u want more than one chat room

3 - testing is difficult

38
Q

definition of the command pattern?

A

encapsulates a request as an object thereby letting you parameterize other objects with different request queue or log requests and support undoable operations

39
Q

what is the story for the command pattern?

A

i have an invoker and a receiver and i want to attach a command to that invoker like have a remote and i want it to control a bunch of lights so i can attach multiple commands to the same invoker or different buttons and each command has a do and undo like turnoff and turnon in the same command

40
Q

what is a great use for command pattern?

A

in any command in any program u can undo the last command so we put them in a list and u just call the unexecute command of the last one

41
Q

definition of the adapter pattern?

A

it converts the interface of a class into another interface the client expects. adapter lets classes work together that couldn’t otherwise due to incompatible interfaces

42
Q

do we use the adapter do change the behavior?

A

no we use it to just adapt the client code to call the same behavior that we want

43
Q

what is the story of the adapter?

A

we have a library that we use that we are calling a method on it called request but we are not sure that the method will stay the same in the external library so we create an adapter with a request method and the adapter calls the external method so when the library change we can change the adapter no the client

44
Q

what is the story of the facade?

A

when u follow the single responsibility principle u end up with alot of small classes so the facade provides just an exterior to deal with those classes to make thing easier

45
Q

what is the law demeter or the principle of the least knowledge?

A

if you are in an object u r only allowed to invoke methods on
1 - that same object
2 - objects that are passed as parameters
3 - objects that u created
4 - a component of this object(an instance variable)

46
Q

what is the definition of the facade pattern?

A

it provides a unified interface to a set of interfaces in a subsystem . facade defines a higher lever interface that makes the subsystem easier to use

47
Q

definition of the proxy pattern?

A

it provides a surrogate or a placeholder for another object in order to control access to it

48
Q

what are the types of proxy pattern?

A

1 - remote
2 - virtual
3 - protection

49
Q

what is the use case of the virtual proxy?

A

caching or lazy loading , where u have an expensive operation and a cheap one and u actually want the cheap one so we call the proxy that is responsible for the original one (it has the same interface) and the proxy determines how to retrieve that cheap operation

50
Q

what is the intent of the bridge pattern?

A

is to decouple an abstraction from it’s implementation so that the two can vary independently

51
Q

what is a cartesian product?

A

it’s a set derived from two other sets

52
Q

what is the use case of bridge patterns?

A

we want to pair any number of an abstraction with any number of another abstraction

53
Q

what are structural patterns?

A
1 - decorator
2 - adapter
3 - facade
4 - proxy
5 - bridge
54
Q

what is the template method pattern?

A

it defines a skeleton of an algorithm in an operation deferring some steps to subclasses , template method lets subclasses redefine certain steps of an algorithm without changing the algorithm structure

55
Q

what is the use case of template method?

A

to save or validate orm objects into the database with using the abstract method implementation in the subclass to hook into the main save method or validate method

56
Q

what is the principle of the template method?

A

dependency inversion

57
Q

why do we use the composite pattern?

A

we want the user to not know whether he is dealing with a collection of objects or just one object and we treat every node or leaf in the tree hierarchy the same so we don’t have to use if nodes

58
Q

what is recursion in object oriented programming?

A

calling the same method but on different instances of the object or different objects with the same type

59
Q

what is a point in refactoring?

A

replace a conditional with polymorphism

60
Q

what is the definition of composite pattern?

A

composes object into tree structures to represent part whole hirearchy , composite let clients treat individual objects and composition of objects uniformly

61
Q

should we avoid mutation?

A

yes we want immutability

62
Q

what are the frameworks that use the composite pattern?

A

front end frameworks like react or angular

63
Q

what is the use case of composite pattern?

A

a todolist where u treat an entire object with ul as a normal todo list with just an li the same way

64
Q

what is the general structure of the composite pattern?

A

we have a single object and a composite where a composite is a single object (behaves like a single object) but also contains a single object

65
Q

what are the benefits of iterator pattern?

A

1 - you don’t have to expose your datastructure
2 - lazy loading
3 - keep track of iteration and pause and continue

i give one item at a time not all at once

66
Q

what is the definition of the iterator pattern?

A

the iterator pattern defines a way to access the elements of an aggregate object sequentially without exposing it’s underline implementation

67
Q

what are the state machines?

A

they are memory less machines that make decisions based on where they are currently and not based on how they got there

68
Q

what is the definition of state pattern?

A

allows an object to alter it’s behavior when it’s internal state changes the object will appear to change class

69
Q

what is the problem with null?

A

the conditional to check whether the object is null or not