Reading 6 Chaper 5 Questions

estrapadesherbetSoftware and s/w Development

Nov 18, 2013 (3 years and 6 months ago)

75 views

Reading
6



Chaper
5

Questions


Read pages 1
69
-
1
87

and then type or write answers to these questions:


1.

How do you create a global variable in Java?

2.

Page 170 says that a downside to a global variable in Java is that you have to create the object when the
p
rogram first begins. Technically, that is not true. You can declare in class A, static B b. Then, anytime
later (anywhere) you can instantiate b, with code like this: A.b = new B(). But that defeats the purpose
of having a global variable. Why?

3.

Study the s
ingleton code on page 173 very carefully. Understand it completely. Suppose you need a
Log

class to store data about access attempts on any system mass storage device. The class should have
a
addTo

method and a
write

method. Since the log will be utilized
system wide, it need to be a
singleton. Write the code for the
Log

class trying hard to rely on your memory of how it works and not
peeking at the text unless you have to.

4.

On page 177, what does this mean, “...the Singleton is created in a lazy manner...?”

What are the
practical implications?

5.

While we say that lazy creation is advantage, once we the first request is made, the singleton exists
forever. Suppose this singleton occupied a large amount of memory which causes a slow
-
down in the
system when it is
not needed. Often, long stretches of time go by when the singleton is not needed.
Then, it may get hundreds of hits in a brief time. What change could you make to the classic singleton
design to help this situation.

6.

Be the JVM
. Explain why the approach to
singleton presented so far, can fail with multiple threads.

7.

Why is the multiple threads problem a somewhat limited problem?

8.

How do we fix the multithreading problem? What is the disadvantage of this?

9.

Describe three ways to improve the multithreading single
ton.

10.

What does the volatile keywork in Java mean?

11.

Why do the authors warn against
simply making all methods and variables static?

12.

We have a new design principle in the first paragraph on page 185. What is it?

13.

What are the two responsibilities that the sing
leton has?

14.

In terms of the singleton pattern, why are global variables bad?