๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Java

[Java] static ์‘์šฉ - ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด

by ์ฝ”๋”ฉํ•˜๋Š” ๋ถ•์–ด 2021. 2. 19.
๋ฐ˜์‘ํ˜•

โ–ถ static ์‘์šฉ ใ…ก ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด

โ€‹

๋‹จ๊ณ„ 1. ์ƒ์„ฑ์ž๋ฅผ private์œผ๋กœ ๋งŒ๋“ค๊ธฐ

package singleton;

public class Company {
 
    private Company() {
    }

}

-์‹ฑ๊ธ€ํ†ค ํŒจํ„ด์—์„œ๋Š” ์ƒ์„ฑ์ž๋ฅผ ๋ฐ˜๋“œ์‹œ ๋ช…์‹œ์ ์œผ๋กœ ๋งŒ๋“ค๊ณ  ๊ทธ ์ ‘๊ทผ ์ œ์–ด์ž๋ฅผ private์œผ๋กœ ์ง€์ •ํ•ด์•ผ ํ•œ๋‹ค.

๊ทธ๋Ÿฌ๋ฉด ์ƒ์„ฑ์ž๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ๋””ํดํŠธ ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ค์ง€ ์•Š๊ณ , ์ ‘๊ทผ ์ œ์–ด์ž๊ฐ€ private์ด๋ฏ€๋กœ ์™ธ๋ถ€ ํด๋ž˜์Šค์—์„œ ๋งˆ์Œ๋Œ€๋กœ Company ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์—†๊ฒŒ ๋œ๋‹ค.

โ€‹

โ€‹

 

๋‹จ๊ณ„ 2. ํด๋ž˜์Šค ๋‚ด๋ถ€์— static์œผ๋กœ ์œ ์ผํ•œ ์ธ์Šคํ„ด์Šค ์ƒ์„ฑํ•˜๊ธฐ

package singleton;

public class Company {
    private static Company instance = new Company();  // ์œ ์ผํ•˜๊ฒŒ ์ƒ์„ฑํ•œ ์ธ์Šคํ„ด์Šค
    
    private Company() {
    }

}

-๋‹จ๊ณ„ 1์—์„œ ์™ธ๋ถ€ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์—†๋„๋ก ๋งŒ๋“ค์—ˆ๋‹ค. ํ•˜์ง€๋งŒ ์šฐ๋ฆฌ๊ฐ€ ํ”„๋กœ๊ทธ๋žจ์—์„œ ์‚ฌ์šฉํ•  ์ธ์Šคํ„ด์Šค ํ•˜๋‚˜๋Š” ํ•„์š”ํ•˜๋‹ค. ๋”ฐ๋ผ์„œ Company ํด๋ž˜์Šค ๋‚ด๋ถ€์—์„œ ํ•˜๋‚˜์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. ์ด ์ธ์Šคํ„ด์Šค๊ฐ€ ํ”„๋กœ๊ทธ๋žจ ์ „์ฒด์—์„œ ์‚ฌ์šฉํ•  ์œ ์ผํ•œ ์ธ์Šคํ„ด์Šค๊ฐ€ ๋œ๋‹ค.

โ€‹

 

โ€‹

๋‹จ๊ณ„ 3. ์™ธ๋ถ€์—์„œ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋Š” public ๋ฉ”์„œ๋“œ ๋งŒ๋“ค๊ธฐ

package singleton;

public class Company {
    private static Company instance = new Company();  // ์œ ์ผํ•˜๊ฒŒ ์ƒ์„ฑํ•œ ์ธ์Šคํ„ด์Šค
    
    private Company() {
    }

    public static Company getInstance() {  // ์ธ์Šคํ„ด์Šค ์™ธ๋ถ€์—์„œ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋„๋ก public get() ๋ฉ”์„œ๋“œ ๊ตฌํ˜„
        if (instance == null) {
            instance = new Company();
        }
        return instance;
    }

}

