Chapter 2: Design Patterns + Principles Flashcards

1
Q

Functional Interface

A

Marked with @FunctionalInterface. Should contain 1 abstract method only. You can however have other static or default methods. Also make sure it is an interface, not an abstract class.

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

Extending a functional interface

A

If you extend a functional interface and don’t declare any new methods, then it will also be a functional interface.

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

Lambda rules

A
  • If 1 parameter mentions data type, they all must do.
  • In your Lambda body, you can’t re-declare a variable name
  • If you’re not returning anything need {} on RHS (can’t just be blank)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Polymorphism

A

You can pass an instance of a class to anything which expects an instance of its super class or interface it implements (Note: all objects are accessed by reference, so you never have direct access to the object in memory)

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

Casting rules in polymorphism

A
  • Casting to a super class doesn’t require an explicit cast
  • Casting to a subclass does require an explicit cast
  • Compile won’t allow casts to unrelated types
  • Can get ClassCastException if the object is not of the specified type
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Encapsulation

A
  • Use getters and setters and add constraints on them to ensure objects are created/modified correctly
  • Allows you to maintain certain invariants about the data
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Is-a relationship

A

Known as the inheritance test. Class A extends B, so A is-a B. But be careful of any super classes as well in case the inheritance doesn’t make sense.

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

Has-a relationship

A

Object composition test. Used when an object has another object, which is included as a member of the object class.

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

Singleton Pattern

A
  • Creates a single object in memory
  • Private static variable with name instance
  • Access via public static getInstance()
  • All other constructors are marked as private which means it is effectively final
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Singleton lazy initialisation

A
  • Delays instantiation until the first .getInstance() call.
  • Means it takes less time to load up the application
  • But may not be thread-safe
  • Can use synchronized keyword to ensure that difference objects aren’t created in memory.
  • Can use the volatile keyword and add a synchronized block to check if object is null
  • Volatile keyword in Java - it will write it to main memory, not just to CPU cache.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Immutable Objects

A
  • Constructor to set everything
  • Instance variables private + final
  • No setters
  • Don’t allow mutable objects to be accessed directly
  • Prevent methods from being overridden
  • Need to ensure that when someone is passing you a mutable object to a constructor that you create a copy of it, otherwise the caller might still have a reference to the same object and can change something later on.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Builder

A

Should be in the same package or as a static inner class. If it’s in the same file as an inner class, then the constructor can be final, forcing you to use the Builder.

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