Head First Design Patterns Flashcards
Strategy Design pattern story
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
what do we preceive about inheritance?
that it’s a great purpose for reuse but it doesn’t work for maintenance
what is the one thing that is constant in all programming?
that overtime the application grows and changes or it will die
what is the design principle for inheritance?
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
what are the benefits of encapsulating the parts the vary?
fewer unintended consequences from code changes and more flexibility in your system
design pattern that help us while designing the duck behaviors?
program to an interface not an implementation
why did we use an interface and not an abstract class isn’t the whole point to use polymorphism?
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
what do we break when we manually in the constructor initialize the object?
we are programming to an implementation not an interface
how to set the reference of the object or the behavior and functionality of it dynamically during runtime?
buy implementing setter and getter methods
how should u start thinking about the set of behaviors?
as they are a family of algorithms that as an algorithm represent something that a class would do
what is better Has-A or IS-A
HAS-A
what is the design principle for using compostion?
favor composition over inheritance
why is composition better than inheritance?
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
should u spend time on reuse or maintainability?
as most of the coding is done after the software is ready due to change so we should focus on maintainability
what is the definition of the strategy design patterns?
defines a family of algorithms, encapsulates each one , and makes them interchangeable.
strategy lets the algorithm vary independently from clients that use it
what is a common misconception about design patterns?
that by knowing good OO Basics u will be able to construct reusable ,flexible , maintainable systems
Observer Design Pattern Story?
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
definition of the observer pattern?
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
what do u mean by loose coupling?
two objects are loosely coupled when they can interact but have very little knowledge of eachother
what is the design principle for loose coupling?
strive for loosely coupled designs between objects that interact
what should u never depend on while using the observer pattern?
the order for the observers
what is the open-closed principle?
classes should be closed for modification but open for extension
should u apply open-closed principle everywhere in your code?
no just to the parts that might change otherwise ur code will become complicated
what is the story for the decorator pattern?
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
what is the definition of the decorator pattern?
it attaches additional responsibilities to an object dynamically .Decorators provide a flexible alternative to subclassing for extending functionality
how should you use inhertiance?
not to get behavior but to achieve type matching
what is wrong with “new”?
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