Object Creation and Destruction Flashcards

1
Q

Describe static factory method and its benefits and drawbacks.

A
  • Static factory methods are a great alternative to standard constructor for the following reasons:
    1) They have descriptive names. In addition, you can distinguish between factory methods with the same parameter using descriptive names that you can’t do with constructors.
    2) You can create instance-controlled classes using static factory methods by having the method return the same instance of class if the instance is immutable. This can help performance and guarantees that a.equals(b) only if a == b
    3) You can encapsulate and hide implementation of interfaces by declaring static factory method returning an interface. The implementation itself can be nonpublic and user does not need to know about it. If you are trying to do this on interface, which cannot have method, you usually use the interface and make it plural and make in an uninstantiable class. For example, the Collections class in java api.
  • Drawback with static factory method is that private constructor means the class cannot be inherited. In addition, static factory method do not stand out among other methods, so it has to be made clear in documentation how to instantiate a class.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Describe the Builder pattern and its benefits and drawbacks.

A
  • There are time when you have a class that need a lot of parameters, a small number being required while others are optional.
  • Traditionally, developers provide telescoping constructors - one constructor only with required parameters and a large number of others that allow client to specify optional parameters. This is very hard to read and use.
  • Alternatively, developers may use javabean pattern where you construct empty object and set fields one by one. Problem with this is that object maybe used when it’s in an inconsistent state. Furthermore, there’s no way to make object immutable.
  • Builder pattern solve both problems by making constructor easy to read and can verify that parameters passed in is consistent.
  • In addition, you can think of builder object as carrier of information needed to construct an object. You can use it in abstract factory pattern, passing builder in as parameter, for example.
  • Drawback of this pattern is that it can be verbose if only small number of fields is required.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Describe how to enforce singleton property.

A
  • This is done by making the constructor private and either provide a static factory method that returns a final static field. Alternatively, the final static field can be made public and used directly.
  • Note that singletons make clients that use singleton hard to test if there is no interface. Try to provide interface if there’s a need for singleton.
  • If the singleton should be serializable, all fields must be marked transient and readResolve method should be overridden to return the final static field. Otherwise, every time we deserialize, a new object gets created!
  • Alternative to using private constructor and static field is to use enum. This is actually easier if no need for testing.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Describe how to enforce noninstantiable classes.

A
  • Some classes are used as collection of static methods like static factory class and should not be instantiated.
  • Enforce this by making the constructor private
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Describe when it’s unnecessary to create new objects and how to reuse objects.

A
  • Sometimes you create immutable objects. In that case, you can reuse the same object multiple times instead of creating new one every time.
  • In addition, if an object is mutable, but we never actually change its value, we can also create an object only once and reuse it multiple times. For example, you may have a start date and end date that you want to compare to. Those date never change, so no need to construct new in method every time it’s called.
  • You reuse the same object by putting it in a final static field that gets returned or reused multiple times.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Describe how to avoid and when to avoid memory leaks.

A
  • Sometimes you have classes that manages its own memory.
  • For example, you may have an array with a portion being used and another portion not being used, demarcated by a size variable. If you decrement the size variable without nulling out reference. to object outside of size variable, that object will never be garbage collected!
  • Another example is cache or callbacks. Some object may remain in cache or callback much longer than it is useful. It’s a good idea to consider how will these obsolete references ever be removed. A good way is through weakhashmap,which gets rid of objects where there are no external references to key automatically.
  • Every time you have class that manages its own memory, beware of memory leaks!
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Explain why finalizers are bad and alternative to finalizers

A
  • Finalizers run on a separate thread and is not guaranteed to be run in a timely manner. Classes with finalizers are often hit with a huge performance problem because a lot of objects maybe waiting for finalizer to run and cannot be garbage collected.
  • In fact, finalizers are not guaranteed to run at all, so don’t put anything that absolutely must run here.
  • For resources that must be closed, provide a method to close the resource that client must call explicitly instead of providing finalizer. Finalizer maybe provided as safety-net, but remember performance problem above.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly