Creating and Destroying Objects Flashcards

Creating and Destroying Objects

1
Q

Creating and Destroying Objects

A

We should be aware of when and how to create
them, when and how to avoid creating them, how to ensure they are destroyed in a
timely manner, and how to manage any cleanup actions that must precede their
destruction.

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

Item 1: Consider static factory methods instead of constructors

A
The traditional way for a class to allow a client to obtain an instance is to provide
a public constructor. There is another technique that should be a part of every
programmer’s toolkit. A class can provide a public static factory method, which is
simply a static method that returns an instance of the class.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Advantage of static factory methods

A

One advantage of static factory methods is that, unlike constructors, they
have names.

A second advantage of static factory methods is that, unlike constructors,
they are not required to create a new object each time they’re invoked.

A third advantage of static factory methods is that, unlike constructors,
they can return an object of any subtype of their return type.

A fourth advantage of static factories is that the class of the returned
object can vary from call to call as a function of the input parameters. 
A fifth advantage of static factories is that the class of the returned object
need not exist when the class containing the method is written.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Limitations of static factory methods

A

The main limitation of providing only static factory methods is that
classes without public or protected constructors cannot be subclassed.

A second shortcoming of static factory methods is that they are hard for
programmers to find.

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

Service provider frameworks,

A

Such flexible
static factory methods form the basis of service provider frameworks, like the Java
Database Connectivity API (JDBC). A service provider framework is a system in
which providers implement a service, and the system makes the implementations
available to clients, decoupling the clients from the implementations.
There are three essential components in a service provider framework: a
service interface, which represents an implementation; a provider registration
API, which providers use to register implementations; and a service access API,
which clients use to obtain instances of the service. The service access API may
allow clients to specify criteria for choosing an implementation. In the absence of
such criteria, the API returns an instance of a default implementation, or allows
the client to cycle through all available implementations. The service access API
is the flexible static factory that forms the basis of the service provider framework.
An optional fourth component of a service provider framework is a service
provider interface, which describes a factory object that produces instances of the
service interface.

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