Good Constructors Flashcards

1
Q

What are the two problems with common constructors?

A
  1. Too many (optional) parameters

2. Magic numbers

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

What are the four most common constructor techniques?

A
  1. Builder
  2. Static Factory Method
  3. Telescoping
  4. Java Bean
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How is the builder pattern implemented?

A

Public static inner class contains mandatory and. optional variables. Builder constructor with only mandatory parameters.

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

How do you use a builder constructor?

A

A = new A.Builder(100,10)
.y(1)
.y(2)
.build();

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

What are static methods?

A

An instance of the class needs to be created in order to call them.

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

How do static factory methods work as a constructor?

A

Static methods return different instances of the class

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

How are static factory methods implemented and used?

A
public static A newInstance(){
		return new A();
	}

A a = A.create();

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

What are telescoping constructors?

A

Various constructors of different sizes. Smaller ones call the next bigger one.

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

What is reflection in java?

A

A programm can examine a class definition and use it.

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

How are JavaBeans used as a constructor?

A
  1. Create empty object with constructor

2. set parameters with setters

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

When would you use a builder and when a static factory method?

A

Builder: > 5 Parameters
SFM: < 5 Parameters

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