-์ด์ œ private์œผ๋กœ ์„ ์–ธํ•œ ์œ ์ผํ•œ ์ธ์Šคํ„ด์Šค๋ฅผ ์™ธ๋ถ€์—์„œ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ค์ •ํ•ด์•ผ ํ•œ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด public ๋ฉ”์„œ๋“œ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์œ ์ผํ•˜๊ฒŒ ์ƒ์„ฑํ•œ ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•ด ์ค€๋‹ค. ์ด ๋•Œ, ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ๋Š” ๋ฐ˜๋“œ์‹œ static์œผ๋กœ ์„ ์–ธํ•ด์•ผ ํ•œ๋‹ค. ์™œ๋ƒํ•˜๋ฉด getInstance( ) ๋ฉ”์„œ๋“œ๋Š” ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ๊ณผ ์ƒ๊ด€์—†์ด ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

โ€‹

โ€‹

 

๋‹จ๊ณ„ 4. ์‹ค์ œ๋กœ ์‚ฌ์šฉํ•˜๋Š” ์ฝ”๋“œ ๋งŒ๋“ค๊ธฐ

package singleton;

public class CompanyTest {
    public static void main(String[] args) {
        Company myCompany1 = Company.getInstance();  // ํด๋ž˜์Šค ์ด๋ฆ„์œผ๋กœ getInstance() ํ˜ธ์ถœํ•˜์—ฌ ์ฐธ์กฐ ๋ณ€์ˆ˜์— ๋Œ€์ž… 
        Company myCompany2 = Company.getInstance();  // ํด๋ž˜์Šค ์ด๋ฆ„์œผ๋กœ getInstance() ํ˜ธ์ถœํ•˜์—ฌ ์ฐธ์กฐ ๋ณ€์ˆ˜์— ๋Œ€์ž…
        System.out.println(myCompany1 == myCompany2);  // ๋‘ ๋ณ€์ˆ˜๊ฐ€ ๊ฐ™์€ ์ฃผ์†Œ์ธ์ง€ ํ™•์ธ
    }
}

-์™ธ๋ถ€ ํด๋ž˜์Šค์—์„œ๋Š” Company๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ static์œผ๋กœ ์ œ๊ณต๋˜๋Š” getInstance( ) ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

Company.getInstance( );์™€ ๊ฐ™์ด ํ˜ธ์ถœํ•˜๋ฉด ๋ฐ˜ํ™˜ ๊ฐ’์œผ๋กœ ์œ ์ผํ•œ ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ›์•„์˜จ๋‹ค.

โ€‹

โ€‹

<๊ฒฐ๊ณผ๊ฐ’>

-myCompany1๊ณผ myCompany2๋ฅผ ๋น„๊ตํ•ด ๋ณด๋ฉด ๊ฐ™์€ ์ฐธ์กฐ ๊ฐ’์„ ๊ฐ€์ง€๋Š” ๋™์ผํ•œ ์ธ์Šคํ„ด์Šค์ž„์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

์—ด ๋ฒˆ, ๋ฐฑ ๋ฒˆ์„ ํ˜ธ์ถœํ•ด๋„ ํ•ญ์ƒ ๊ฐ™์€ ์ฃผ์†Œ์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ๋ฐ˜ํ™˜๋  ๊ฒƒ์ด๋‹ค.

๋˜, Company ํด๋ž˜์Šค๋Š” ๋‚ด๋ถ€์— ์ƒ์„ฑ๋œ ์œ ์ผํ•œ ์ธ์Šคํ„ด์Šค ์™ธ์—๋Š” ๋” ์ด์ƒ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์—†๋‹ค.

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

 

 

 

์‹ค์Šต ) ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด์œผ๋กœ ํด๋ž˜์Šค ๊ตฌํ˜„ ์—ฐ์Šตํ•˜๊ธฐ

์ž๋™์ฐจ ๊ณต์žฅ์ด ์žˆ๋‹ค. ์ž๋™์ฐจ ๊ณต์žฅ์€ ์œ ์ผํ•œ ๊ฐ์ฒด์ด๊ณ , ์ด ๊ณต์žฅ์—์„œ ์ƒ์‚ฐ๋˜๋Š” ์ž๋™์ฐจ๋Š” ์ œ์ž‘๋  ๋•Œ๋งˆ๋‹ค ๊ณ ์œ  ๋ฒˆํ˜ธ๊ฐ€ ๋ถ€์—ฌ๋œ๋‹ค. ์ž๋™์ฐจ ๋ฒˆํ˜ธ๊ฐ€ 10001๋ถ€ํ„ฐ ์‹œ์ž‘๋˜์–ด ์ž๋™์ฐจ๊ฐ€ ์ƒ์„ฑ๋ ๋•Œ๋งˆ๋‹ค 10002, 10003 ์ด๋ ‡๊ฒŒ ๋ฒˆํ˜ธ๊ฐ€ ๋ถ™๋„๋ก ์ž๋™์ฐจ ๊ณต์žฅ ํด๋ž˜์Šค, ์ž๋™์ฐจ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด ๋ณด์ž. ๋‘ ํด๋ž˜์Šค๋Š” ๋‹ค์Œ CarFactoryTest.java ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๊ฐ€ ์ˆ˜ํ–‰๋  ์ˆ˜ ์žˆ๋„๋ก ๊ตฌํ˜„ํ•ด ๋ณด์ž.

