๋ชฉ์ฐจ
์ด๋ค ์ํฉ์์ ์ฐ์ผ๊น?
ํฉํ ๋ฆฌ ๋ฉ์๋ ํจํด์์ Ship์์ ๋ฅผ ์ดํด๋ณด์๋ค.
Ship์ Anchor์ Wheel ๋ถํ์ ์ถ๊ฐํ๋ ค๊ณ ํ๋ค
์ด๋, Anchor์ Wheel์ ์๋ก ์ง์ง์ด์ง๋ ๋ถํ์ด๋ค!
Anchor - Wheel / AnchorPro - WheelPro / AnchorV2 - WheelV2 ๋ก ์ง์ง์ด์ ธ์ ์ถ๊ฐ๋์ด์ผํ๋ค
๊ธฐ์กด ์ฝ๋์ ๋ณ๊ฒฝ์ ์ต์ํํ์ฌ ๋ถํ์ ์ถ๊ฐํ๊ณ ์์ ํ ์ ์๋ ๋ฐฉ๋ฒ์ ์์๊น?
์ถ์ ํฉํ ๋ฆฌ ํจํด์ด๋?
์ฌ๋ฌ ๊ด๋ จ์๋ ๊ฐ์ฒด๋ฅผ ๋ง๋ค์ด์ฃผ๋ ์ธํฐํ์ด์ค๋ฅผ ๋ง๋๋ ํจํด์ด๋ค
ํฉํ ๋ฆฌ ๋ฉ์๋ ํจํด๊ณผ ์ ์ฌํ์ง๋ง, ๊ตฌ์ฒด์ ์ผ๋ก ์ด๋ค ํด๋์ค์ ์ธ์คํด์ค๋ฅผ ์ฌ์ฉํ๋์ง ๊ฐ์ถ ์ ์๊ณ
์๋ก ์ ์ฌํ ๊ฐ์ฒด๊ฐ ์กฐํฉ๋์ด์ผํ๋ ๊ฒฝ์ฐ ํฉํ ๋ฆฌ๋ฅผ ์ธํฐํ์ด์คํ ํ์ฌ
WhiteAnchor - BlackWheel์ ์กฐํฉ์ ๋ฐฉ์งํ ์ ์๋ค!
๊ตฌํ๋ฐฉ๋ฒ
๊ธฐ์กด์ฝ๋
public class WhiteshipFactory implements ShipFactory {
@Override
public Ship createShip() {
Ship ship = new Whiteship();
ship.setAnchor(new WhiteAnchor());
ship.setWheel(new WhiteWheel());
return ship;
}
}
๋ณ๊ฒฝ์ฝ๋
public class WhiteAnchor implements Anchor{
}
public class WhiteWheel implements Wheel{
}
//โญ ์ถ์ํฉํ ๋ฆฌ
public interface ShipPartsFactory {
Anchor createAnchor();
Wheel createWheel();
}
public class WhiteShipPartsFactory implements ShipPartsFactory {
@Override
public Anchor createAnchor() {
return new WhiteAnchor();
}
@Override
public Wheel createWheel() {
return new WhiteWheel();
//return๊ฐ์ ํญ์ Wheel์ implementsํด์ผํ๋ฏ๋ก ๊ท์
์ ์งํจ Wheel๋ง ๋ค์ด์ฌ ์ ์๊ฒํ๋ค
}
}
public class WhiteShipFactory implement ShipFactory {
private ShipPartsFactory shipPartsFactory;
public WhiteShipFactory(ShipPartsFactory shipPartsFactory){
this.shipPartsFactory = shipPartsFactory;
}
@Override
public ship createShip(){
Ship ship = new WhiteShip();
ship.setAnchor(shipPartsFactory.createAnchor());
ship.setWheel(shipPartsFactory.createWheel());
//๐ฅshipPartsFactory์์ ์ด๋ฏธ ์ ํด์ง Anchor์ Wheel์ด ๋ง๋ค์ด์ง๋ค.
return ship;
}
}
ํฉํ ๋ฆฌ ๋ฉ์๋ ํจํด์ด๋ผ๋ฉด ์ ์ฝ๋๋ ์๋์ ๊ฐ์์ ๊ฒ์ด๋ค.
ship.setAnchor(new WhiteAnchor());
ship.setWheel(new WhiteWheel());
Anchor์ Wheel์ด ์ง์ง์ด์ ธ์ผํ๋ ์ํฉ์์
ship.setAnchor(new WhiteAnchor());
ship.setWheel(new BlackWheel());
new๋ก ์ง์ ๊ฐ์ฒด๋ฅผ ์ฃผ์ ํ๋ค๋ฉด BlackWheel()์ฒ๋ผ ๊ฐ์ฒด๊ฐ ์๋ชป๋ค์ด๊ฐ๋ ๊ฒฝ์ฐ๊ฐ ๋ฐ์ํ ์ ์๋ค!!!
์ถ์ ํฉํ ๋ฆฌ ํจํด์์๋ ShipPartsFactory ์๋ฆฌ์ ๋ฐ๋
WhiteShipPartsFactory์ ์ด๋ฏธ WhiteAnchor(), WhiteWheel()์ ๋ฐํํ๋๋ก ์ง์ ๋์ด์์ผ๋
์ ์ฌํ ๊ฐ์ฒด๊ฐ ์กฐ๋ฆฝ๋์ด์ผ ํ๋ ๊ฒฝ์ฐ ์ค์๋ฅผ ๋ฐฉ์งํ ์ ์๋ค.
public class Client {
public static void main(String[] args){
ShipFactory shipFactory = new WhiteShipFactory(new WhitePartsFactory());
Ship ship = shipFactory.createShip();
System.out.println(ship.getAnchor().getClass());
//WhiteAnchor
System.out.println(ship.getWheel().getClass());
//WhiteWheel
}
}
Ummm... ํฉํ ๋ฆฌ ๋ฉ์๋ ํจํด๊ณผ ์ถ์ ํฉํ ๋ฆฌ ํจํด์ ์ฐจ์ด๋?
๊ด์ ์ ์ฐจ์ด
ํฉํ ๋ฆฌ ๋ฉ์๋ ํจํด์ “ํฉํ ๋ฆฌ๋ฅผ ๊ตฌํํ๋ ๋ฐฉ๋ฒ (inheritance)”์ ์ด์
์ถ์ ํฉํ ๋ฆฌ ํจํด์ “ํฉํ ๋ฆฌ๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ (composition)”์ ์ด์
๋ชฉ์ ์ ์ฐจ์ด
ํฉํ ๋ฆฌ ๋ฉ์๋ ํจํด์ ๊ตฌ์ฒด์ ์ธ ๊ฐ์ฒด ์์ฑ ๊ณผ์ ์ ํ์ ๋๋ ๊ตฌ์ฒด์ ์ธ ํด๋์ค๋ก ์ฎ๊ธฐ๋ ๊ฒ์ด ๋ชฉ์ .
=> ๊ทธ ์์ฒด๋ก ํ๋์ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ๊ฒฝ์ฐ
์ถ์ ํฉํ ๋ฆฌ ํจํด์ ๊ด๋ จ์๋ ์ฌ๋ฌ ๊ฐ์ฒด๋ฅผ ๊ตฌ์ฒด์ ์ธ ํด๋์ค์ ์์กดํ์ง ์๊ณ ๋ง๋ค ์ ์๊ฒ ํด์ฃผ๋ ๊ฒ์ด ๋ชฉ์ .
=> ์ฌ๋ฌ ์ ์ฌ ๊ฐ์ฒด๋ฅผ ์์ฑํด์ ์กฐ๋ฆฝํ๋ ๊ฒฝ์ฐ
์ฐธ๊ณ
'๐ฐ Design Pattern' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Adapter Pattern | ์ด๋ํฐํจํด (0) | 2022.08.14 |
---|---|
Prototype Pattern | ํ๋กํ ํ์ ํจํด (0) | 2022.08.14 |
Factory Method Pattern | ํฉํ ๋ฆฌ ๋ฉ์๋ ํจํด (0) | 2022.07.29 |
Singleton pattern | ์ฑ๊ธํคํจํด (3) | 2022.07.29 |
[๊ฐ์ฒด ์์ฑ ํจํด] ์ ์ธต์ ์์ฑ์ ํจํด โ ์๋ฐ๋น ํจํด โ Builder ํจํด, lombok @Builder ์ฌ์ฉ๋ฐฉ๋ฒ ๋ฐ ์ฃผ์์ (0) | 2022.01.29 |