W9 Flashcards
(6 cards)
Dependency Injection
here is an unnecessary dependency that should be eliminated.
If class A is dependant on class B, you can eliminate the dependency on B by calling the B constructor in the class that contains an instance of A. Then we “inject” the B object into a method or constructor of A by making it a parameter. In the case of a method call, we can give the parameter a more abstract type (interface or super class) so that when A calls the method, it is not able to tell if the method was implemented in the B class or not.
Builder
You have a class, say it is called B, with many variables or otherwise complex structure where the rest of the program should not depend on the classes involved in creating a B. How do you make sure the B is created without introducing unnecessary dependencies?
The Builder class can create all of the variables and then put them inside a new instance of B (encapsulates the creation process) so that no other class has to be dependant on the variables of B. In other words, the B constructor and the constructors of its variables are all called by the Builder class, not any other class, eliminating the need for dependencies between the rest of the program and the classes required in the construction of an object of type B.
Strategy
(ii) There is more than one algorithm or “way” of doing something and you don’t know which one you want to use until the program starts running.
(iii) Create an abstraction (interface or abstract class) for the Strategy and then implement it or extend it with specific Strategy sub- or implementing classes. Then include a variable of the abstract Strategy type in the class where the algorithm’s method will be called. That way, you can inject whichever strategy is needed.
Observer
(ii) There is a cause and an effect in different parts of the program. We want to
connect them without locating them in the same class or classes that depend on each
other.
(iii) Using inheritance (the old way in Java), the “cause” object inherits from a
class called Observable. Then you add the observer (“effect”) classes to the
Observable part. When the “cause” occurs in the subclass of Observable, the
Observable part of that object calls the “update” method in each Observer class
which triggers the “effect(s)”.
Using composition, we can put a PropertyChangeSupport object in whatever object is
being listened too, then add PropertyChangeListeners to the PropertyChangeSupport
object. When the property changes (the “cause”), the PropertyChangeSupport object
notifies the listener objects who can then trigger their respective “effects” in
whichever class is reacting to the change.
Adapter
ii) You have a class from a previous project or different piece of software that
you want to use, but it doesn’t quite have the proper public methods that you need.
(iii) Using inheritance, you can create a subclass that extends the old class and
includes the missing methods.
Using composition, you can create a container class that has an instance of the old
class as a variable. The rest of the program can call the container’s methods,
which then call the old class’s methods.
Façade
(ii) A class has too many sources of potential change. In other words, it violates
the Single Responsibility Principle.
(iii) Empty the logic from that class into their own new classes. Then include
those new classes as variables inside the original class.