๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๊ฐœ๋ฐœ/JAVA

[JAVA] ์ƒ์„ฑ์ž ๋Œ€์‹  ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ๋ฅผ ๊ณ ๋ คํ•ด์•ผ ํ•˜๋Š” ์ด์œ !

by ynzu๐Ÿค 2022. 1. 12.
๋ฐ˜์‘ํ˜•

 

๋จผ์ € ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ๊ฐ€ ๋ญ”์ง€ ์ž˜ ๋ชจ๋ฅด๊ฒ ์œผ๋ฉด ์•„๋ž˜ ํฌ์ŠคํŒ…์„ ๋ณด๊ณ  ์˜ค์ž!

 

[Java] ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ๋ž€? (static factory method)

์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ ์ •์ (static), ํŒฉํ† ๋ฆฌ(factory), ๋ฉ”์„œ๋“œ(method) GoF๋ผ๋Š” ๋””์ž์ธ ํŒจํ„ด ์ค‘ ํŒฉํ† ๋ฆฌ ํŒจํ„ด์—์„œ ์œ ๋ž˜ํ•œ ๋‹จ์–ด๋กœ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์—ญํ• ์„ ๋ถ„๋ฆฌํ•˜๊ฒ ๋‹ค๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค. (ํ•˜์ง€๋งŒ ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”

ynzu-dev.tistory.com

 

1. ์ด๋ฆ„์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค.

BigInteger ํด๋ž˜์Šค๋ฅผ ์˜ˆ๋ฅผ ๋“ค์–ด ๋ณด๋ฉด ์ƒ์„ฑ์ž์ธ BigInteger(int, int, Random)๊ณผ ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ์ธ probablePrime(int, Random) ์ค‘ ํ›„์ž๊ฐ€ ๋ฉ”์†Œ๋“œ ๋ช…๋•๋ถ„์— '๊ฐ’์ด ์†Œ์ˆ˜์ธ BigInteger๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค'๋ผ๋Š” ์˜๋ฏธ๋ฅผ ํŒŒ์•…ํ•˜๊ธฐ ์‰ฝ๋‹ค.

BigInteger bigInteger = new BigInteger(int, int, Random);

public BigInteger(int bitLength, int certainty, Random rnd) {
        BigInteger prime;

        if (bitLength < 2)
            throw new ArithmeticException("bitLength < 2");
        prime = (bitLength < SMALL_PRIME_THRESHOLD
                                ? smallPrime(bitLength, certainty, rnd)
                                : largePrime(bitLength, certainty, rnd));
        signum = 1;
        mag = prime.mag;
}

BigInteger bigInteger = BigInteger.probablePrime(int, Random);

public static BigInteger probablePrime(int bitLength, Random rnd) {
        if (bitLength < 2)
            throw new ArithmeticException("bitLength < 2");

        return (bitLength < SMALL_PRIME_THRESHOLD ?
                smallPrime(bitLength, DEFAULT_PRIME_CERTAINTY, rnd) :
                largePrime(bitLength, DEFAULT_PRIME_CERTAINTY, rnd));
}

์ด๋ ‡๊ฒŒ ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ๋Š” ๋„ค์ด๋ฐ๋งŒ ์ž˜ ์ง“๋Š”๋‹ค๋ฉด ์ง๊ด€์ ์œผ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์–ด์„œ ์—‰๋šฑํ•œ ๊ฒƒ์„ ํ˜ธ์ถœํ•˜๋Š” ์‹ค์ˆ˜๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค.

Boolean.logicalOr(boolean a, boolean b);
Boolean.logicalAnd(boolean a, boolean b);

 

๋ฐ˜๋ฉด ์ƒ์„ฑ์ž๋Š” ์ƒ์„ฑ์ž์˜ ํŠน์„ฑ ํŒŒ์•…์ด ํ•„์š”ํ•˜๋ฉฐ, ์‹œ๊ทธ๋‹ˆ์ฒ˜๊ฐ€ ๋™์ผํ•œ ๊ฒƒ(๋ฉ”์†Œ๋“œ ๋ช…, ๋ฉ”์†Œ๋“œ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ๋™์ผํ•œ ๊ฒƒ)์„ ์ค‘๋ณตํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค. 

 

 

2. ํ˜ธ์ถœ๋  ๋•Œ๋งˆ๋‹ค ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒˆ๋กœ ์ƒ์„ฑํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

๋ถˆ๋ณ€ ํด๋ž˜์Šค์˜ ๊ฒฝ์šฐ ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฏธ๋ฆฌ ๋งŒ๋“ค์–ด ๋†“๊ฑฐ๋‚˜ ์ƒˆ๋กœ ์ƒ์„ฑํ•œ ์ธ์Šคํ„ด์Šค๋ฅผ ์บ์‹ฑํ•˜๊ณ , ์žฌํ™œ์šฉํ•˜์—ฌ ๋ถˆํ•„์š”ํ•œ ๊ฐ์ฒด ์ƒ์„ฑ์„ ํ”ผํ•  ์ˆ˜ ์žˆ๋‹ค. 

์˜ˆ๋ฅผ ๋“ค์–ด Boolean.valudOf(boolean) ์€ ๊ฐ์ฒด๋ฅผ ์•„์˜ˆ ์ƒ์„ฑํ•˜์ง€ ์•Š๋Š”๋‹ค. ์•„๋ž˜๋Š” Boolean ํด๋ž˜์Šค์˜ ์ผ๋ถ€์ด๋‹ค.

 public final class Boolean implements java.io.Serializable,Comparable<Boolean> {
 
 	public static final Boolean TRUE = new Boolean(true);

 	public static final Boolean FALSE = new Boolean(false);
    
	public static Boolean valueOf(boolean b) {
		return (b ? TRUE : FALSE);
    }
    
}

 

๋น„์šฉ์ด ํฌ๊ณ , ๊ฐ์ฒด๊ฐ€ ๋ฐ˜๋ณต์ ์œผ๋กœ ์ž์ฃผ ํ˜ธ์ถœ๋˜๋Š” ์ƒํ™ฉ์ด๋ผ๋ฉด ์„ฑ๋Šฅ๋„ ๊ฐœ์„ ๋œ๋‹ค. (ํ”Œ๋ผ์ด์›จ์ดํŠธ ํŒจํ„ด๊ณผ ์œ ์‚ฌ)

๋ถˆ๋ณ€์ด๊ณ , ์ž์ฃผ ์‚ฌ์šฉ๋œ๋‹ค๋ฉด ๊ฐ™์€ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ์‹์œผ๋กœ ์ฝ”๋”ฉํ•˜์ž!

 

3. ๋ฐ˜ํ™˜ ํƒ€์ž…์˜ ํ•˜์œ„ ํƒ€์ž… ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

public class Parent {

    public Parent(){}
    
    public static Parent getChildInstance() {
        return Child.getInstance();
    }
}

public class Child extends Parent{

    private Child(){}

    public static Child getInstance() {
        return new Child();
    }
}

๋ฐ˜ํ™˜ํ•  ๊ฐ์ฒด ํด๋ž˜์Šค๋ฅผ ์ž์œ ๋กญ๊ฒŒ ์„ ํƒํ•  ์ˆ˜ ์žˆ์–ด ์œ ์—ฐ์„ฑ์ด ์ œ๊ณต๋œ๋‹ค.

์ด๋ฅผ ์‘์šฉํ•˜๋ฉด API๋ฅผ ๋งŒ๋“ค ๋•Œ ๊ตฌํ˜„ ํด๋ž˜์Šค(Child)๋ฅผ ๊ณต๊ฐœํ•˜์ง€ ์•Š๊ณ , Parent๋ฅผ ํ†ตํ•ด ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ์–ด์„œ API๋ฅผ ์ž‘๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. 

 

4. ๋งค๊ฐœ๋ณ€์ˆ˜์— ๋”ฐ๋ผ ๋งค๋ฒˆ ๋‹ค๋ฅธ ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

static class CARD{
    public static final String SAMSUNG_CARD = "์‚ผ์„ฑ";
    public static final String KAKAO_CARD="์นด์นด์˜ค";
    
    static Payment payment(String card){ // ๋“ค์–ด์˜ค๋Š” card ์˜ ๊ฐ’ ์— ๋”ฐ๋ผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฐ์ฒด๊ฐ€ ๋‹ค๋ฅด๋‹ค
        switch (card){
            case SAMSUNG_CARD:
                return new SamSungPayment();
            case KAKAO_CARD:
                return new KakaoPayment();
        }
        throw new IllegalArgumentException();
    }
}

์ด ๋ถ€๋ถ„์€ ์„ค๋ช…ํ•˜๊ธฐ ์œ„ํ•œ ์˜ˆ์ œ๊ฐ€ ๋– ์˜ค๋ฅด์ง€ ์•Š์•„ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํฌ์ŠคํŒ…์—์„œ ๋ฐœ์ทŒํ•˜์˜€๋‹ค. (์ถœ์ฒ˜ : https://k3068.tistory.com/

SamSungPayment์™€ KakaoPayment ํด๋ž˜์Šค๋Š” Payment ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์€ ์ƒํƒœ์ด๋‹ค.

 

5. ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ์‹œ์ ์—๋Š” ๋ฐ˜ํ™˜ํ•  ๊ฐ์ฒด์˜ ํด๋ž˜์Šค๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

์ด๋Ÿฐ ์œ ์—ฐํ•จ์œผ๋กœ JDBC์™€ ๊ฐ™์€ ์„œ๋น„์Šค ์ œ๊ณต์ž ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ๋งŒ๋“œ๋Š” ๊ทผ๊ฐ„์ด ๋œ๋‹ค.  JDBC ๋“œ๋ผ์ด๋ฒ„๋ฅผ ๋กœ๋”ฉํ•˜๊ณ  Connection์„ ์ƒ์„ฑํ•˜๋Š” ์†Œ์Šค๋ฅผ ๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

//JDBC ๋“œ๋ผ์ด๋ฒ„ ๋กœ๋”ฉ 
Class.forName("com.mysql.jdbc.Driver"); 

//Connection ์ƒ์„ฑ 
Connection con = DriverManager.getConnection(url, user, pw); //DB ์ ‘์† ์ •๋ณด

getConnection()์— ์˜ํ•ด ๋ฐ˜ํ™˜๋˜๋Š” ๊ตฌํ˜„์ฒด๋Š” DBMS์˜ ์ข…๋ฅ˜์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง€๊ฒŒ ๋˜๋Š”๋ฐ, ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•˜๋ฉด ๊ฐ DBMS์— ๋งž๋Š” API๋ฅผ ๋”ฐ๋กœ ๊ตฌํ˜„ํ•˜์ง€ ์•Š๊ณ  ํด๋ž˜์Šค๊ฐ€ ๋กœ๋“œ๋˜๋Š” ์‹œ์ ์—์„œ ๊ตฌํ˜„์ฒด๋ฅผ ์ƒ์„ฑํ•ด์„œ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

์ฐธ๊ณ  : Class.forName() ์— ๋Œ€ํ•œ ํฌ์ŠคํŒ…  -> https://kyun2.tistory.com/23

 

 

ํ•˜์ง€๋งŒ ์žฅ์ ๋งŒ ์žˆ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋‹จ์ ๋„ ์žˆ๋‹ค.

1. ์ƒ์†์„ ํ•˜๋ ค๋ฉด public์ด๋‚˜ protected ์ƒ์„ฑ์ž๊ฐ€ ํ•„์š”ํ•ด์„œ ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ๋ฅผ ์ œ๊ณตํ•˜๋ฉด ํ•˜์œ„ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์—†๋‹ค.  

 

2. ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ์ฐพ๊ธฐ ์–ด๋ ต๋‹ค.

์ƒ์„ฑ์ž์ฒ˜๋Ÿผ API ์„ค๋ช…์— ๋ช…ํ™•ํžˆ ๋“œ๋Ÿฌ๋‚˜์ง€ ์•Š์œผ๋ฏ€๋กœ, ๋„๋ฆฌ ์•Œ๋ ค์ง„ ๋ช…๋ช… ๊ทœ์น™์— ๋”ฐ๋ผ ๋ฉ”์†Œ๋“œ ๋ช…์„ ๋„ค์ด๋ฐ ํ•ด์•ผํ•  ๊ฒƒ ์ด๋‹ค.

 

๊ฒฐ๋ก 

์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ์™€ ์ƒ์„ฑ์ž๋Š” ๊ฐ๊ฐ์˜ ์“ฐ์ž„์ƒˆ๊ฐ€ ์žˆ์œผ๋‹ˆ ๊ฐ๊ฐ์˜ ์žฅ๋‹จ์ ์„ ํŒŒ์•…ํ•˜์—ฌ ์ƒํ™ฉ์— ๋”ฐ๋ผ ์‚ฌ์šฉํ•˜๋ฉด ๋  ๊ฒƒ ๊ฐ™๋‹ค.

๊ทธ๋ž˜๋„ ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ๊ฐ€ ์œ ๋ฆฌํ•œ ๊ฒฝ์šฐ๊ฐ€ ๋” ๋งŽ์œผ๋ฏ€๋กœ ๋ฌด์กฐ๊ฑด ์ƒ์„ฑ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋˜ ์Šต๊ด€์„ ๊ณ ์ณ์•ผ ํ•œ๋‹ค๊ณ  ํ•œ๋‹ค..! 

 

 

์ถœ์ฒ˜ : ์ดํŽ™ํ‹ฐ๋ธŒ์ž๋ฐ”

'์ƒ์„ฑ์ž ๋Œ€์‹  ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ๋ฅผ ๊ณ ๋ คํ•˜๋ผ'

 

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€