1/43
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
Deisgn patterns are pieces of code that you can reference and copy into your program to solve common problems.
False
While Design Patterns are high-level structural descriptions of a solution, a(n) _______________________ defines a clear set of actions that can achieve some goal. If the latter is a cooking recipe, the former is a blueprint.
algorithm
Which of the following are usually present in a pattern description?
Structure of classes in the pattern.
Code Example of the pattern in action.
Intent of the pattern where the problem and solution are briefly described.
Motivation for using the pattern, describing the problem in more detail.
The concept of "Patterns" is often attributed to Christopher Alexander in a book discussing architectural patterns for buildings and neighborhoods.
True
The most famous book on Patterns in programming , Design Patterns: Elements of Reusable Object-Oriented Software, was published in 1994, by Erich Gamma, John Vlissides, Ralph Johnson, and Richard Helm. This is know is the "Gang of Four" or GoF book.
True
Which of the following are advantages of using patterns?
Patterns offer _________ solutions to common software problems and they tend to reinforce good object-oriented design thinking.
Pattern names are _______ _______ when talking to other developers. It is much easier to use _____ _____ than to describe the different interfaces and classes involved and what they will do--especially when it is likely that the other developer already knows what the Observer pattern means.
"tried and true"
effective shorthand
"Observer Pattern"
Which of the following are typical arguments against patterns?
Patterns can be ____ ____ (____ __ ____) who have limited tool sets, and so look for places to apply patterns when simpler code would be equally effective.
Patterns are meant to be a general tool adapted to a specific context, but sometimes get implemented _____ ___, awkwardly and inefficiently, without that adaptation to the context.
Patterns ____ ___ ___ ______ by providing complicated ways to solve problems that other languages are better suited to solve and can solve more easily.
over-used (especially by rookies)
"textbook" style
prop up bad languages
A. Compare and Contrast the Decorator and Adapter Patterns.
1. Both the Adapter and the Decorator patterns
2. The Adapter pattern wraps one object's interface with a different interface
3. The Decorator pattern wraps one interface with an object of the same interface
use use wrappers to augment an objects functionality.
in order to get incompatible systems to work together.
in order to create different functional variations of the same type of object.
Compare and Contrast the Template Method with the Abstract Factory Method.
Both the Template Method and the Abstract Factory break down a series of
The Template Method is used to combine similar unchanging
The Abstract Factory is used to create similar families of objects
steps in abstractions that are managed by their own subclasses.
tasks in the super-template, and to control the order in which the steps are called.
and to separate the production of objects from their constructors.
Compare and Contrast the Push and Pull variations of the Observer Pattern.
Both methods have a subject and an observer who needs to be
With the Push method,
With the Pull method,
The pull method has tighter coupling,
updated when the subject's state changes.
the subject pushes all of its relevant data out to its observers whenever its state changes.
the subject notifies all observers that there has been a change, and the observers must then pull the information from the subject.
because the observers need references to their subjects.
Compare and contrast the Simple Factory, Factory Method, and Abstract Factory Patterns.
The Simple Factory merely separates the creation of an object from the class itself.
The Factory Method makes that factory class into an Interface,
It creates a class (a factory) that returns objects that implement a certain interface.
with Concrete sub-factories to create different types of objects.
The Abstract Factory adds the creation of different related objects into the super-type factory.
List and Describe three different uses for the Command Pattern:
____ perhaps you process them in batches for efficiency, so you need to queue the commands up and keep track of old commands so that they can be processed later.
You need to keep track of operations because you want
These other entities need to pass the command on, but they don't need to know the details needed for execution.
You have asynchronous processing of commands--
to undo them one by one later.
The caller of the function and the executor of the function are separated by various intermediaries.
Explain the Template Method Pattern and how some variations can maintain both flexibility and stability.
The Template Method Pattern divides an algorithm or method (the "Template") into discrete steps, some of which are implemented, and some of which are abstract so sub-classes will implement them.
Sometimes, "Hooks" are introduced as functions in the Template Method. These are functions that the Template implements, but have no body, so that they are effectively skipped if not needed. However, they allow sub-classes to introduce steps at particular places in the algorithm if they are needed in the future.
On the other hand, sometimes the template method or some of the sub-steps are marked "final" so that no sub-class can interfere with an important step or, in the case of the template, override the overall order of method calls.
Name and explain the meaning behind each letter of the SOLID Principles:
Single Responsibility Principle, Open-Close Principle, Liskov Substitution Principle, Interface Segregation Principle, Dependency Inversion Principle.
Single Responsibility Principle
A software design principle stating that a class should have only one reason to change, meaning it should only have one job or responsibility.
Open-Close Principle
Open for extension, closed for modification
Liskov Substitution Principle
Child should be able to do what the parent classes do
Interface Segregation Principle
Don’t depend on what you don’t need
Dependency Inversion Principle
Depend on abstractions, not concretions.
Strategy [Problem Pattern Addresses]
You have variations of an algorithm or "how to complete a task," and you want to be able to switch between them at runtime.
Factory [Problem Pattern Addresses]
You want to separate the creation of objects from the code that uses the object.
Adapter [Problem Pattern Addresses]
You have two interfaces that you want to connect with each other; a connection that otherwise would not be possible or practical.
Facade [Problem Pattern Addresses]
You need your system to interact with another complex and complicated system--perhaps one whose updates are out of your control.
Decorator [Problem Pattern Addresses]
You have a large number of optional functionalities you want to be able to add to a class. To solve the problem with subclassing would create too many subclasses.
Observer [Problem Pattern Addresses]
You have an entity in your program that needs to respond when another entity's state changes.
Command [Problem Pattern Addresses]
You need to separate the call to execute from the code that executes.
Template [Problem Pattern Addresses]
You have multiple classes taking similar data or performing similar actions that could be abstracted up into a super-class, while still allowing the sub-classes to override specific parts of the process.
Strategy [Design Pattern Description]
Use an interface to abstract the general algorithm. Then use different objects implementing that interface to represent the different versions of the algorithm. Finally, the context will hold a variable of the Interface type, switching between different implementers as needed.
Factory [Design Pattern Description]
You abstract the construction of an object or family of objects into an interface, so different concrete subclasses can make their objects according to their own needs.
Adapter [Design Pattern Description]
Implement the one interface that will wrap another incompatible one, so that commands to Interface One can be translated by the wrapper into command to Interface Two.
Facade [Design Pattern Description]
Create a simplified interface for that complicated other system and place it in a separate class. On your end, the new interface will only present the methods and operations you need to call. However, it will manage all the references and method calls to the objects in the complicated system.
Decorator [Design Pattern Description]
Create an interface representing the general type of object. You create both concrete versions of the interface, and "Wrapper" versions. The Wrapper versions contain references to another object of the Interface type and merely add their functionality to the method calls when needed.
Observer [Design Pattern Description]
Create an Interface that encapsulates the change notification. All watchers implement that interface. The Subject can then register all the watchers and send them the change notifications when appropriate.
Command [Design Pattern Description]
Encapsulate a request into a stand-alone object that contains all the details needed for the executor to perform the operation.
Template [Design Pattern Description]
Create a skeleton of the algorithm in an abstract superclass, dividing the steps into separate functions. Provide implementations for the steps that are common among the variations, and make the others abstract. Subclasses will override the abstract steps.
Strategy [Examples from the Java Standard Library]
LayoutManager
All functional interfaces are examples of the strategy pattern.
Executor Framework (used with concurrent programming. Executors are different strategies for allocating threads for execution.)
Factory [Examples from the Java Standard Library]
NumberFormat
DateFormat
BorderFactory
Adapter [Examples from the Java Standard Library]
Enumerator/Iterator (walk through specific concrete classes but get used by regular abstractions like enhanced for-loops)
Collection.list()
Arrays.asList (adapts Arrays into ArrayLists)
Facade [Examples from the Java Standard Library]
java.net.URL hides the complexity behind the lower-level classes that handle network communication
Observer Push [Examples from the Java Standard Library]
Observer,
Observable (?)
EventListener
ActionListener
Observer Pull [Examples from the Java Standard Library]
ActionListener could implement the pull method. (The ActionEvent sent to the observer has a reference to the subject retrievable with getSource(), so the Oberserver can go back to the subject and get information.)
Command [Examples from the Java Standard Library]
The Runnable Interface. These allow you to create an object that has the "run" command, which can then be passed to a Thread to execute.
Template [Examples from the Java Standard Library]
AbstractList.get() and .set()
Collections.sort()