ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • ์•„์ดํ…œ13. clone ์žฌ์ •์˜๋Š” ์ฃผ์˜ํ•ด์„œ ์ง„ํ–‰ํ•˜๋ผ
    DevBook/Effective Java 2023. 5. 7. 21:02

    ๐Ÿ“์ƒํ™ฉ

    • Cloneable์€ ๋ณต์ œํ•ด๋„ ๋˜๋Š” ํด๋ž˜์Šค์ž„์„ ๋ช…์‹œํ•˜๋Š” ์šฉ๋„์ธ mixin ์ธํ„ฐํŽ˜์ด์Šค์ง€๋งŒ, ์•„์‰ฝ๊ฒŒ๋„ ์˜๋„ํ•œ ๋ชฉ์ ์„ ์ œ๋Œ€๋กœ ์ด๋ฃจ์ง€ ๋ชปํ–ˆ์Œ
      • clone ๋ฉ”์„œ๋“œ๊ฐ€ ์„ ์–ธ๋œ ๊ณณ์ด Cloneable์ด ์•„๋‹Œ Object์ด๊ณ , protected๋กœ ์„ ์–ธ๋จ --> Cloneable์„ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ๋งŒ์œผ๋กœ๋Š” ์™ธ๋ถ€ ๊ฐ์ฒด์—์„œ clone ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์—†์Œ
      • ๋ฆฌํ”Œ๋ ‰์…˜์„ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, 100% ์„ฑ๊ณตํ•˜๋Š” ๊ฒƒ์€ ์•„๋‹˜ --> ํ•ด๋‹น ๊ฐ์ฒด๊ฐ€ ์ ‘๊ทผ์ด ํ—ˆ์šฉ๋œ clone ๋ฉ”์„œ๋“œ๋ฅผ ์ œ๊ณตํ•œ๋‹ค๋Š” ๋ณด์žฅ์ด ์—†๊ธฐ ๋•Œ๋ฌธ์ž„
    • ํ•˜์ง€๋งŒ Cloneable ๋ฐฉ์‹์€ ๋„๋ฆฌ ์“ฐ์ด๊ณ  ์žˆ์–ด ์ž˜ ์•Œ์•„๋‘๋Š” ๊ฒƒ์ด ์ข‹์Œ

     

    ์ด๋ฒˆ ์•„์ดํ…œ์—์„œ ๋‹ค๋ฃฐ ๋‚ด์šฉ 

    • clone ๋ฉ”์„œ๋“œ๋ฅผ ์ž˜ ๋™์ž‘ํ•˜๊ฒŒ๋” ํ•ด์ฃผ๋Š” ๊ตฌํ˜„ ๋ฐฉ๋ฒ•
    • ์–ธ์ œ ์žฌ์ •์˜ํ•ด์„œ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š”์ง€, ๊ฐ€๋Šฅํ•œ ๋‹ค๋ฅธ ์„ ํƒ์ง€๋Š” ๋ฌด์—‡์ธ์ง€

     

    ๐Ÿ“๋ฐฉ๋ฒ•

    <์‚ฌ์ „ ์ง€์‹>

    Cloneable ์ธํ„ฐํŽ˜์ด์Šค์˜ ์—ญํ• 

    • ํ•ด๋‹น ์ธํ„ฐํŽ˜์ด์Šค์—๋Š” ์•„๋ฌด๋Ÿฐ ๋ฉ”์„œ๋“œ๋„ ์ •์˜๋˜์–ด ์žˆ์ง€ ์•Š์Œ
    • Object์˜ protected ๋ฉ”์„œ๋“œ์ธ clone์˜ ๋™์ž‘ ๋ฐฉ์‹์„ ๊ฒฐ์ •ํ•จ
    • Cloneable์„ ๊ตฌํ˜„ํ•œ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค์—์„œ clone์„ ํ˜ธ์ถœํ•˜๋ฉด ๊ทธ ๊ฐ์ฒด์˜ ํ•„๋“œ๋“ค์„ ํ•˜๋‚˜ํ•˜๋‚˜ ๋ณต์‚ฌํ•œ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์€ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค์—์„œ ํ˜ธ์ถœํ•˜๋ฉด CloneNotSupportedException์„ ๋˜์ง
    • Cloneable์˜ ๊ฒฝ์šฐ์—๋Š” ์ƒ์œ„ ํด๋ž˜์Šค์— ์ •์˜๋œ protected ๋ฉ”์„œ๋“œ์˜ ๋™์ž‘ ๋ฐฉ์‹์„ ๋ณ€๊ฒฝํ•œ ๊ฒƒ์ž„
      • ์ด๋Š” ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ด๋ก€์ ์œผ๋กœ ์‚ฌ์šฉํ•œ ์˜ˆ์ด๋‹ˆ ๋”ฐ๋ผ ํ•˜์ง€ ๋ง์ž

     

    clone ๋ฉ”์„œ๋“œ์˜ ์ผ๋ฐ˜ ๊ทœ์•ฝ

     

    <์ œ๋Œ€๋กœ ๋™์ž‘ํ•˜๊ฒŒ๋” ํ•˜๋Š” ๊ตฌํ˜„ ๋ฐฉ๋ฒ•>

    1) ๊ฐ€๋ณ€ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•˜์ง€ ์•Š๋Š” ํด๋ž˜์Šค

    ์ฝ”๋“œ 13-1 ๊ฐ€๋ณ€ ์ƒํƒœ๋ฅผ ์ฐธ์กฐํ•˜์ง€ ์•Š๋Š” ํด๋ž˜์Šค์šฉ clone ๋ฉ”์„œ๋“œ

    @Override
    public PhoneNumber clone() {
        try {
        	return (PhoneNumber) super.clone();
        } catch (CloneNotSupportedException e) {
        	throw new AssertionError();
        }
    }
    • super.clone์„ ํ˜ธ์ถœํ•จ
      • ์ด๋ฅผ ํ†ตํ•ด ์–ป์€ ๊ฐ์ฒด๋Š” ์›๋ณธ์˜ ์™„๋ฒฝํ•œ ๋ณต์ œ๋ณธ์ž„
      • ํด๋ž˜์Šค์— ์ •์˜๋œ ๋ชจ๋“  ํ•„๋“œ๋Š” ์›๋ณธ ํ•„๋“œ์™€ ๋˜‘๊ฐ™์€ ๊ฐ’์„ ๊ฐ–์Œ
      • ๋ชจ๋“  ํ•„๋“œ๊ฐ€ ๊ธฐ๋ณธ ํƒ€์ž…์ด๊ฑฐ๋‚˜ ๋ถˆ๋ณ€ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•œ๋‹ค๋ฉด ์ด ๊ฐ์ฒด๋Š” ์™„๋ฒฝํžˆ ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ์ƒํƒœ์ž„
    • ํ•ด๋‹น ๋ฉ”์„œ๋“œ๊ฐ€ ๋™์ž‘ํ•˜๊ฒŒ ํ•˜๋ ค๋ฉด PhoneNumber ํด๋ž˜์Šค ์„ ์–ธ์— Cloneable์„ ๊ตฌํ˜„ํ•ด์•ผ ํ•จ

     

    2) ๊ฐ€๋ณ€ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•˜๋Š” ํด๋ž˜์Šค

    ์˜ˆ์‹œ - Stack ํด๋ž˜์Šค

    public class Stack {
        private Object[] elements;
        private int size = 0;
        private static final int DEFAULT_INITIAL_CAPACITY = 16;
        
        public Stack() {
        	this.elements = new Object[DEFAULT_INITIAL_CAPACITY];
        }
        
        ...
    }
    • clone ๋ฉ”์„œ๋“œ๊ฐ€ ๋‹จ์ˆœํžˆ super.clone์˜ ๊ฒฐ๊ณผ๋ฅผ ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค๋ฉด?
      • ๋ฐ˜ํ™˜๋œ Stack ์ธ์Šคํ„ด์Šค์˜ size ํ•„๋“œ๋Š” ์˜ฌ๋ฐ”๋ฅธ ๊ฐ’์„ ๊ฐ€์ง€์ง€๋งŒ, elements ํ•„๋“œ๋Š” ์›๋ณธ Stack ์ธ์Šคํ„ด์Šค์™€ ๋˜‘๊ฐ™์€ ๋ฐฐ์—ด์„ ์ฐธ์กฐํ•จ
      • ์›๋ณธ์ด๋‚˜ ๋ณต์ œ๋ณธ ์ค‘ ํ•˜๋‚˜๋ฅผ ์ˆ˜์ •ํ•˜๋ฉด ๋‹ค๋ฅธ ํ•˜๋‚˜๋„ ์ˆ˜์ •๋˜์–ด ๋ถˆ๋ณ€์‹์„ ํ•ด์นจ
      • ๋”ฐ๋ผ์„œ ํ”„๋กœ๊ทธ๋žจ์ด ์ด์ƒํ•˜๊ฒŒ ๋™์ž‘ํ•˜๊ฑฐ๋‚˜ NullPointerException์„ ๋˜์งˆ ๊ฒƒ์ž„
    • clone ๋ฉ”์„œ๋“œ๋Š” ์‚ฌ์‹ค์ƒ ์ƒ์„ฑ์ž์™€ ๊ฐ™์€ ํšจ๊ณผ๋ฅผ ๋ƒ„
      • ์ฆ‰, clone์€ ์›๋ณธ ๊ฐ์ฒด์— ์•„๋ฌด๋Ÿฐ ํ•ด๋ฅผ ๋ผ์น˜์ง€ ์•Š๋Š” ๋™์‹œ์— ๋ณต์ œ๋œ ๊ฐ์ฒด์˜ ๋ถˆ๋ณ€์‹์„ ๋ณด์žฅํ•ด์•ผ ํ•จ
      • ๋”ฐ๋ผ์„œ Stack์˜ clone ๋ฉ”์„œ๋“œ๊ฐ€ ์ œ๋Œ€๋กœ ๋™์ž‘ํ•˜๋ ค๋ฉด ์Šคํƒ ๋‚ด๋ถ€ ์ •๋ณด๋ฅผ ๋ณต์‚ฌํ•ด์•ผ ํ•จ

     

    a) ์ฐธ์กฐ ๊ฐ์ฒด์˜ ๋‚ด๋ถ€ ์ •๋ณด๊นŒ์ง€ ๋ณต์‚ฌํ•˜๋ ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ๊นŒ?

    ์ฝ”๋“œ 13-2 ๊ฐ€๋ณ€ ์ƒํƒœ๋ฅผ ์ฐธ์กฐํ•˜๋Š” ํด๋ž˜์Šค์šฉ clone ๋ฉ”์„œ๋“œ

    @Override
    public Stack clone() {
        try {
        	Stack result = (Stack) super.clone();
            result.elements = elements.clone();
            return result;
        } catch (CloneNotSupportedException e) {
        	throw new AssertionError();
        }
    }
    • elements ๋ฐฐ์—ด์˜ clone์„ ์žฌ๊ท€์ ์œผ๋กœ ํ˜ธ์ถœํ•จ
    • ๋ฐฐ์—ด์˜ clone
      • ๋Ÿฐํƒ€์ž„๊ณผ ์ปดํŒŒ์ผํƒ€์ž„ ํƒ€์ž… ๋ชจ๋‘ ์›๋ณธ ๋ฐฐ์—ด๊ณผ ๋˜‘๊ฐ™์€ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•จ
      • ๋”ฐ๋ผ์„œ ๋ฐฐ์—ด์„ ๋ณต์ œํ•  ๋•Œ๋Š” ๋ฐฐ์—ด์˜ clone ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ผ๊ณ  ๊ถŒ์žฅํ•จ --> ๋ฐฐ์—ด์€ clone ๊ธฐ๋Šฅ์„ ์ œ๋Œ€๋กœ ์‚ฌ์šฉํ•˜๋Š” ์œ ์ผํ•œ ์˜ˆ
    • ์ ์šฉํ•˜์ง€ ๋ชปํ•˜๋Š” ๊ฒฝ์šฐ
      • elements ํ•„๋“œ๊ฐ€ final์ธ ๊ฒฝ์šฐ ์œ„์˜ ๋ฐฉ์‹์€ ๋™์ž‘ํ•˜์ง€ ์•Š์Œ (final ํ•„๋“œ์—๋Š” ์ƒˆ๋กœ์šด ๊ฐ’์„ ํ• ๋‹นํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ)
      • ์ด๋Š” '๊ฐ€๋ณ€ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•˜๋Š” ํ•„๋“œ๋Š” final๋กœ ์„ ์–ธํ•˜๋ผ'๋Š” ์ผ๋ฐ˜ ์šฉ๋ฒ•๊ณผ ์ถฉ๋Œํ•จ
        • ๋ณต์ œ ๊ฐ€๋Šฅํ•œ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ์ผ๋ถ€ ํ•„๋“œ์—์„œ final ํ•œ์ •์ž๋ฅผ ์ œ๊ฑฐํ•ด์•ผ ํ•  ์ˆ˜๋„ ์žˆ์Œ

     

    b) clone์„ ์žฌ๊ท€์ ์œผ๋กœ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ๋งŒ์œผ๋กœ ์ถฉ๋ถ„ํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ๋Š” ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ๊นŒ?

    ์˜ˆ์‹œ - HashTable

    • ํ•ด์‹œํ…Œ์ด๋ธ” ๋‚ด๋ถ€๋Š” ๋ฒ„ํ‚ท๋“ค์˜ ๋ฐฐ์—ด์ด๊ณ , ๊ฐ ๋ฒ„ํ‚ท์€ ํ‚ค-๊ฐ’ ์Œ์„ ๋‹ด๋Š” ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ์˜ ์ฒซ ๋ฒˆ์งธ ์—”ํŠธ๋ฆฌ๋ฅผ ์ฐธ์กฐํ•จ
    public class HashTable implements Cloneable {
        private Entry[] buckets = ...;
        
        private static class Entry {
        	final Object key;
            Object value;
            Entry next;
            
            Entry(Object key, Object value, Entry next) {
            	this.key = key;
                this.value = value;
                this.next = next;
            }
        }
        
        ...
    }

     

    ์ฝ”๋“œ 13-3 ์ž˜๋ชป๋œ clone ๋ฉ”์„œ๋“œ - ๊ฐ€๋ณ€ ์ƒํƒœ๋ฅผ ๊ณต์œ ํ•จ

    @Override
    public HashTable clone() {
        try {
        	HashTable result = (HashTable) super.clone();
            result.buckets = buckets.clone(); //๋ฐฐ์—ด ๋ณต์‚ฌ
            return result;
        } catch (CloneNotSupportedException e) {
        	throw new AssertionError();
        }
    }

     

    • ๋ณต์ œ๋ณธ์€ ์ž์‹ ๋งŒ์˜ ๋ฒ„ํ‚ท ๋ฐฐ์—ด์„ ๊ฐ€์ง€์ง€๋งŒ, ์ด ๋ฐฐ์—ด์€ ์›๋ณธ๊ณผ ๊ฐ™์€ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ฐธ์กฐํ•˜์—ฌ ์›๋ณธ๊ณผ ๋ณต์ œ๋ณธ ๋ชจ๋‘ ์˜ˆ๊ธฐ์น˜ ์•Š๊ฒŒ ๋™์ž‘ํ•  ๊ฐ€๋Šฅ์„ฑ์ด ์ƒ๊น€

     

    ๋‹ค์Œ์€ ์ผ๋ฐ˜์ ์ธ ํ•ด๋ฒ•์ž„

    ์ฝ”๋“œ 13-4 ๋ณต์žกํ•œ ๊ฐ€๋ณ€ ์ƒํƒœ๋ฅผ ๊ฐ–๋Š” ํด๋ž˜์Šค์šฉ ์žฌ๊ท€์  clone ๋ฉ”์„œ๋“œ

    public class HashTable implements Cloneable {
        private Entry[] buckets = ...;
        
        private static class Entry {
        	final Object key;
            Object value;
            Entry next;
            
            Entry(Object key, Object value, Entry next) {
            	this.key = key;
                this.value = value;
                this.next = next;
            }
            
            //ํ•ด๋‹น ์—”ํŠธ๋ฆฌ๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ์žฌ๊ท€์ ์œผ๋กœ ๋ณต์‚ฌ
            Entry deepCopy() {
            	return new Entry(key, value,
                	next == null ? null : next.deepCopy());
            }
        }
        
        @Override
        public HashTable clone() {
        	try {
                HashTable result = (HashTable) super.clone();
                result.buckets = new Entry[buckets.length];
                for (int i = 0; i < buckets.length; i++) {
                	if (buckets[i] != null) {
                    	result.buckets[i] = buckets[i].deepCopy();
                    }
                }
                return result;
            } catch (CloneNotSupportedException e) {
            	throw new AssertionError();
            }
        }
        
        ...
    }
    • private ํด๋ž˜์Šค์ธ HashTable.Entry์— ๊นŠ์€๋ณต์‚ฌ๋ฅผ ์ถ”๊ฐ€ํ•จ
    • HashTable์˜ clone ๋ฉ”์„œ๋“œ๋Š”
      1. ์ ์ ˆํ•œ ํฌ๊ธฐ์˜ ์ƒˆ๋กœ์šด ๋ฒ„ํ‚ท ๋ฐฐ์—ด ํ• ๋‹น
      2. ์›๋ž˜์˜ ๋ฒ„ํ‚ท ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉฐ ๋น„์ง€ ์•Š์€ ๊ฐ ๋ฒ„ํ‚ท์— ๋Œ€ํ•ด ๊นŠ์€๋ณต์‚ฌ๋ฅผ ์ˆ˜ํ–‰ํ•จ
    • ๋‹จ์ 
      • ์žฌ๊ท€ ํ˜ธ์ถœ๋กœ ๋ฆฌ์ŠคํŠธ์ด ์›์†Œ ์ˆ˜๋งŒํผ ์Šคํƒ ํ”„๋ ˆ์ž„์„ ์†Œ๋น„ํ•จ
      • ๋ฆฌ์ŠคํŠธ๊ฐ€ ๊ธธ๋ฉด ์Šคํƒ ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ๋ฅผ ์ผ์œผํ‚ฌ ์œ„ํ—˜์ด ์žˆ์Œ
      • ์ด ๋ฌธ์ œ๋ฅผ ํ”ผํ•˜๋ ค๋ฉด deepCopy๋ฅผ ์žฌ๊ท€ ํ˜ธ์ถœ ๋Œ€์‹  ๋ฐ˜๋ณต์ž๋ฅผ ์จ์„œ ์ˆœํšŒํ•˜๋Š” ๋ฐฉํ–ฅ์œผ๋กœ ์ˆ˜์ •ํ•ด์•ผ ํ•จ

     

    ์ฝ”๋“œ 13-5 ์—”ํŠธ๋ฆฌ ์ž์‹ ์ด ๊ฐ€๋ฆฌํ‚ค๋Š” ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜๋ณต์ ์œผ๋กœ ๋ณต์‚ฌํ•จ

    Entry deepCopy() {
        Entry result = new Entry(key, value, next);
        for (Entry p = result; p.next != null; p = p.next) {
        	p.next = new Entry(p.next.key, p.next.value, p.next.next);
        }
        return result;
    }

     

    c) ๋งˆ์ง€๋ง‰ ๋ฐฉ๋ฒ•

    • super.clone์„ ํ˜ธ์ถœํ•˜์—ฌ ์–ป์€ ๊ฐ์ฒด์˜ ๋ชจ๋“  ํ•„๋“œ๋ฅผ ์ดˆ๊ธฐ ์ƒํƒœ๋กœ ์„ค์ •ํ•œ ํ›„, ์›๋ณธ ๊ฐ์ฒด์˜ ์ƒํƒœ๋ฅผ ๋‹ค์‹œ ์ƒ์„ฑํ•˜๋Š” ๊ณ ์ˆ˜์ค€ ๋ฉ”์„œ๋“œ๋“ค์„ ํ˜ธ์ถœํ•จ
    • ์˜ˆ์‹œ
      • HashTable ์˜ˆ๋ฅผ ๋ณด๋ฉด, buckets ํ•„๋“œ๋ฅผ ์ƒˆ๋กœ์šด ๋ฒ„ํ‚ท ๋ฐฐ์—ด๋กœ ์ดˆ๊ธฐํ™”ํ•จ
      • ์›๋ณธ ํ…Œ์ด๋ธ”์— ๋‹ด๊ธด ๋ชจ๋“  ํ‚ค-๊ฐ’ ์Œ ๊ฐ๊ฐ์— ๋Œ€ํ•ด ๋ณต์ œ๋ณธ ํ…Œ์ด๋ธ”์˜ put(key, value) ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ด ๋‘˜์˜ ๋‚ด์šฉ์ด ๊ฐ™๊ฒŒ ํ•ด์ฃผ๋ฉด ๋จ
    • ํŠน์ง•
      • Cloneable ์•„ํ‚คํ…์ฒ˜์˜ ๊ธฐ์ดˆ๊ฐ€ ๋˜๋Š” ํ•„๋“œ ๋‹จ์œ„ ๊ฐ์ฒด ๋ณต์‚ฌ๋ฅผ ์šฐํšŒํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ „์ฒด Cloneable ์•„ํ‚คํ…์ฒ˜์™€๋Š” ์–ด์šธ๋ฆฌ์ง€ ์•Š๋Š” ๋ฐฉ์‹์ž„

     

    ์ฃผ์˜์‚ฌํ•ญ

    • ์ƒ์„ฑ์ž์—์„œ๋Š” ์˜ค๋ฒ„๋ผ์ด๋”ฉ ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์ง€ ์•Š์•„์•ผ ํ•จ
    • ์ƒ์†์šฉ ํด๋ž˜์Šค๋Š” Cloneable์„ ๊ตฌํ˜„ํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ด ์ข‹์Œ - ์˜ˆ์‹œ
    • Cloneable์„ ๊ตฌํ˜„ํ•œ thread-safeํ•œ ํด๋ž˜์Šค๋ฅผ ์ž‘์„ฑํ•  ๋•Œ๋Š” clone ๋ฉ”์„œ๋“œ ์—ญ์‹œ ๋™๊ธฐํ™”ํ•ด์ค˜์•ผ ํ•จ

     

    <clone ๋Œ€์‹  ๊ถŒ์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•>

    • ๋ณต์‚ฌ ์ƒ์„ฑ์ž์™€ ๋ณต์‚ฌ ํŒฉํ„ฐ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•œ ๋” ๋‚˜์€ ๊ฐ์ฒด ๋ณต์‚ฌ ๋ฐฉ์‹์„ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ์Œ
      • ๋ณต์‚ฌ ์ƒ์„ฑ์ž?
        • ๋‹จ์ˆœํžˆ ์ž์‹ ๊ณผ ๊ฐ™์€ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ธ์ˆ˜๋กœ ๋ฐ›๋Š” ์ƒ์„ฑ์ž๋ฅผ ๋งํ•จ

     

    ์ฝ”๋“œ 13-7 ๋ณต์‚ฌ ์ƒ์„ฑ์ž

    public Yum(Yum yum) { ... }

    ์ฝ”๋“œ 13-8 ๋ณต์‚ฌ ํŒฉํ„ฐ๋ฆฌ

    public static Yum newInstance(Yum yum) { ... }

     

    • ์œ„์™€ ๊ฐ™์€ ์ธํ„ฐํŽ˜์ด์Šค ๊ธฐ๋ฐ˜ ๋ณต์‚ฌ ์ƒ์„ฑ์ž์™€ ๋ณต์‚ฌ ํŒฉํ„ฐ๋ฆฌ๋ฅผ '๋ณ€ํ™˜ ์ƒ์„ฑ์ž'์™€ '๋ณ€ํ™˜ ํŒฉํ„ฐ๋ฆฌ' ๋ผ๊ณ  ํ•จ
    • ํด๋ผ์ด์–ธํŠธ๋Š” ์›๋ณธ์˜ ๊ตฌํ˜„ ํƒ€์ž…์— ์–ฝ๋งค์ด์ง€ ์•Š๊ณ  ๋ณต์ œ๋ณธ์˜ ํƒ€์ž…์„ ์ง์ ‘ ์„ ํƒํ•  ์ˆ˜ ์žˆ์Œ 

     

    ๐Ÿ“ํ•ต์‹ฌ ์ •๋ฆฌ

    • Cloneable๋กœ ์ธํ•œ ๋ฌธ์ œ์ ์„ ๋˜์งš์–ด๋ดค์„ ๋•Œ, ์ƒˆ๋กœ์šด ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๋งŒ๋“ค ๋•Œ๋Š” ์ ˆ๋Œ€ Cloneable์„ ํ™•์žฅํ•ด์„œ๋Š” ์•ˆ ๋˜๋ฉฐ, ์ƒˆ๋กœ์šด ํด๋ž˜์Šค๋„ ์ด๋ฅผ ๊ตฌํ˜„ํ•ด์„œ๋Š” ์•ˆ๋จ
    • final ํด๋ž˜์Šค๋ผ๋ฉด Cloneable์„ ๊ตฌํ˜„ํ•ด๋„ ์œ„ํ—˜์ด ํฌ์ง€ ์•Š์ง€๋งŒ, ์„ฑ๋Šฅ ์ตœ์ ํ™” ๊ด€์ ์—์„œ ๊ฒ€ํ† ํ•œ ํ›„ ๋ณ„๋‹ค๋ฅธ ๋ฌธ์ œ๊ฐ€ ์—†์„ ๋•Œ๋งŒ ๋“œ๋ฌผ๊ฒŒ ํ—ˆ์šฉํ•ด์•ผ ํ•จ (์•„์ดํ…œ 67)
    • ๊ธฐ๋ณธ ์›์น™์€ '๋ณต์ œ ๊ธฐ๋Šฅ์€ ์ƒ์„ฑ์ž๋‚˜ ํŒฉํ„ฐ๋ฆฌ๋ฅผ ์ด์šฉํ•˜๋Š” ๊ฒŒ ์ตœ๊ณ ' ๋ผ๋Š” ๊ฒƒ์ž„
    • ๋‹จ, ๋ฐฐ์—ด๋งŒ์€ clone ๋ฉ”์„œ๋“œ ๋ฐฉ์‹์ด ๊ฐ€์žฅ ๊น”๋”ํ•˜๊ณ  ์ด ๊ทœ์น™์˜ ํ•ฉ๋‹นํ•œ ์˜ˆ์™ธ๋ผ ํ•  ์ˆ˜ ์žˆ์Œ

     

    ์ฝ์–ด๋ณผ ๊ฒƒ

    ๋Œ“๊ธ€

Designed by Tistory.