Shared Flashcard Set

Details

Design Patterns
Design Patterns
47
Computer Science
Graduate
05/09/2019

Additional Computer Science Flashcards

 


 

Cards

Term
Describes a problem which occurs over and over again in our environment
Describes the core of the solution to that problem
Use this solution a million times over, without ever doing it the same way twice
Definition
design pattern
Term
What are the design pattern catalogs?
Definition
creational patterns, structural patterns, behavioral patterns
Term
Abstract the instantiation process
Make a system independent of how its objects are created, composed, and represented
Definition
creational patterns
Term
Abstract factory and singleton are examples of _____ patterns
Definition
creational
Term
Concern with how classes and objects are composed to form a larger structure
Use inheritance to compose interfaces or implementations
Definition
structural patterns
Term
Façade and proxy are examples of _____ patterns
Definition
structural
Term
Concern with algorithms and the assignment of responsibilities between objects
Describe patterns of communication between objects or class
Definition
behavioral patterns
Term
Iterator and chain of responsibility are examples of _____ patterns
Definition
behavioral
Term
Abstract factory, builder, factory method, prototype, and singleton are examples of _____ patterns
Definition
creational
Term
Provide an interface (e.g.: interface or abstract class) for creating families of related or dependent product objects without specifying their concrete classes
Definition
abstract factory
Term
Abstract Factory declares an _____ for creating product objects. _____ Factories as singletons. Product objects are actually created within a _____ Factory
Definition
interface, Concrete, Concrete
Term
Give a code example of Abstract Factory
Definition
AbstractFactory concreteFactory1 = new ConcreteFactory1();
AbstractProductA productA1 = concreteFactory1.createProductA();
AbstractProductB productB1 = concreteFactory1.createProductB();
AbstractFactory concreteFactory2 = new ConcreteFactory2();
AbstractProductA productA2 = concreteFactory2.createProductA();
AbstractProductB productB2 = concreteFactory2.createProductB();
Term
Separate the construction of a complex object from its representation so that the same construction process can create different representation
Definition
builder
Term
Use an abstract class Builder that defines an operation for each component that a _____ may ask it to create
Use a subclass ConcreteBuilder to _____ operations defined in Builder abstract class
Definition
director, override
Term
Give a code example of Builder
Definition
public class Director {
public Product createProduct(Builder builder) {
builder.buildPartA();
builder.buildPartB();
return builder.getProduct();
}
}

public abstract class Builder {
protected Product product = new Product();
public abstract void buildPartA();
public abstract void buildPartB();
public abstract Product getProduct();
}

public class ConcreteBuilder1 extends Builder {
public void buildPartA() {
...
}
public void buildPartB() {
...
}
public Product getProduct() {
return product;
}
}

public class Client {
public static void main(String[] args) {
Director director = new Director();
Builder myBuilder = new ConcreteBuilder1();
Product result = director.createProduct(myBuilder);
}
}
Term
Define an interface for creating an object, but let subclasses decide which class to instantiate
Definition
factory method
Term
Abstract class Creator declares the _____ method, which returns an object of Product
Subclass ConcreteCreator overrides the _____ method to return an instance of a ConcreteProduct
Definition
factory, factory
Term
Give a code example of factory method
Definition
public abstract class Creator {
protected Product product = new Product();
public abstract Product factoryMethod();
}

public class ConcreteCreator1 extends Creator {
public Product factoryMethod() {
...
return product;
}
}

public class Client {
public static void main(String[] args) {
Creator myCreator = new ConcreteCreator1();
Product result = myCreator.factoryMethod();
}
}
Term
Specify the kinds of objects to create using a prototype instance, and create new objects by copying this prototype
Definition
prototype
Term
Abstract class _____ declares the clone method, which returns a copy of cloned object
Subclass _____ overrides the clone method to return a copy of itself
Definition
Prototype, ConcretePrototype
Term
Give a code example of prototype
Definition
public abstract class Prototype implements Cloneable {
public abstract Prototype clone();
}

public class ConcretePrototype1 extends Prototype implements Cloneable {
public Prototype clone() {
...
return new ConcretePrototype1();
}
}

public class Client {
public static void main(String[] args) {
Prototype myPrototype = new ConcretePrototype1();
Prototype result = myPrototype.clone();
}
}
Term
Ensure a class has only one instance, and provide a global point of access to it
Definition
singleton
Term
The following describes an implementation of _____
Define a getInstance operation that lets clients access its unique instance
Define a constructor with visibility protected or private
Define an instance attribute with scope static
Definition
singleton
Term
Give a code example of singleton
Definition
public class Singleton {
private static Singleton instance = null;

protected Singleton() {
...
}

public static Singleton getInstance() {
if(instance == null) {
instance = new Singleton();
}
return instance;
}
}
Term
Adapter, bridge, composite, decorator, façade, flyweight, and proxy are examples of _____ patterns
Definition
structural
Term
Convert the interface of a class into another interface clients expect
Let classes work together that could not otherwise because of incompatible interfaces
Definition
adapter
Term
Adapter implementation:
Interface Socket defines _____ operation
Subclass Adapter overrides _____ operation
Definition
request, request
Term
Give a code example of adapter
Definition
public interface Socket {
public String getOutput();
}

public class Plug {
private String specification = "5 AMP";
public String getInput() {
return specification;
}
}

public class Adapter implements Socket {
public String getOutput(int input) {
Plug plug = new Plug();
return plug.getInput();
}
}
Term
Decouple an abstraction from its implementation so that two can vary independently
Share an implementation among multiple objects
Definition
bridge
Term
Bride implementation:
_____ defines operations
_____ implements the operations defined in _____
Definition
Abraction, Implementor, Abstraction
Term
Give a code example of bridge
Definition
class QuestionManager {
protected Question questDB;
}