โ€‹

โ€‹

<๋‚ด๊ฐ€ ํ•œ ๊ฒƒ>

package singleton;

public class CarFactory {
   
    String createCar;
	private static CarFactory instance = new CarFactory();

    public String getCreateCar() {
        return createCar;
    }

    public void setCreateCar(String createCar) {
        this.createCar = createCar;
    }

	public static CarFactory getInstance() {
		if (instance == null) {
			instance = new CarFactory();
		}
		return instance;
	}

	public Car createCar() {
		return new Car();
	}
}

 

package singleton;

public class Car {
    int carNum;
	private static int serialNum = 10000;
	
	public Car() {
		serialNum++;
		carNum = serialNum;
	}

	public int getCarNum() {
		return carNum;
	}

	public void setCarNum(int carNum) {
		this.carNum = carNum;
	}
}

 

package singleton;

public class CarFactoryTest {
	public static void main(String[] args) {
		CarFactory factory = CarFactory.getInstance();
		Car mySonata = factory.createCar();
		Car yourSonata = factory.createCar();

		System.out.println(mySonata.getCarNum());
		System.out.println(yourSonata.getCarNum());
	}

}

<๊ฒฐ๊ณผ๊ฐ’>

 

๋”๋ณด๊ธฐ

โ€‹<์„ ์ƒ๋‹˜์ด ํ•œ ๊ฒƒ>

package singleton;

public class CarFactory {
	private static CarFactory instance;

	private CarFactory() {
	}

	public static CarFactory getInstance() {
		if (instance == null) {
			instance = new CarFactory();
		}
		return instance;
	}

	public Car createCar() {
		return new Car();;
	}
}

 

package chapter6.singleton;

public class Car {

	private static int serialNum = 10000;
	int carNum;
	
	public Car() {
		serialNum++;
		carNum = serialNum;
	}

	public int getCarNum() {
		return carNum;
	}
}

 

 

 

์—ฐ์Šต๋ฌธ์ œ Q5 ) ์•„์นจ ์ถœ๊ทผ๊ธธ์— ๊น€ ์”จ๋Š” 4000์›์„ ๋‚ด๊ณ  ๋ณ„๋‹ค๋ฐฉ์—์„œ ์•„๋ฉ”๋ฆฌ์นด๋…ธ๋ฅผ ์‚ฌ ๋งˆ์…จ๋‹ค. ์ด ์”จ๋Š” ์ฝฉ๋‹ค๋ฐฉ์—์„œ 4500์›์„ ๋‚ด๊ณ  ๋ผ๋–ผ๋ฅผ ์‚ฌ ๋งˆ์…จ๋‹ค.

'06-2 ๊ฐ์ฒด ๊ฐ„ ํ˜‘๋ ฅ' ์„ ์ฐธ๊ณ ํ•˜์—ฌ ์ด ๊ณผ์ •์„ ๊ฐ์ฒด ์ง€ํ–ฅ์œผ๋กœ ํ”„๋กœ๊ทธ๋ž˜๋ฐํ•ด๋ณด์ž.

โ€‹

โ€‹

<๋‚ด๊ฐ€ ํ•œ ๊ฒƒ>

package singleton;

public class Person {

	String personName;
	int money;

	public Person(String personName, int money) {
		this.personName = personName;
		this.money = money;
	}

    public Person() {
    }

    public void takeStar(Star star) {
        star.take(4000);
        this.money -= 4000;
    }

    public void takeKong(Kong kong) {
        kong.take(4500);
        this.money -= 4500;
    }

