728x90

 

(μ½”λ“œλŠ” Java κΈ°μ€€)

 

ꡳ이 λ‚΄μš©μ„ μ •λ¦¬ν•˜μ§€λŠ” μ•ŠμœΌλ €κ³  ν–ˆλŠ”λ°, 'ν…œν”Œλ¦Ώ λ©”μ„œλ“œμ™€ νŒ©ν† λ¦¬ λ©”μ„œλ“œμ˜ 차이점이 λ­”κ°€μš”?'λΌλŠ” μ§ˆλ¬Έμ„ 보고 λ§μ•˜λ‹€. μ•„, 이건 정리해야겠닀 μ‹Άμ–΄μ„œ μ•„μ˜ˆ κ°œλ…λΆ€ν„° λ‹€μ‹œ κ³΅λΆ€ν–ˆλ‹€.

 

 

κ²°λ‘ λΆ€ν„° λ§ν•˜μžλ©΄, ν…œν”Œλ¦Ώ λ©”μ„œλ“œ νŒ¨ν„΄μ€ 일련의 ν”„λ‘œμ„ΈμŠ€λ₯Ό κ³ μ •μ‹œμΌœλ†“κ³  찍어내듯 μ‚¬μš©ν•  수 있게 μ„€κ³„ν•˜λŠ” 방식이닀. μ—¬λŸ¬ 번 μ‚¬μš©ν•˜κ³  ν”„λ‘œμ„ΈμŠ€μ— 큰 λ³€ν™”κ°€ μ—†λŠ” μ•Œκ³ λ¦¬μ¦˜μΌ λ•Œ μ‚¬μš©ν•œλ‹€. 각 단계 μ•ˆμ—μ„œλŠ” λ³€κ²½(ν™•μž₯)이 μžˆμ„ 수 있으며 이λ₯Ό μ²˜λ¦¬ν•˜κΈ° μœ„ν•΄ μ„ΈλΆ€ λ‹¨κ³„λŠ” κ΅¬ν˜„ ν΄λž˜μŠ€μ—μ„œ μ •μ˜ν•œλ‹€.

νŒ©ν† λ¦¬ λ©”μ„œλ“œ νŒ¨ν„΄λ„ μ΄λ¦„μ—μ„œμ²˜λŸΌ 찍어내듯 μ„€κ³„ν•˜λŠ” 방식이닀. ν…œν”Œλ¦Ώ λ©”μ„œλ“œλ₯Ό 기반으둜 ν•˜κΈ° λ•Œλ¬Έμ— λΉ„μŠ·ν•œ λŠλ‚Œμ΄λΌκ³  μƒκ°ν• μˆ˜λ„ μžˆλŠ”λ°, ν…œν”Œλ¦Ώ λ©”μ„œλ“œ νŒ¨ν„΄μ˜ ν•œ μΌ€μ΄μŠ€κ°™μ€ λŠλ‚Œμ΄λ‹€. κ³΅μž₯μ΄λΌλŠ” μ΄λ¦„λ‹΅κ²Œ '객체'λ₯Ό λ‹€λ£¨λŠ”λ° 주어진 쑰건에 λ§žλŠ” 객체λ₯Ό λ¦¬ν„΄ν•˜λ―€λ‘œ μŠ€νŠΈλž˜ν‹°μ§€ νŒ¨ν„΄κ³Όλ„ κ΄€λ ¨ μžˆλ‹€ λ³Ό 수 μžˆκ² λ‹€.

 

 

 


 Template Method Pattern 

μ•Œκ³ λ¦¬μ¦˜μ˜ κ΅¬μ‘°λŠ” λ³€κ²½ν•˜μ§€ μ•Šκ³ , 각 단계λ₯Ό μž¬μ •μ˜ν•  수 있게 ν•˜λŠ” λ””μžμΈ νŒ¨ν„΄

 

 

 πŸŽˆ When To Use 

  • μ•Œκ³ λ¦¬μ¦˜ 전체 κ΅¬μ‘°λŠ” μœ μ§€ν•˜λ˜ νŠΉμ • λ‹¨κ³„λ§Œ ν™•μž₯(μž¬μ •μ˜)ν•΄μ•Ό ν•˜λŠ” 경우
  • 거의 λΉ„μŠ·ν•œ μ•Œκ³ λ¦¬μ¦˜μ„ ν¬ν•¨ν•˜λŠ” μ—¬λŸ¬ ν΄λž˜μŠ€κ°€ μžˆλŠ” 경우

 

 

 πŸŽˆ κΈ°λ³Έ 섀계 

μ•Œκ³ λ¦¬μ¦˜μ˜ 각 단계λ₯Ό λ©”μ„œλ“œλ‘œ λΆ„λ¦¬ν•œλ‹€.

- μ™ΈλΆ€ νŒ¨ν‚€μ§€μ—μ„œ λ©”μ„œλ“œ μ„ΈλΆ€ λ‚΄μš©μ— μ ‘κ·Όν•˜μ§€ λͺ»ν•˜κ²Œ ν•˜λ €λ©΄ μ ‘κ·Ό μ œμ–΄μžλ₯Ό 섀정해쀄 것

 

좔상 ν΄λž˜μŠ€μ—λŠ” λ³€κ²½λ˜μ§€ μ•Šμ„ μš”μ†Œλ₯Ό μ •μ˜ν•œλ‹€.

- μ•Œκ³ λ¦¬μ¦˜ μˆœμ„œλ₯Ό templateMethod() λ©”μ„œλ“œμ— μ •μ˜ν•œλ‹€. μ™ΈλΆ€μ—μ„œλŠ” 이 λ©”μ„œλ“œλ₯Ό ν˜ΈμΆœν•˜μ—¬ μ•Œκ³ λ¦¬μ¦˜μ„ μ‹€ν–‰ν•œλ‹€.

Primitive λ©”μ„œλ“œλŠ” λ°˜λ“œμ‹œ μž¬μ •μ˜ν•΄μ•Ό ν•œλ‹€. κ΅¬ν˜„λ§ˆλ‹€ 변경될 수 μžˆλŠ” μš”μ†Œλ₯Ό λ‹΄λŠ”λ‹€.

- Hook λ©”μ„œλ“œλŠ” ν•„μš”μ— 따라 μž¬μ •μ˜ν•  수 μžˆλ‹€. λ³€κ²½λ˜μ§€ μ•Šκ±°λ‚˜ 변경이 선택적인 μš”μ†Œλ₯Ό λ‹΄λŠ”λ‹€.

 

κ΅¬ν˜„ ν΄λž˜μŠ€μ—μ„œλŠ” 각 상황에 맞게 λ©”μ„œλ“œλ₯Ό μž¬μ •μ˜ν•œλ‹€.

 

 

 πŸŽˆ 적용 방법

package customLibrary;

public abstract class GoingOut {
	// primitive λ©”μ„œλ“œ
	abstract void wearClothes();

	// hook λ©”μ„œλ“œ
	void openDoor() {
		System.out.println("문을 μ—΄κ³  λ‚˜κ°„λ‹€.");
	}

	// μ™ΈλΆ€μ—μ„œ ν˜ΈμΆœν•˜λŠ” λ©”μ„œλ“œ
	public void prepare() {
		wearClothes();
		openDoor();
	}
}

class MeetFriends extends GoingOut {
	@Override
	void wearClothes() {
		System.out.println("ν›„λ“œν‹°λ₯Ό μž…λŠ”λ‹€.");
	}

	@Override
	void openDoor() {
		// You can override operation
		// Or not
        // System.out.println("λ…Έν¬ν•œ λ’€ 문을 μ—΄κ³  λ‚˜κ°„λ‹€.");
	}
}

 

package whatever;

class Main {
	MeetFriends plan;

	void init() {
		plan = new MeetFriends();
	}

	void execute() {
		plan.prepare();
	}
}

/***
 * 
 * < output >
 * ν›„λ“œν‹°λ₯Ό μž…λŠ”λ‹€.
 * 문을 μ—΄κ³  λ‚˜κ°„λ‹€. 
 *
 */

 

 

 


 Factory Method Pattern 

쑰건에 μ ν•©ν•œ 객체λ₯Ό λ°˜ν™˜ν•˜λŠ” λ””μžμΈ νŒ¨ν„΄

 

 

 πŸŽˆ When To Use

μž‘μ—…ν•  객체의 μ •ν™•ν•œ μœ ν˜•κ³Ό 쒅속성을 λͺ¨λ₯΄λŠ” 경우

  • μžλ°”μ—μ„œ μ œλ„ˆλ¦­ μ‚¬μš©ν•˜λŠ” λŠλ‚Œ..?

 

라이브러리/ν”„λ ˆμž„μ›Œν¬μ˜ λ‚΄λΆ€ ꡬ성 μš”μ†Œ ν™•μž₯을 μ œκ³΅ν•  ν•„μš”κ°€ μžˆλŠ” 경우

  • μƒˆλ‘œμš΄ μš”μ†Œ(Product)λ₯Ό μΆ”κ°€ν•˜κ³  μ‹ΆμœΌλ©΄ ν•΄λ‹Ή Concrete Productλ₯Ό λ°˜ν™˜ν•˜λŠ” μƒˆλ‘œμš΄ Concrete Creator만 μ •μ˜ν•˜λ©΄ λœλ‹€.
  • λ¦¬μ†ŒμŠ€ μ ˆμ•½μ„ μœ„ν•΄ 개체λ₯Ό μž¬μ‚¬μš©ν•˜μ—¬ νŠΉμ„± μˆ˜μ •μ΄ μš©μ΄ν•˜λ‹€.

 

 

🎈 κΈ°λ³Έ κ°œλ…

Product

  • νŒ©ν† λ¦¬ λ©”μ„œλ“œμ—μ„œ λ°˜ν™˜ν•  객체의 μΈν„°νŽ˜μ΄μŠ€

ConcreteProduct

  • Product의 κ΅¬ν˜„ 클래슀(λ“€)

Creator

  • Product νƒ€μž…μ˜ 객체λ₯Ό λ°˜ν™˜ν•˜λŠ” νŒ©ν† λ¦¬ λ©”μ„œλ“œλ₯Ό μ„ μ–Έν•˜λŠ” 클래슀

ConcreteCreator

  • Creator의 νŒ©ν† λ¦¬ λ©”μ„œλ“œλ₯Ό μž¬μ •μ˜ν•˜μ—¬ ConcreteProductλ₯Ό λ°˜ν™˜

ClientCode

  • νŒ©ν† λ¦¬ λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•˜λŠ” μ‹€μ œ μ½”λ“œ. ν•΄λ‹Ή μ½”λ“œμ—μ„œλŠ” λ°˜ν™˜λ˜λŠ” 객체가 무엇인지 μ•Œ 수 μ—†κ³ , μ•Œ ν•„μš”λ„ μ—†λ‹€. μ–΄λ–€ 객체든 간에 μ²˜λ¦¬ν•˜λŠ” ν”„λ‘œμ„ΈμŠ€λ₯Ό μΆ”μƒμ μœΌλ‘œ 닀뀄야 ν•œλ‹€.

 

 

 

🎈 적용 방법

λ‹€μ–‘ν•œ νŒ©ν† λ¦¬ κ΅¬ν˜„ λ°©μ‹μ˜ 비ꡐ (refactoring.guru)

μ•„λž˜ μ½”λ“œλŠ” enum을 μ‚¬μš©ν•΄μ„œ 생성 객체λ₯Ό κ΅¬λΆ„ν•˜λŠ” 방식이 μ•„λ‹Œ, μ„œλΈŒ ν΄λž˜μŠ€μ—μ„œ μž¬μ •μ˜ν•˜μ—¬ 생성 객체λ₯Ό κ΅¬λΆ„ν•˜λŠ” 방식이닀. ( = Factory Method Pattern )

// Creator
abstract class TicketFactory {
	// Factory Method
	public abstract Ticket ticketFactory(int number);
}

// Concrete Creator
class ChildrenTicketFactory extends TicketFactory {
	@Override
	public Ticket ticketFactory(int number) {
		ChildrenTicket ticket = new ChildrenTicket();
		for (int i = 0; i < number; i++)
			ticket.buyTicket();
		return ticket;
	}
}

// Concrete Creator
class AdultsTicketFactory extends TicketFactory{
// ...  μƒλž΅
}

 

// Product
interface Ticket {
	abstract void buyTicket();
}

// Concrete Product
class ChildrenTicket implements Ticket {
	@Override
	public void buyTicket() {
		System.out.println("This ticket is for Children");
	}
}

// Concrete Product
class AdultsTicket implements Ticket {
	@Override
	public void buyTicket() {
		System.out.println("This ticket is for Adults");
	}
}

 

class Main {
	TicketFactory factory;

	void init() {
		// μ–΄μ©Œκ³ μ €μ©Œκ³  쑰건 -> 어린이 ν‹°μΌ“ 생성 ν• λ‹Ή
		factory = new ChildrenTicketFactory();
	}

	void execute() {
		// Client CodeλŠ” μ–΄λ–€ νŒ©ν† λ¦¬κ°€ μ™”λŠ”μ§€ μ•Œ ν•„μš” μ—†λ‹€.
		factory.ticketFactory(3);
	}
}

 

 

 

 


 μ°Έκ³ μžλ£Œ 

 

728x90

'πŸ“˜ μŠ€ν„°λ””' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

Java Spring 기본적인 μ§ˆλ¬Έλ“€  (0) 2022.04.10

+ Recent posts