interface Question {
public void nextQuestion();
public void newQuestion(String q);
public void displayQuestion();
}

class QuestionFormat extends QuestionManager {
public void displayAll() {
...
}
}

class JavaQuestions implements Question {
public void nextQuestion() {
if(current <= questions.size() - 1) {
current++;
}
}
...
}

class TestBridge {
public static void main(String[] args) {
QuestionFormat questions = new QuestionFormat("Java Language");
questions.questDB = new JavaQuestions();
questions.display();
questions.next();
questions.newOne("What is object?");
questions.displayAll();
}
}
Term
Compose objects into tree structures
Let clients treat individual objects and compositions of objects uniformly
Definition
composite
Term
Composite implementation:
Class Component defines and implements _____ behavior
Subclass Composite defines behavior for components having _____
Subclass Composite implements _____-related operations in class Component
Definition
default, children, child
Term
Give a code example of composite
Definition
public class Employee {
private String name;
private double salary;
private Vector subordinates;
public Vector getSubordinates() {
return subordinates;
}
public void setSubordinates(Vector subordinates) {
this.subordinates = subordinates;
}
public Employee(String name, double sal) {
setName(name);
setSalary(sal);
subordinates = new Vector();
}
public void add(Employee e) {
subordinates.addElement(e);
}
public void remove(Employee e) {
subordinates.remove(e);
}
}

public class Composite extends Employee {
private void addEmployeesToTree() {
Employee CFO = new Employee("CFO", 300000);
Employee headFinance1 = new Employee("Head Finance. North Zone", 20000);
Employee headFinance2 = new Employee("Head Finance. West Zone", 22000);
Employee accountant1 = new Employee("Accountant1", 10000);
Employee accountant2 = new Employee("Accountant2", 9000);
Employee accountant3 = new Employee("Accountant3", 11000);
Employee accountant4 = new Employee("Accountant4", 12000);
CFO.add(headFinance1);
CFO.add(headFinance2);
headFinance1.add(accountant1);
headFinance1.add(accountant2);
headFinance2.add(accountant3);
headFinance2.add(accountant4);
}
}
Term
Attach additional responsibilities to an object dynamically
Provide a flexible alternative to subclasses for extending functionality
Definition
decorator
Term
Class _____ defines operations
Subclass _____ implements operations and adds more operations
Definition
Decorator, ConcreteDecorator
Term
Give a code example of decorator
Definition
public abstract class Decorator {
public abstract void place(Branch branch);
}

public class ChristmasTree {
private Branch branch;
public Branch getBranch() {
return branch;
}
}

public class BallDecorator extends Decorator {
public BallDecorator(ChristmasTree tree) {
Branch branch = tree.getBranch();
place(branch);
}
public void place(Branch branch) {
branch.put("ball");
}
}
Term
Provide a unified interface to a set of interfaces in a set of subsystems
Define a higher-level interface that makes the subsystems easier to use
Definition
facade
Term
Interface _____ defines a set of operations
Subclass within the subsystem implements the operations in the interface
Definition
facade
Term
Give a code example of facade
Definition
public interface Store {
public Goods getGoods();
}

public class FinishedGoodsStore implements Store {
public Goods getGoods() {
return new FinishedGoods();
}
}
Term
Use sharing to support large numbers of fine-grained objects efficiently
Definition
flyweight
Term
Class FlyweightFactory _____ and _____ flyweight objects
Interface Flyweight defines operations taking _____ state from a client
Subclass ConcreteFlyweight implements the Flyweight interface and adds storage for _____ state, if any
A ConcreteFlyweight object must be _____. Any state it stores must be intrinsic, that is, it must be independent of the ConcreteFlyweight object's context
Definition
creates, manages, extrinsic, intrinsic, shareable
Term
Give a code example of flyweight
Definition
class ShapeFactory {
private static final HashMap shapes = new HashMap();
public static MyShape getShape(String label) {
MyShape concreteShape = (MyShape)shapes.get(label);
if(concreteShape == null) {
if(label.equals("O")) {
concreteShape = new MyOval(label);
}
...
shapes.put(label, concreteShape);
}
return concreteShape;
}
}

public interface MyShape {
public void draw(Graphics g, int x, int y, int width, int height, Color color, boolean fill, String font);
}

class MyOval implements MyShape {
private String label;
public MyOval(String label) {
this.label = label;
}
public void draw(Graphics oval, ...) {
if(fill) {
oval.fillOval(x, y, width, height);
}
}
}
Term
Provide a substitute of placeholder for another object to control access to it
Postpone the creation until you need the actual object
Definition
proxy
Term
Class Subject defines the common interface for RealSubject and _____, so that a _____ can be used anywhere a RealSubject is expected
Class _____ maintains a reference that lets the _____ access the real subject
Definition
Proxy, Proxy, Proxy, proxy
Term
Give a code example of proxy
Definition
abstract class Graphic {
public abstract void load();
public abstract void draw();
}

class Image extends Graphic {
public void load() {
...
}
public void draw() {
...
}
}

class ImgProxy extends Graphic {
public void load() {
if(image == null) {
image = new Image(filename);
}
...
}
public void draw() {
...
}
}
Term
Chain of responsibility, command, interpreter, iterator, mediator, memento, observer, state, strategy, template method, and visitor are examples of _____ patterns
Definition
behavioral
Supporting users have an ad free experience!