    public void showInfo() {
        System.out.println(personName + "๋‹˜์˜ ์ž”์•ก์€ " + money + "์› ์ž…๋‹ˆ๋‹ค.");
    }

}

 

package singleton;

public class Star {
    
    String personName;
	int money;

	public Star(String string) {
    }

    public void take(int money) {
        this.money += money;
    }
 
    public void showInfo() {
        System.out.println("์Šคํƒ€1ํ˜ธ์ ์˜ ์ž”์•ก์€ " + money + "์ž…๋‹ˆ๋‹ค.");
	}
}

 

package singleton;

public class Kong {
    
    String personName;
	int money;

	public Kong(String string) {
    }

    public void take(int money) {
        this.money += money;
    }
 
    public void showInfo() {
        System.out.println("์ฝฉ1ํ˜ธ์ ์˜ ์ž”์•ก์€ " + money + "์ž…๋‹ˆ๋‹ค.");
	}
}

 

package singleton;

public class CafeTest {
	public static void main(String[] args) {

		Person kim = new Person("๊น€์”จ", 10000);
		Person lee = new Person("์ด์”จ", 15000);

		Star star1 = new Star("์Šคํƒ€1ํ˜ธ์ ");
		Kong kong1 = new Kong("์ฝฉ1ํ˜ธ์ ");

		kim.takeStar(star1);
        lee.takeKong(kong1);

        kim.showInfo();
        lee.showInfo();

        star1.showInfo();
        kong1.showInfo();
	}
}

<๊ฒฐ๊ณผ๊ฐ’>

 

๋”๋ณด๊ธฐ

<์„ ์ƒ๋‹˜์ด ํ•œ ๊ฒƒ>

package singleton;

public class Person {

	String name;
	int money;

	public Person(String name, int money) {
		this.name = name;
		this.money = money;
	}

    public void takeStar(Star star, int money) {
        star.take(money);
        this.money -= money;
    }

    public void takeKong(Kong kong, int money) {
        kong.take(money);
        this.money -= money;
    }

    public void showInfo() {
        System.out.println(name + "๋‹˜์˜ ๋‚จ์€ ๋ˆ์€ " + money + "์› ์ž…๋‹ˆ๋‹ค.");
    }

}	

 

package singleton;

public class Star {
    
    String starNum;
    int customerCnt;
	int money;

	public Star(String name) {
        starNum = name;
    }

    public void take(int money) {
        this.money += money;
        customerCnt++;
    }
 
    public void showInfo() {
        System.out.println(starNum + "์˜ ๋งค์ถœ์€ " + money + "์› ์ž…๋‹ˆ๋‹ค.");
	}
}

 

package singleton;

public class Kong {
    
    String kongNum;
    int customerCnt;
	int money;

	public Kong(String name) {
        kongNum = name;
    }

    public void take(int money) {
        this.money += money;
        customerCnt++;
    }
 
    public void showInfo() {
        System.out.println(kongNum + "์˜ ๋งค์ถœ์€ " + money + "์› ์ž…๋‹ˆ๋‹ค.");
	}
}

 

package singleton;

public class CafeTest {
	public static void main(String[] args) {

		Person kim = new Person("๊น€์”จ", 10000);
		Person lee = new Person("์ด์”จ", 15000);

		Star star1 = new Star("์Šคํƒ€1ํ˜ธ์ ");
		Kong kong1 = new Kong("์ฝฉ1ํ˜ธ์ ");

		kim.takeStar(star1, 4000);
        lee.takeKong(kong1, 4500);

        kim.showInfo();
        lee.showInfo();

        star1.showInfo();
        kong1.showInfo();
	}
}

 

๋ฐ˜์‘ํ˜•

'Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Java] ๋ฐฐ์—ด  (0) 2021.02.19
[Java] ์—ฐ์Šต๋ฌธ์ œ  (0) 2021.02.19
[Java] ๋ณ€์ˆ˜ ์œ ํšจ ๋ฒ”์œ„  (0) 2021.02.19
[Java] ํด๋ž˜์Šค ๋ฉ”์„œ๋“œ  (0) 2021.02.18
[Java] Static ๋ณ€์ˆ˜  (0) 2021.02.18

๋Œ“๊ธ€