design patterns Flashcards

(12 cards)

1
Q

factory

A

a design pattern used when a superclass has multiple subclasses that may be returned; lets a class defer instantiation to a subclass

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

when to use factory design pattern

A

if a class cannot predic t the type of object that needs to be created;
if a class wants its subclasses to specify the objects created;
if classes delegate responsibility to one of the helper subclasses

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

design pattern

A

software design incorporating best practices for a given item; well-described solutions to the most common problems during software development

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

singleton

A

ensures a JVM contains only one instance of an object; checks if an instance has been created, makes a new one if not, returns the one instance

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

adapter

A

“glues” two classes together so they are compatible; adds behavior to an object without modifying others; use when class 1 and class 2 have different methods

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

subject-observer

A

a subject/publisher notifies observers/subscribers when something changes;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

maven

A

manage libraries, compile and package code, run tests

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

spring-boot

A

framework for building web applications

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

pub-sub example code

A

interface Observer {
void update(String message);
}

class Subscriber implements Observer {
public void update(String message) {
System.out.println(“Got update: “ + message);
}
}

class Channel {
private List<Observer> subscribers = new ArrayList<>();</Observer>

void subscribe(Observer o) {
    subscribers.add(o);
}

void notifyObservers(String msg) {
    for (Observer o : subscribers) {
        o.update(msg);
    }
} } Channel yt = new Channel(); Observer alice = new Subscriber(); yt.subscribe(alice); yt.notifyObservers("New video posted!");
  • def observer
  • def subscriber class, implementing observer
  • both have void update() but in subscriber, update() prints the message
  • keep list of subscribers to send message to
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

adapter example code

A

interface TargetInterface {
void request(); // Method expected by Class1
}

class Class2 {
void specificRequest() {
System.out.println(“Called specificRequest from Class2”); //method expected by class2
}
}

class AdapterClass implements TargetInterface {
private Class2 adaptee;

public AdapterClass(Class2 adaptee) {
    this.adaptee = adaptee;
}

@Override
public void request() {
    adaptee.specificRequest();  // Redirect call
} }
  • when an existing class can be reused but doesn’t implement the interface that needs to be used, write an adapter that calls the interface’s method when prompted by the class’s method
  • open to extension, closed to modification
  • interface1 contains m1, class2 contains m2
  • class adapterClass implements interface1
  • new instance within adapterClass
  • public void m1 {adapterobject.m2();}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

singleton example code

A

s1 and s2 are the same object

class Singleton {
private static Singleton instance;

private Singleton() {} // private constructor

public static Singleton getInstance() { //return existing instance
    if (instance == null) { //if existing instance not found
        instance = new Singleton(); //make one
    }
    return instance; //return existing instance, now that you're sure it exists; there will only be one
} } Singleton s1 = Singleton.getInstance(); Singleton s2 = Singleton.getInstance();
  • private static Singleton instance; private Singleton()
  • call a method publicly to getInstance
  • if instance == null (DNE), make one; now return instance
  • every time something runs getInstance, it will return the same and only instance existing
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

unittest example

A

public class Calculator {
public int add(int a, int b) {
return a + b;
}
}

import org.junit.Test;
import static org.junit.Assert.*;

public class CalculatorTest {

@Test
public void testAdd() {
    Calculator calc = new Calculator();
    int result = calc.add(2, 3);
    assertEquals(5, result);
} }
  • import org.junit.Test AND org.junit.Assert
  • @Test public void testClassName
  • create new instance
  • declare var = class.method(args)
  • assertEquals (rawExpectedresult, var)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly