Java Design Patterns Exam 4

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/43

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

44 Terms

1
New cards

Deisgn patterns are pieces of code that you can reference and copy into your program to solve common problems.

False

2
New cards

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

3
New cards

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.

4
New cards

The concept of "Patterns" is often attributed to Christopher Alexander in a book discussing architectural patterns for buildings and neighborhoods.

True

5
New cards

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

6
New cards

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"

7
New cards

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

8
New cards

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

  1. use use wrappers to augment an objects functionality.

  2. in order to get incompatible systems to work together.

  3. in order to create different functional variations of the same type of object.

9
New cards

Compare and Contrast the Template Method with the Abstract Factory Method.

  1. Both the Template Method and the Abstract Factory break down a series of

  2. The Template Method is used to combine similar unchanging

  3. The Abstract Factory is used to create similar families of objects

  1. steps in abstractions that are managed by their own subclasses.

  2. tasks in the super-template, and to control the order in which the steps are called.

  3. and to separate the production of objects from their constructors.

10
New cards

Compare and Contrast the Push and Pull variations of the Observer Pattern.

  1. Both methods have a subject and an observer who needs to be

  2. With the Push method,

  3. With the Pull method,

  4. The pull method has tighter coupling,

  1. updated when the subject's state changes.

  2. the subject pushes all of its relevant data out to its observers whenever its state changes.

  3. the subject notifies all observers that there has been a change, and the observers must then pull the information from the subject. 

  4. because the observers need references to their subjects. 

11
New cards

Compare and contrast the Simple Factory, Factory Method, and Abstract Factory Patterns.

  1. The Simple Factory merely separates the creation of an object from the class itself. 

  2. The Factory Method makes that factory class into an Interface,

  1. It creates a class (a factory) that returns objects that implement a certain interface. 

  2. with Concrete sub-factories to create different types of objects. 

  3. The Abstract Factory adds the creation of different related objects into the super-type factory. 

12
New cards

List and Describe three different uses for the Command Pattern:

  1. ____ 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.

  2. You need to keep track of operations because you want

  3. These other entities need to pass the command on, but they don't need to know the details needed for execution.

  1. You have asynchronous processing of commands--

  2. to undo them one by one later.

  3. The caller of the function and the executor of the function are separated by various intermediaries. 

13
New cards

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.

14
New cards

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.

15
New cards

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.

16
New cards

Open-Close Principle

Open for extension, closed for modification

17
New cards

Liskov Substitution Principle

Child should be able to do what the parent classes do

18
New cards

Interface Segregation Principle

Don’t depend on what you don’t need

19
New cards

Dependency Inversion Principle

Depend on abstractions, not concretions.

20
New cards

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.

21
New cards

Factory [Problem Pattern Addresses]

You want to separate the creation of objects from the code that uses the object. 

22
New cards

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.

23
New cards

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.

24
New cards

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.

25
New cards

Observer [Problem Pattern Addresses]

You have an entity in your program that needs to respond when another entity's state changes.

26
New cards

Command [Problem Pattern Addresses]

You need to separate the call to execute from the code that executes.

27
New cards

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.

28
New cards

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.

29
New cards

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.

30
New cards

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.  

31
New cards

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. 

32
New cards

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.

33
New cards

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.

34
New cards

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.

35
New cards

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. 

36
New cards

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.)

37
New cards

Factory [Examples from the Java Standard Library]

NumberFormat

DateFormat

BorderFactory

38
New cards

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)

39
New cards

Facade [Examples from the Java Standard Library]

java.net.URL hides the complexity behind the lower-level classes that handle network communication

40
New cards

Decorator [Examples from the Java Standard Library]

java.io.BufferedReader

java.io.FileReader

ScrollPane in Swing is an example--it has the same interface as any other Component, but it "wraps" a Component with scrollbars.

41
New cards

Observer Push [Examples from the Java Standard Library]

Observer,

Observable (?)

EventListener

ActionListener

42
New cards

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.)

43
New cards

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.

44
New cards

Template [Examples from the Java Standard Library]

AbstractList.get() and .set()

Collections.sort()