Effective Java


A better approach to object copying is to



Download 2,19 Mb.
Pdf ko'rish
bet67/341
Sana11.07.2022
Hajmi2,19 Mb.
#776765
1   ...   63   64   65   66   67   68   69   70   ...   341
Bog'liq
Effective Java

A better approach to object copying is to
provide a 
copy constructor 
or
 copy factory
.
A copy constructor is simply a
constructor that takes a single argument whose type is the class containing the
constructor, for example,
// Copy constructor
public Yum(Yum yum) { ... };
A copy factory is the static factory (Item 1) analogue of a copy constructor:
// Copy factory
public static Yum newInstance(Yum yum) { ... };
The copy constructor approach and its static factory variant have many
advantages over 
Cloneable
/
clone
: they don’t rely on a risk-prone extralinguistic
object creation mechanism; they don’t demand unenforceable adherence to thinly
documented conventions; they don’t conflict with the proper use of final fields;
they don’t throw unnecessary checked exceptions; and they don’t require casts.
Furthermore, a copy constructor or factory can take an argument whose type
is an interface implemented by the class. For example, by convention all general-
purpose collection implementations provide a constructor whose argument is of
type 
Collection
or 
Map
. Interface-based copy constructors and factories, more
properly known as 
conversion constructors
and 
conversion factories
, allow the
client to choose the implementation type of the copy rather than forcing the client
to accept the implementation type of the original. For example, suppose you have

HashSet

s
, and you want to copy it as a 
TreeSet
. The 
clone
method can’t offer
this functionality, but it’s easy with a conversion constructor: 
new
TreeSet<>(s)
.
Given all the problems associated with 
Cloneable
, new interfaces should not
extend it, and new extendable classes should not implement it. While it’s less
harmful for final classes to implement 
Cloneable
, this should be viewed as a per-
formance optimization, reserved for the rare cases where it is justified (Item 67).
As a rule, copy functionality is best provided by constructors or factories. A nota-
ble exception to this rule is arrays, which are best copied with the clone method.


CHAPTER 3
METHODS COMMON TO ALL OBJECTS
66

Download 2,19 Mb.

Do'stlaringiz bilan baham:
1   ...   63   64   65   66   67   68   69   70   ...   341




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish