[Design pattern] Builder Pattern ๐ŸŽž, (Creational patterns)

ํ™์ •์™„ยท2021๋…„ 8์›” 5์ผ
0

Design pattern

๋ชฉ๋ก ๋ณด๊ธฐ
5/7
post-thumbnail

Builder Pattern ๐ŸŽž

์ƒ์„ฑ์ž์— ๋“ค์–ด๊ฐˆ ๋งค๊ฐœ ๋ณ€์ˆ˜๊ฐ€ ๋งŽ๋“  ์ ๋“  ์ฐจ๋ก€๋Œ€๋กœ ๋งค๊ฐœ ๋ณ€์ˆ˜๋ฅผ ๋ฐ›์•„๋“ค์ด๊ณ  ๋ชจ๋“  ๋งค๊ฐœ ๋ณ€์ˆ˜๋ฅผ ๋ฐ›์€ ๋’ค,
๋ณ€์ˆ˜๋“ค์„ ํ†ตํ•ฉํ•ด์„œ ํ•œ ๋ฒˆ์— ์‚ฌ์šฉ


Why Use Builder Pattern?

//์กฐ๊ฑด : ์ด ๊ฐ์ฒด๋Š” ํ•œ๋ฒˆ ์ƒ์„ฑ๋˜๋ฉด ์ฝ๊ธฐ(Read)๋งŒ ๊ฐ€๋Šฅ
public class PersonInfo {
	private String name;
	private Integer age;
	private String favoriteColor;
	private String favoriteAnimal;
	private Integer favoriteNumber;

	public PersonInfo(String name, Integer age, String favoriteColor, String
    favoriteAnimal, Integer favoriteNumber) {
		super();
		this.name = name;
		this.age = age;
		this.favoriteColor = favoriteColor;
		this.favoriteAnimal = favoriteAnimal;
		this.favoriteNumber = favoriteNumber;
	}

	public String getPersonInfo() {
		String personInfo = String.format(
        
       "name:%s, age:%d, favoriteColor:%s, 
  	favoriteAnimal:%s, favoriteNumber:%d",
        name, age, favoriteColor, favoriteAnimal, favoriteNumber);

		return personInfo;
	}
}

์œ„์˜ ์กฐ๊ฑด์„ ๊ณ ๋ คํ•˜๋ฉด setter ๋ฉ”์„œ๋“œ๋Š” ์กด์žฌ ๋ถˆ๊ฐ€๋Šฅ,
์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด์„œ๋งŒ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅ๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


์ด ๊ณผ์ •์—์„œ ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅ๋ฐ›์„ ๋•Œ ๋‹ค์–‘ํ•œ ์˜ˆ์™ธ๐Ÿค•๋“ค์ด ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

  1. ์ƒ์„ฑ์ž ์ค‘ ์ผ๋ถ€๋งŒ์„ ์•Œ๊ณ  ์žˆ๋Š” ์˜ˆ์™ธ
  2. ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•˜๋Š” ์ˆœ์„œ๊ฐ€ ๋‹ค๋ฅธ ์˜ˆ์™ธ
  3. ๊ฐ€๋…์„ฑ์„ ์ข‹๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด ์ถ”๊ฐ€ ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ค์–ด์„œ ์ฒ˜๋ฆฌํ•  ์˜ˆ์™ธ ๋“ฑ

๋‹ค์–‘ํ•œ ์˜ˆ์™ธ๋“ค์„ ์ „๋ถ€ ๊ณ ๋ คํ•ด์„œ ์ƒˆ๋กœ์šด ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ค๊ณ  ํ•ด๊ฒฐํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
๋งค๋ฒˆ ์‚ฌ์šฉ์ž๊ฐ€ ์ฒดํฌํ•ด์•ผ ๋˜๋Š” ์ƒํ™ฉ์ด ์˜ค๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.
๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— Builder Pattern์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๐Ÿ˜€


Implement Builder Pattern

  • ๋ถˆํ•„์š”ํ•œ ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ค์ง€ ์•Š๊ณ  ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ ๋‹ค.
  • ๋ฐ์ดํ„ฐ์˜ ์ˆœ์„œ์— ์ƒ๊ด€์—†์ด ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ ๋‹ค.
  • ์‚ฌ์šฉ์ž๊ฐ€ ๋ดค์„ ๋•Œ ๋ช…์‹œ์ ์ด๊ณ  ์ดํ•ดํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค.

public class PersonInfoBuilder {
	private String name;
	private Integer age;
	private String favoriteColor;
	private String favoriteAnimal;
	private Integer favoriteNumber;

	public PersonInfoBuilder setName(String name) {
		this.name = name;
		return this;
	}

	public PersonInfoBuilder setAge(Integer age) {
		this.age = age;
		return this;
	}

	public PersonInfoBuilder setFavoriteColor(String favoriteColor) {
		this.favoriteColor = favoriteColor;
		return this;
	}

	public PersonInfoBuilder setFavoriteAnimal(String favoriteAnimal) {
		this.favoriteAnimal = favoriteAnimal;
		return this;
	}

	public PersonInfoBuilder setFavoriteNumber(Integer favoriteNumber) {
		this.favoriteNumber = favoriteNumber;
		return this;
	}

	public PersonInfo build() {
		PersonInfo personInfo = new PersonInfo(name, age, favoriteColor, 
        	favoriteAnimal, favoriteNumber);
		return personInfo;
	}
}
public class BuilderPattern {

	public static void main(String[] args) {
		
		PersonInfoBuilder personInfoBuilder = new PersonInfoBuilder();

		PersonInfo result = personInfoBuilder.setName("Hong").setAge(24).
        	setFavoriteAnimal("cat").setFavoriteColor("black").build();

		System.out.println(result.getPersonInfo());
	}
}

์ •์ƒ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.


  • Builder class๋ฅผ ๊ผญ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด๋‚ผ class์™€ ๋ถ„๋ฆฌํ•  ํ•„์š”๋Š” ์—†์Šต๋‹ˆ๋‹ค. ๐Ÿ‘†
    ์•„๋ž˜์™€ ๊ฐ™์ด ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด๋‚ผ class ๋‚ด๋ถ€์— Builder class๋ฅผ ํฌํ•จํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

// ์ •์  ์ค‘์ฒฉ ํด๋ž˜์Šค ํ˜•ํƒœ(Static Nested Class)

public class PersonInfo {

	private String name;
	private Integer age;
	private String favoriteColor;
	private String favoriteAnimal;
	private Integer favoriteNumber;

	private PersonInfo(String name, Integer age, String favoriteColor, String favoriteAnimal, 
    Integer favoriteNumber) {
		super();
		this.name = name;
		this.age = age;
		this.favoriteColor = favoriteColor;
		this.favoriteAnimal = favoriteAnimal;
		this.favoriteNumber = favoriteNumber;
	}

	public String getPersonInfo() {
		String personInfo = String.format("name:%s, age:%d, favoriteColor:%s, 
        	favoriteAnimal:%s, favoriteNumber:%d",
            	name, age, favoriteColor, favoriteAnimal, favoriteNumber);

		return personInfo;
	}

	public static class PersonInfoBuilder {
		
		/* ๋ฐ–์— ์žˆ๋Š” class์˜ ๋ณ€์ˆ˜์™€ ๋ฉ”์†Œ๋“œ ์ค‘์— static์ด ๋ถ™์€ ๊ฒƒ๋“ค์„ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
		 * 
		 * */
		private String name;
		private Integer age;
		private String favoriteColor;
		private String favoriteAnimal;
		private Integer favoriteNumber;

		public PersonInfoBuilder setName(String name) {
			this.name = name;
			return this;
		}

		public PersonInfoBuilder setAge(Integer age) {
			this.age = age;
			return this;
		}

		public PersonInfoBuilder setFavoriteColor(String favoriteColor) {
			this.favoriteColor = favoriteColor;
			return this;
		}

		public PersonInfoBuilder setFavoriteAnimal(String favoriteAnimal) {
			this.favoriteAnimal = favoriteAnimal;
			return this;
		}

		public PersonInfoBuilder setFavoriteNumber(Integer favoriteNumber) {
			this.favoriteNumber = favoriteNumber;
			return this;
		}

		public PersonInfo build() {
			PersonInfo personInfo = new PersonInfo(name, age, favoriteColor, 
            		favoriteAnimal, 
            		favoriteNumber);
			return personInfo;
		}
	}
}
public class BuilderPattern {

	public static void main(String[] args) {

		// PersonInfo class์˜ ๊ฐ์ฒด๊ฐ€ ์—†์–ด๋„ PersonInfoBuilder class์˜ ๊ฐ์ฒด ์ƒ์„ฑ์ด ๊ฐ€๋Šฅ
		PersonInfo.PersonInfoBuilder personInfoBuilder = new PersonInfo.PersonInfoBuilder();

		PersonInfo result = personInfoBuilder.setAge(23).setName("Hong").
        				 setFavoriteColor("black")
					.setFavoriteAnimal("cat").build();

		System.out.println(result.getPersonInfo());
	}
}

์ด ๊ฒฝ์šฐ์—๋„ ์ •์ƒ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.


Closing Remarks

์ดํ•ด๋ฅผ ๋•๊ธฐ ์œ„ํ•ด ๋‹ค์ด์–ด๊ทธ๋žจ์„ ์ฒจ๋ถ€ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.


  • Builder class๋ฅผ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด๋‚ผ class์™€ ๋ถ„๋ฆฌํ•œ ๊ฒฝ์šฐ

  • Builder class๋ฅผ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด๋‚ผ class ๋‚ด๋ถ€์— ํฌํ•จํ•œ ๊ฒฝ์šฐ

profile
์Šต๊ด€์ด ์ „๋ถ€๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€