Web Server

ํด๋ผ์ด์–ธํŠธ๊ฐ€ GET, POST ๋“ฑ์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•ด ์ „์†กํ•œ ์š”์ฒญ์„ ๋ฐ›์•„ HTML์ด๋‚˜ ์˜ค๋ธŒ์ ํŠธ๋ฅผ HTTP ํ”„๋กœํ† ์ฝœ์„ ์ด์šฉํ•ด ์ „์†กํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์ด๋‹ค. ์›น ์„œ๋ฒ„๋Š” ์›นํŽ˜์ด์ง€, ์ด๋ฏธ์ง€ ๋“ฑ ์ •์ ์ธ ์ปจํ…์ธ ๋งŒ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

WAS(Web Application Server)

WAS๋ž€ Web Server์™€ Web Container์˜ ๊ฒฐํ•ฉ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ์†Œํ”„ํŠธ์›จ์–ด์ด๋‹ค. Web Server๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— Web Server์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. Web Server์™€ ๋‹ฌ๋ฆฌ WAS๋Š” ๋™์ ์ธ ํŽ˜์ด์ง€๋ฅผ ๋งŒ๋“ค์–ด ์œ ์—ฐํ•˜๊ฒŒ ์‘๋‹ตํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์ด๋‹ค. 

 

Web Server๋Š” ์ •์ ์ธ ์ปจํ…์ธ ๋งŒ ์ œ๊ณตํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํด๋ผ์ด์–ธํŠธ์˜ ์š”๊ตฌ์— ์œ ์—ฐํ•˜๊ฒŒ ๋Œ€์ฒ˜ํ•˜๊ธฐ ์–ด๋ ต๋‹ค. DB์™€ ์—ฐ๊ฒฐํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ ๋ฐ›๊ฑฐ๋‚˜ ๋ฐ์ดํ„ฐ ์กฐ์ž‘์„ ํ•˜์—ฌ ๋™์ ์ธ ํŽ˜์ด์ง€๋ฅผ ์ƒ์„ฑํ•ด ์‘๋‹ตํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” WAS๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค. 

 

 

Tomcat

์•„ํŒŒ์น˜ ์žฌ๋‹จ์—์„œ ๋งŒ๋“  ์˜คํ”ˆ์†Œ์Šค WAS(Web Application Server)์ด๋‹ค. Tomcat์€ Java Servlet๊ณผ JSP๊ฐ€ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ํ™˜๊ฒฝ์„ ์ œ๊ณตํ•˜์—ฌ ๋™์ ์ธ ํŽ˜์ด์ง€๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. DB์—ฐ๊ฒฐ ๋ฐ ๋ฐ์ดํ„ฐ ์กฐ์ž‘, ์‘์šฉํ”„๋กœ๊ทธ๋žจ๊ณผ ์ƒํ˜ธ์ž‘์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

 

Tomcat์—๋Š” ์„œ๋ฒ„๊ฐ€ start ๋˜๋Š” ์‹œ์ ์— ์ฝ์–ด ๋“ค์—ฌ์„œ ๋™์ž‘์„ ์ค€๋น„ํ•˜๋Š” xml ๋ฌธ์„œ๊ฐ€ ์žˆ๋‹ค.

Tomcat/ ํ•˜์œ„์— ์žˆ๋Š” ํด๋”์˜ .xml ํŒŒ์ผ์— ํ™˜๊ฒฝ ์„ค์ •์„ ํ•˜๋ฉด Tomcat ์„œ๋ฒ„๋ฅผ ์ด์šฉํ•˜๋Š” ๋ชจ๋“  ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์— ๋™์ผํ•œ ํ™˜๊ฒฝ์„ค์ •์ด ์ ์šฉ๋œ๋‹ค.

 

- context.xml

- server.xml

- tomcat-users.xml

- web.xml

 

์„œ๋ฒ„์‹คํ–‰์‹œ ์ฝ๋Š” ์ˆœ์„œ

request -> catalina -> context -> servlet -> response

 

context.xml

ํ†ฐ์บฃ์— ๋Œ€ํ•œ ๊ตฌ์ฒด์ ์ธ ํ™˜๊ฒฝ์„ค์ • ์˜ต์…˜(ex access log, data sources, session manager ๋“ฑ)

context ์„ค์ •์€ ํ†ฐ์บฃ์—์„œ ๊ตฌ๋™๋˜๋Š” ํ•˜๋‚˜์˜ ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ž์ฒด์˜ ์„ค์ •์„ ์˜๋ฏธํ•œ๋‹ค. 

ํ†ฐ์บฃ์ด ์š”์ฒญ์„ ๋ฐ›์œผ๋ฉด Catealina๊ฐ€ ์š”์ฒญ์— ๋งž๋Š” Context๋ฅผ ์ฐพ๊ณ ,

Context๋Š” ์ž์‹ ์ด ์„ค์ •๋œ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ deployment description file(web.xml)์„ ๊ธฐ๋ฐ˜์œผ๋กœ 

์ „๋‹ฌ๋ฐ›์€ ์š”์ฒญ์„ Servlet์—๊ฒŒ ์ „๋‹ฌํ•˜์—ฌ ์ฒ˜๋ฆฌ๋˜๋„๋ก ํ•œ๋‹ค. 

 

 

web.xml

web.xml์—์„œ ์„ค์ •ํ•œ ๊ฒƒ์€ ์ „์ฒด ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์— ์ ์šฉ๋œ๋‹ค.

 

 

WEB-INF

src/main/webapp/WEB-INF

Web Information์˜ ์•ฝ์ž์ด๊ณ , WEB-INFํด๋”์—๋Š” ๋ธŒ๋ผ์šฐ์ €์— ์ง์ ‘ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๊ณ , ์˜ค์ง ์„œ๋ฒ„๋‚ด์—์„œ๋งŒ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. 

 

META-INF

src/main/webapp/META-INF

- java์—์„œ ์„ค์ • ๊ด€๋ จ ํŒŒ์ผ์„ ์ €์žฅํ•˜๋Š” ํด๋”

- java ํŒจํ‚ค์ง• ๊ธฐ์ˆ ์ธ jar์˜ ์ผ๋ถ€

 

'๐ŸงProgramming' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Servlet  (0) 2024.01.19
Java๊ฐ€ ํŒŒ์ผ์„ ์ฝ๋„๋ก ํ•˜๋Š” ๋ฒ•  (0) 2024.01.14
Logging | SLF4J  (0) 2024.01.14
React | export์™€ export default์˜ ์ฐจ์ด  (0) 2023.08.12
OCP  (0) 2022.02.21

์ž๋ฐ”ํŒŒ์ผ์—์„œ javax.servlet.Servlet ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•ด์„œ ์ž‘์„ฑ

spring์ด servlet stack๊ธฐ๋ฐ˜ ๊ธฐ์ˆ ์ด๋‹ค. ๊ทธ๋ž˜์„œ ์ž˜ ์•Œ์•„๋†“์•„์•ผํ•œ๋‹ค.

servlet ์ŠคํŽ™์„ ๊ตฌํ˜„ํ•œ Tomcat ์›น ์„œ๋ฒ„๋ฅผ ์„ค์น˜ํ•˜์—ฌ ํ™œ์šฉ ๊ฐ€๋Šฅ

 

Servlet์ด๋ž€

 

ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์–ด๋–ค ์š”์ฒญ์„ ํ•˜๋ฉด ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋‹ค์‹œ ์ „์†กํ•ด์ฃผ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.

์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์„œ๋ฒ„์—์„œ ๋™์ž‘ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ ๊ตฌํ˜„์‹œ ์‚ฌ์šฉํ•œ๋‹ค.

์„œ๋ธ”๋ฆฟ์€ ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์— ๋”ฐ๋ผ ์„œ๋ฒ„๊ฐ€ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ์œ ์ผํ•œ ์ž๋ฐ” ํŒŒ์ผ์ด๋‹ค. 

 

์„œ๋ธ”๋ฆฟ์„ ๋™์ž‘์‹œํ‚ค๊ธฐ ์œ„ํ•œ ์กฐ๊ฑด์€ ์„œ๋ธ”๋ฆฟ API๋ฅผ ํ†ตํ•ด ์ œ๊ณต๋ฐ›์„ ์ˆ˜ ์žˆ์œผ๋ฉฐ, 

์„œ๋ธ”๋ฆฟ์„ ๊ตฌํ˜„ํ•  ๋•Œ ํ•ด๋‹น API์ธ  javax.servlet.http.HttpServlet ๋งŒ ์ƒ์†(extends) ๋ฐ›์œผ๋ฉด ๋จ

 

๋ชจ๋“  ์ž๋ฐ” ํŒŒ์ผ์— extends HttpServlet์„ ์ž‘์„ฑํ•˜์—ฌ ํ•ด๋‹น java ํŒŒ์ผ์„ ์„œ๋ธ”๋ฆฟ์œผ๋กœ ๋™์ž‘ํ•˜๋„๋ก ๊ตฌํ˜„

 

 

 

 

HttpServlet

HttpServlet์„ ์ƒ์†๋ฐ›์€ ํด๋ž˜์Šค๋ฅผ Servlet ์ด๋ผ๊ณ  ํ•จ

HTTP ์š”์ฒญ ๋ฉ”์„œ๋“œ์— ๋”ฐ๋ผ doGet(), doPost(), doPut() ๋“ฑ ์š”์ฒญ ๋ฉ”์„œ๋“œ์— ๋งž๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์žฌ์ •์˜(Overriding)ํ•˜์—ฌ ์‚ฌ์šฉํ•ด์•ผํ•จ

 

doGet() : ํ•ด๋‹น ์„œ๋ธ”๋ฆฟ์ด HTTP GET ์š”์ฒญ์„ ๋ฐ›์•˜์„ ๋•Œ ๋™์ž‘ํ•˜๋Š” ๋ฉ”์„œ๋“œ

doPost() : ํ•ด๋‹น ์„œ๋ธ”๋ฆฟ์ด HTTP POST ์š”์ฒญ์„ ๋ฐ›์•˜์„ ๋•Œ ๋™์ž‘ํ•˜๋Š” ๋ฉ”์„œ๋“œ

๊ทธ ์™ธ ์ˆ˜๋ช…์ฃผ๊ธฐ์™€ ๊ด€๋ จ๋œ init(), destory()

 

class HelloServlet extends HttpServlet { ... }

 

 

Servlet์„ ์™œ ์“ฐ๋Š”์ง€

์‚ฌ์šฉ์ž์˜ ์š”์ฒญ์— ์˜ํ•ด ์ƒ์„ฑ๋œ ์„œ๋ธ”๋ฆฟ ๊ฐ์ฒด๋Š” ์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ ๋‚ด๋ถ€์— ๋ณด๊ด€ํ•œ๋‹ค. ์ดํ›„ ๋™์ผํ•œ ๊ฒฝ๋กœ๋กœ ์š”์ฒญ์ด ์˜ฌ ๊ฒฝ์šฐ, 

๋ณด๊ด€ํ•˜๊ณ  ์žˆ๋˜ ์„œ๋ธ”๋ฆฟ์„ ์žฌ์‚ฌ์šฉํ•˜์—ฌ ์š”์ฒญ ์‹œ๋งˆ๋‹ค ์ƒˆ๋กญ๊ฒŒ ์„œ๋ธ”๋ฆฟ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์ง€ ์•Š๋Š”๋‹ค. 

 

์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์„œ๋ธ”๋ฆฟ ํด๋ž˜์Šค๋“ค์„ ์ธ์Šคํ„ด์Šคํ™”ํ•˜๊ณ 

์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ ๋‚ด๋ถ€์— ๋ณด๊ด€ํ•˜๊ณ  ์žˆ๋‹ค๊ฐ€ ์š”์ฒญ์— ๋”ฐ๋ผ ํ•ด๋‹น ์„œ๋ธ”๋ฆฟ ๊ฐ์ฒด๋ฅผ ์žฌ์‹คํ–‰ ์‹œ์ผœ์ฃผ๋Š” ์—ญํ• ์„ ํ•จ

ํด๋ผ์ด์–ธํŠธ๋กœ๋ถ€ํ„ฐ์˜ ์š”์ฒญ์ด ์ตœ์ดˆ ์š”์ฒญ์ธ์ง€ ์•„๋‹Œ์ง€์— ๋”ฐ๋ผ ํ•ด๋‹น ์š”์ฒญ์— ๋งž๋Š” ์„œ๋ธ”๋ฆฟ ๊ฐ์ฒด์˜ ์ƒ์„ฑ ์—ฌ๋ถ€๊ฐ€ ๊ฒฐ์ •๋จ

์ด ์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ ์ค‘ ๋Œ€ํ‘œ์ ์œผ๋กœ Apache Tomcat์ด ์žˆ๋‹ค.

 

@WebServlet(urlPatterns="/first-request-servlet")
public class FirstHttpRequestServlet extends HttpServlet {
	
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("request" + req);
		System.out.println("response" + resp);
	}
}

 

 

pom.xml

Maven์˜ ๋นŒ๋“œ ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” ํŒŒ์ผ. POM(Project Object Model)์„ ์„ค์ •ํ•˜๋Š” ๋ถ€๋ถ„์œผ๋กœ, ํ”„๋กœ์ ํŠธ ๋‚ด ๋นŒ๋“œ ์˜ต์…˜์„ ์„ค์ •ํ•˜๋Š” ๋ถ€๋ถ„์ด๋‹ค. ๋‹ค๋ฅธ ํ”„๋กœ์ ํŠธ์—์„œ๋„ ๋ณต๋ถ™์ด ๊ฐ€๋Šฅํ•˜์—ฌ, ํ˜‘์—…ํ•  ๋•Œ ๋ฒ„์ „ ํ†ต์ผํ•  ๋•Œ ํŽธํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ์—ฐ๊ฒฐ์„ ํ•ด์•ผํ•  ๋•Œ dependency๋กœ ์ถ”๊ฐ€ํ•˜๋Š” ๋ถ€๋ถ„์ด ์žˆ์—ˆ๋‹ค.

<dependency>
	<groupId>mysql</groupId>
    	<artifactId>mysql-connector-java</artifactId>
    	<version>8.0.13</version>
</dependency>

 

web.xml

์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์„œ๋น„์Šค ์ฒ˜๋ฆฌ์— ๊ด€ํ•œ ๋‚ด์šฉ์ด ์ •์˜๋œ ํ™˜๊ฒฝ ์„ค์ • ํŒŒ์ผ.

์›น ์„œ๋ฒ„๋Š” web.xml์— ์ •์˜๋œ ๋‚ด์šฉ๋Œ€๋กœ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•œ ์„ค์ •์„ ์ˆ˜ํ–‰ํ•จ

์„œ๋ฒ„๊ฐ€ ๊ธฐ๋™๋  ๋•Œ ์ด ํŒŒ์ผ์„ ์ฝ๋Š”๋‹ค. node.js ํ™˜๊ฒฝ์—์„œ package.json๊ณผ ๊ฐ™์€ ์—ญํ• ์„ ํ•œ๋‹ค.

๋ชจ๋“  Web Application์€ ๋ฐ˜๋“œ์‹œ ํ•˜๋‚˜์˜ web.xml ํŒŒ์ผ์„ ๊ฐ€์ ธ์•ผํ•˜๊ณ , ์œ„์น˜๋Š” WEB-INFํด๋” ํ•˜์œ„์— ์žˆ๋‹ค.

web.xmlํŒŒ์ผ์˜ ์„ค์ •๋“ค์€ Web Application ์‹œ์ž‘ ์‹œ ๋ฉ”๋ชจ๋ฆฌ์— ๋กœ๋”ฉ๋œ๋‹ค.

Application์— ๋Œ€ํ•œ ์š”์ฒญ์ด ์ˆ˜์‹ ๋˜๋ฉด web.xml์„ ์‚ฌ์šฉํ•˜์—ฌ ์š”์ฒญ์˜ url์„ ํ•ด๋‹น ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•ด์•ผํ•˜๋Š” ์ฝ”๋“œ์— ๋งคํ•‘ํ•œ๋‹ค.

<web-app>
	<!-- alias -->
    <servlet>
    	<servlet-name>title</servlet-name>
    	<servlet-class>servlets.Servlet</servlet-class>
    </servlet>
    
    <!-- mapping -->
    <servlet-mapping>
    	<servlet-name>title</servlet-name>
    	<url-pattern>title</url-pattern>
    </servlet-mapping>
</web-app>

 

 

WebServlet

@WebServlet(urlPatterns="/first-request-servlet")
public class FirstHttpRequestServlet extends HttpServlet {
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("request" + request);
		System.out.println("response" + response);
		
		// ์š”์ฒญ๋ฉ”์„ธ์ง€ ์•ˆ์— ๋ญ๊ฐ€ ๋‹ด๊ฒผ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•œ ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ์Œ
		request.getContextPath();
		request.getRequestURI();
		
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		
		// Header ๋ฉ”์‹œ์ง€ ๋‹ด๊ธด ์ •๋ณด ์ถ”์ถœ
		PrintWriter out = response.getWriter();
		out.println("<html>");
		out.println("<body>");
		out.println("<head>");
		out.println("<title>Request Information Example</title>");
		out.println("</head>");
		
		out.println("<body>");
		
		out.println("<h3>์š”์ฒญ ๋ฉ”์‹œ์ง€์— ๋‹ด๊ธด ์ •๋ณด ํ™•์ธ</h3>");
		
		// Header ๋ฉ”์‹œ์ง€์— ๋‹ด๊ธด ์ •๋ณด ์ถ”์ถœ
		out.println("Context Path : " + request.getContextPath() + "<br/>");
		out.println("Request URL : " + request.getRequestURL() + "<br/>");
		out.println("Request URI : " + request.getRequestURI() + "<br/>");
		out.println("Server Port : " + request.getServerPort() + "<br/>");
		out.println("Request Protocol : " + request.getProtocol() + "<br/>");
		out.println("Request METHOD: " + request.getMethod() + "<br/>");
		out.println("Query String : " + request.getQueryString() + "<br/>");// http://localhost:8080/step02servletsyntax/first-request-servlet?name=jerry&age=15
		out.println("Parameter(name) : " + request.getParameter("name") + "<br/>");
		out.println("Parameter(age) : " + request.getParameter("age") + "<br/>");
		
		out.println("</body>");
        out.println("</html>");
        
		out.close();
	}
}

 

์š”์ฒญ์— ๋‹ด๊ธด ์ •๋ณด๋“ค์„ ์„œ๋ฒ„๊ฐ€ ์•Œ ์ˆ˜ ์žˆ๊ฒŒ ๊บผ๋‚ด์•ผํ•จ

HttpServlet ๊ฐ์ฒด๊ฐ€ ์ด ์ •๋ณด๋“ค์„ ๊ฐ€์ง€๊ณ  ์žˆ๊ณ , request์™€ response ์ •๋ณด๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.

 

 

/*
 * 2. Annotation ๊ธฐ๋ฐ˜ ๋งตํ•‘
 * 
 * XML์„ ํ†ตํ•œ ๊ด€๋ จ ๋ฐฉ์‹์˜ ๋ฒˆ๊ฑฐ๋กœ์›€
 * ์ถ”์ƒํ™”๋˜๊ณ  ์ง๊ด€์ ์ธ Annotation ํ‘œ๊ธฐ๋ฒ•์œผ๋กœ ๋งตํ•‘ ์ฒ˜๋ฆฌ๋ฅผ ๊ฐ„์†Œํ™”
 * 
 *  * @WebServlet
 * https://github.com/javaee/servlet-spec/blob/master/src/main/java/javax/servlet/annotation/WebServlet.java
 */
@WebServlet(name = "MySecondServlet", urlPatterns = "/servlets/secondservlet")
/*
 * web.xml์˜ ์•„๋ž˜ ์„ค์ •๊ณผ ๋™์ผํ•จ
 * <servlet-name>SecondServlet </servlet-name>
 * <url-pattern>/servlets/secondservlet</url-pattern>
 */
public class SecondeServlet extends HttpServlet {
	// get์š”์ฒญ์„ ํ†ตํ•ด hello second world๊ฐ€ ์ถœ๋ ฅ๋˜๋„๋ก ํ…Œ์ŠคํŠธ
	// ์„œ๋ฒ„ ์‘๋‹ต์‹œ HTTP Header์— ์ธ์ฝ”๋”ฉ์€ UTF-8๋กœ ์ฒ˜๋ฆฌ๋  ์ˆ˜ ์žˆ๋„๋ก ํ—ค๋” ์ฝ”๋“œ๋„ ์ถ”๊ฐ€
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("text/html");
		resp.setCharacterEncoding("UTF-8");
		
		PrintWriter out = resp.getWriter();
		out.println("<html>");
        out.println("<head>");
        out.println("<title>Hello Second World!</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Hello Second World!</h1>");
        out.println("</body>");
        out.println("</html>");
	}
}

 

 

Url-patterns

 

'๐ŸงProgramming' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Tomcat  (0) 2024.01.22
Java๊ฐ€ ํŒŒ์ผ์„ ์ฝ๋„๋ก ํ•˜๋Š” ๋ฒ•  (0) 2024.01.14
Logging | SLF4J  (0) 2024.01.14
React | export์™€ export default์˜ ์ฐจ์ด  (0) 2023.08.12
OCP  (0) 2022.02.21

Maven Repository์—์„œ ํ•„์š”ํ•œ ๋ชจ๋“ˆ์„ ๊ฐ€์ ธ์™€์„œ ์จ์•ผํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์ƒ๊ฒผ๋‹ค.

์ž๋ฐ”์—์„œ๋Š” ํ”„๋กœ์ ํŠธ์— ์–ด๋–ป๊ฒŒ ๋„ฃ์–ด์ค˜์•ผํ• ๊นŒ

 

 

Build Path

์ž๋ฐ” ํ”„๋กœ์ ํŠธ๋ฅผ ๋นŒ๋“œํ•˜๋Š”๋ฐ ํ•„์š”ํ•œ ๋ชจ๋“  ๋ฆฌ์†Œ์Šค ํŒŒ์ผ๋“ค์ด ๋ชจ์—ฌ์žˆ๋Š” ๊ฒฝ๋กœ์ด๋‹ค.

์ดํด๋ฆฝ์Šค์—์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฝ๋กœ์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค.

 

๋Œ€๋ถ€๋ถ„์˜ ํŽธ์ง‘๊ธฐ์—์„œ๋Š” ์ž๋ฐ” ํ”„๋กœ์ ํŠธ๋ฅผ ๋นŒ๋“œํ•˜๊ณ  ์ปดํŒŒ์ผํ•˜๊ธฐ ์œ„ํ•ด build path๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ํ•œ๋‹ค.

์†Œ์Šค์ฝ”๋“œ๋Š” .java

์‹คํ–‰ํŒŒ์ผ์€ .class

 

Build Path๋Š” ์˜์กด์„ฑ์„ ์ฐพ๋Š” ๋นŒ๋“œ๊ณผ์ •์—์„œ ์‚ฌ์šฉ๋œ๋‹ค.

 

Class Path

ํด๋ž˜์Šค ํŒจ์Šค๋Š” JVM์ด ์‚ฌ์šฉํ•˜๋Š” ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋กœ, JVM์ด ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰์‹œ ํด๋ž˜์Šค๋ฅผ ์ฐพ๊ณ  ๋กœ๋”ฉํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋œ๋‹ค. JVM์ด ํด๋ž˜์Šค ํŒŒ์ผ์„ ์ฐพ๊ณ  ๋กœ๋“œํ•ด์•ผํ•˜๋Š” ๋””๋ ‰ํ† ๋ฆฌ, jar ํŒŒ์ผ ๋ฐ zip ํŒŒ์ผ ๋ชฉ๋ก์„ ์ง€์ •ํ•ด์•ผํ•œ๋‹ค.

 

Class Path๋Š” ์†Œ์Šค ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰๋˜๋Š” ๋Ÿฐํƒ€์ž„์— ์‚ฌ์šฉ๋œ๋‹ค. (๋นŒ๋“œ์‹œ์—๋„ ์‚ฌ์šฉ๋œ๋‹ค)

 

 

 Add External JARs ๋ฅผ ํ†ตํ•ด์„œ .jar ํŒŒ์ผ์„ ์ฃผ์ž…ํ•ด์ฃผ์—ˆ๋‹ค. 
 

'๐ŸงProgramming' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Tomcat  (0) 2024.01.22
Servlet  (0) 2024.01.19
Logging | SLF4J  (0) 2024.01.14
React | export์™€ export default์˜ ์ฐจ์ด  (0) 2023.08.12
OCP  (0) 2022.02.21

Logging

๋กœ๊น…์ด๋ž€ ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰ ์ค‘์— ๋ฐœ์ƒํ•˜๋Š” ์˜ค๋ฅ˜์— ๋Œ€ํ•ด ๋””๋ฒ„๊น…ํ•˜๊ฑฐ๋‚˜ ์šด์˜ ์ค‘์ธ ํ”„๋กœ๊ทธ๋žจ์„ ๋ชจ๋‹ˆํ„ฐ๋ง ํ•˜๊ธฐ ์œ„ํ•ด ๋กœ๊ทธ๋ฅผ ๊ธฐ๋กํ•˜๋Š” ๊ฒƒ. ์ฝ˜์†”์— ์ถœ๋ ฅํ•˜๊ฑฐ๋‚˜ ํŒŒ์ผ ๋“ฑ์— ์ „๋‹ฌํ•˜์—ฌ ์ €์žฅํ•˜๋Š” ํ–‰์œ„

 

SLF4J

ํ’€ ๋„ค์ž„์€ Simple Logging Facade for Java (SLF4J) ๋ผ๋Š” ์ด๋ฆ„์—์„œ ์•Œ ์ˆ˜ ์žˆ๋“ฏ์ด ๋กœ๊น…์— ๋Œ€ํ•œ ์ถ”์ƒํ™”(์ธํ„ฐํŽ˜์ด์Šค) ์—ญํ• ์„ ํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๋‹ค. ์ด๋ฆ„์—์„œ๋ถ€ํ„ฐ Facade๊ฐ€ ๋“ค์–ด๊ฐ€์žˆ๋‹ค. ์ฆ‰, Facade๋ฅผ ํ†ตํ•ด์„œ ํด๋ผ์ด์–ธํŠธ ์ฝ”๋“œ์—์„œ๋Š” ์‹ค์ œ ๊ตฌํ˜„์„ ๋ชจ๋ฅด๊ณ  ๋กœ๊น… ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

Facade Pattern

Facade๋Š” ๊ฑด์ถ•์—์„œ ๊ฑด๋ฌผ ์™ธ๊ด€์ด๋ผ๋Š” ๋œป์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ์šฉ์–ด์ด๋‹ค. ์ด์™€๊ฐ™์ด ๋‚ด๋ถ€์ ์œผ๋กœ ๋ณต์žกํ•œ ์ฝ”๋“œ๋ฅผ ๊ฐ€์ ธ์ฃผ๋Š” ์ƒ์œ„ ์ˆ˜์ค€์˜ ์ธํ„ฐํŽ˜์ด์Šค ์—ญํ• ์„ ํ•˜๋Š” ๊ฐ์ฒด๊ฐ€ ์žˆ๋Š” ํŒจํ„ด์ด๋‹ค. 

 

์ด์™€ ๊ฐ™์ด ์ฝ”๋“œ ์ƒ์—์„œ๋„ ๋ณต์žกํ•œ ์„œ๋ธŒ์‹œ์Šคํ…œ์„ ์™ธ๋ถ€์—์„œ ๊ฐ์‹ธ๊ณ  ์žˆ๋Š” ๋Š๋‚Œ์ด๋‹ค. ๊ณ„์ขŒ๋ฅผ ๋ถ„์„ํ•ด์ฃผ๋Š” ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•  ๋•Œ, ๋ฐ”๋กœ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜์ง€ ์•Š๊ณ , anylze()๋ผ๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด์„œ ์‹คํ–‰์„ ์‹œ์ผฐ์—ˆ๋‹ค. ์ด๋•Œ analyze๊ฐ€ ์ „๋ฉด์—์„œ ์ธํ„ฐํŽ˜์ด์Šค ์—ญํ• ์„ ํ•ด์ฃผ์—ˆ๋‹ค. ์‹ค์ œ analyze() ๋‚ด๋ถ€ ๋กœ์ง์—์„œ ํŒŒ์„œ๊ฐ€ ์‹คํ–‰๋˜๊ณ  ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ ๋“ฑ์„ ํ•˜๋Š” ๋กœ์ง์ด ๋Œ์•„๊ฐ€๊ฒŒ ๋˜์—ˆ๋‹ค. 

์ด ํŒจํ„ด์„ ํ†ตํ•ด ๋ณต์žก์„ฑ์„ ์ค„์ด๊ณ  ์˜์กด์„ฑ์„ ๋‚ฎ์ถœ ์ˆ˜ ์žˆ๋‹ค. 

 

SLF4J์—์„œ ์‚ฌ์šฉ๋˜๋Š” Facade Pattern

SLF4J์—์„œ facade์—ญํ• ์„ ํ•˜๋Š” ์ธํ„ฐํŽ˜์ด์Šค๋Š” ๋ฌด์—‡์ผ๊นŒ? ๋ฐ”๋กœ logger ๊ฐ์ฒด์ด๋‹ค. ๊ทธ๋ž˜์„œ ๊ตฌํ˜„๋ถ€์— ์ƒ๊ด€์—†์ด logging ์ฒ˜๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์‹ค์ œ๋กœ SLF4J๋Š” logback ๋“ฑ ํŒจํ‚ค์ง€ ์•ˆ์— ๋‹ค๋ฅธ ๋ชจ๋“ˆ์„ ํฌํ•จํ•˜๊ณ  ์žˆ๋‹ค.

 

class diagram์œผ๋กœ ๋ณธ ๊ฐœ๋…

Client ํด๋ž˜์Šค๋Š” Subsystem์ธ ํ•˜์œ„ ๊ฐ์ฒด์— ์ง์ ‘ ์ ‘๊ทผํ•˜์ง€ ์•Š๊ณ  Facade ๊ฐ์ฒด์—๋งŒ ์ ‘๊ทผํ•œ๋‹ค. Client๋Š” Facade ์ธํ„ฐํŽ˜์ด์Šค์—๋งŒ ์˜์กดํ•˜๊ณ  ๋ณต์žกํ•œ ํ•˜์œ„ ์‹œ์Šคํ…œ๊ณผ๋Š” ๋…๋ฆฝ์ ์ด๋‹ค.

 

 

 

seqeunce diagram์œผ๋กœ ๋ณธ ๊ฐœ๋…

์œ„ ์‹œํ€€์Šค ๋‹ค์ด์–ด๊ทธ๋žจ์„ ํ†ตํ•ด์„œ ๋Ÿฐํƒ€์ž„์‹œ Client ์™€ Facade์˜ ์ƒํ˜ธ์ž‘์šฉ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

 

SLF4J ๋™์ž‘๊ณผ์ •

- SLF4J API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋กœ๊น… ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค

- ๋ฐฐํฌํ•  ๋•Œ, ๋ฐ”์ธ๋”ฉ ๋œ ๋กœ๊น… ํ”„๋ ˆ์ž„์›Œํฌ๊ฐ€ ์‹ค์ œ ๋กœ๊น… ์ฝ”๋“œ๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค.

 

 

 

์ฐธ๊ณ ์ž๋ฃŒ

 

Facade pattern - Wikipedia

From Wikipedia, the free encyclopedia Software design pattern The facade pattern (also spelled façade) is a software-design pattern commonly used in object-oriented programming. Analogous to a facade in architecture, a facade is an object that serves as a

en.wikipedia.org

 

 

SLF4J - Wikipedia

From Wikipedia, the free encyclopedia Simple Logging Facade for Java (SLF4J) provides a Java logging API by means of a simple facade pattern. The underlying logging backend is determined at runtime by adding the desired binding to the classpath and may be

en.wikipedia.org

 

'๐ŸงProgramming' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Servlet  (0) 2024.01.19
Java๊ฐ€ ํŒŒ์ผ์„ ์ฝ๋„๋ก ํ•˜๋Š” ๋ฒ•  (0) 2024.01.14
React | export์™€ export default์˜ ์ฐจ์ด  (0) 2023.08.12
OCP  (0) 2022.02.21
Recoil | Selector  (0) 2021.12.01

ํ•˜๋‚˜์˜ ํŒŒ์ผ์€ ํ•˜๋‚˜์˜ ๋ชจ๋“ˆ์ž„

์ด ๋ชจ๋“ˆ์„ exportํ•ด์„œ ์จ์•ผํ•˜๋ฏ€๋กœ export๋ฅผ ํ•ด์ฃผ๋Š”๋ฐ

 

export๋งŒ ํ•˜๋ฉด ํ•ด๋‹น ๋ชจ๋“ˆ์•ˆ์— ๋ณต์ˆ˜์˜ ๊ฐœ์ฒด๊ฐ€ ์žˆ์„ ๋•Œ

์—ฌ๋Ÿฌ๊ฐœ๋ฅผ export ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์—๋งŒ export ์‚ฌ์šฉ

 

ํ•ด๋‹น ๋ชจ๋“ˆ์˜ ์ „์ฒด๋ฅผ export ํ• ๋•Œ

export default๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

'๐ŸงProgramming' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Servlet  (0) 2024.01.19
Java๊ฐ€ ํŒŒ์ผ์„ ์ฝ๋„๋ก ํ•˜๋Š” ๋ฒ•  (0) 2024.01.14
Logging | SLF4J  (0) 2024.01.14
OCP  (0) 2022.02.21
Recoil | Selector  (0) 2021.12.01

if-else ๋ฌธ์ œ์ 

- ๋ณ€๊ฒฝ, ํ™•์žฅ์ด ๋  ์ˆ˜๋ก ์ฝ”๋“œ๊ฐ€ ๋ณต์žกํ•ด์ง„๋‹ค.

- ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•˜๊ฑฐ๋‚˜ ์ˆ˜์ •ํ•  ์œ„์น˜๋ฅผ ์ฐพ๋Š”๋ฐ ์ ์  ์˜ค๋ž˜๊ฑธ๋ฆฐ๋‹ค.

- ์‹ค์ˆ˜๋กœ ์ถ”๊ฐ€ํ•˜์ง€์•Š๊ณ  ๋ˆ„๋ฝํ•˜๋Š” ๋ถ€๋ถ„์ด ์ƒ๊ธธ ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๋‹ค.

์ฆ‰, ์œ ์ง€๋ณด์ˆ˜๊ฐ€ ์ ์  ์–ด๋ ค์›Œ์ง„๋‹ค.

 

OCP ๊ฐœ๋ฐฉ ํ์‡„ ์›์น™

์†Œํ”„ํŠธ์›จ์–ด์˜ ๊ตฌ์„ฑ ์š”์†Œ(์ปดํฌ๋„ŒํŠธ, ํด๋ž˜์Šค, ๋ชจ๋“ˆ, ํ•จ์ˆ˜)๋Š”

ํ™•์žฅ์— ๋Œ€ํ•ด์„œ๋Š” ๊ฐœ๋ฐฉ๋˜์–ด์•ผํ•˜์ง€๋งŒ, ๋ณ€๊ฒฝ์— ๋Œ€ํ•ด์„œ๋Š” ํ์‡„๋˜์–ด์•ผ ํ•œ๋‹ค.

๊ธฐ์กด์˜ ์ฝ”๋“œ๋ฅผ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š์œผ๋ฉด์„œ ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ค๊ณ„๊ฐ€ ๋˜์–ด์•ผํ•œ๋‹ค

 

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

๊ธฐ๋Šฅ์ด ์ถ”๊ฐ€, ๋ณ€๊ฒฝ๋˜์–ด๋„ ๊ธฐ์กด ์ฝ”๋“œ๋Š” ๋ณ€๊ฒฝ๋˜์ง€ ์•Š๋Š”๋‹ค. (ํ™•์žฅ์ด ์‰ฝ๋‹ค)

 

OCP ์ ์šฉ ๋ฐฉ๋ฒ•

1. ์ƒ์†์„ ์ด์šฉํ•œ ๋ฐฉ๋ฒ• - ํ…œํ”Œ๋ฆฟ ๋ฉ”์†Œ๋“œ ํŒจํ„ด

๊นจ์ง€๊ธฐ ์‰ฌ์šด ์ƒ์œ„ ํด๋ž˜์Šค ๋ฌธ์ œ

 

 

2. ์ปดํฌ์ง€์…˜์„ ์ด์šฉํ•œ ๋ฐฉ๋ฒ• - ์ „๋žต ํŒจํ„ด

์ƒ์†๋ณด๋‹ค๋Š” ์ปดํฌ์ง€์…˜์„ ์ถ”์ฒœ

1) ๋ณ€๊ฒฝ(ํ™•์žฅ)๋  ๊ฒƒ๊ณผ ๋ณ€ํ•˜์ง€ ์•Š์„ ๊ฒƒ์„ ๊ตฌ๋ถ„ 

๋ณ€๊ฒฝ๋˜๋Š” ๋ถ€๋ถ„์„ ์ธํ„ฐํŽ˜์ด์Šค๋กœ ์ถ”์ถœ

2) ์ด ๋‘ ๋ชจ๋“ˆ์ด ๋งŒ๋‚˜๋Š” ์ง€์ ์— ์ธํ„ฐํŽ˜์ด์Šค ์ •์˜

์ธํ„ฐํŽ˜์ด์Šค์— ์˜์กดํ•˜๋„๋ก ์ž‘์„ฑ(๊ธฐ๋Šฅ ๋ณ€๊ฒฝ์„ ์œ„ํ•ด ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์—)

3) ๊ตฌํ˜„์— ์˜์กดํ•˜๊ธฐ๋ณด๋‹ค ์ •์˜ํ•œ ์ธํ„ฐํŽ˜์ด์Šค์— ์˜์กดํ•˜๋„๋ก ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑ

 

์ž๊ธฐ๊ฐ€ ์ง์ ‘ new ์ƒ์„ฑ์ž๋กœ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜์ง€ ์•Š๊ณ , ์™ธ๋ถ€์—์„œ ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ›๋Š” ๊ฒƒ์„ ์˜์กด์„ฑ ์ฃผ์ž…(Dependency Injection, DI)์ด๋ผ๊ณ  ํ•œ๋‹ค.

 

Dependency Injection ์˜์กด์„ฑ ์ฃผ์ž…
์†Œํ”„ํŠธ์›จ์–ด ๊ฐœ๋ฐœ์—์„œ ์˜์กด์„ฑ ์ฃผ์ž…์€ ๊ฐ์ฒด๊ฐ€ ์˜์กดํ•˜๋Š” ๋‹ค๋ฅธ ๊ฐ์ฒด๋ฅผ ๋ฐ›๋Š” ๊ฒƒ์ด๋‹ค. 
์˜์กด์„ฑ ์ฃผ์ž…์˜ ์˜๋„๋Š” ๊ฐ์ฒด์˜ ์ƒ์„ฑ๊ณผ ์‚ฌ์šฉ์˜ ๊ด€์‹ฌ์„ ๋ถ„๋ฆฌํ•˜๋Š” ๊ฒƒ์ด๋‹ค. → ๊ฐ€๋…์„ฑ๊ณผ ์ฝ”๋“œ ์žฌ์‚ฌ์šฉ์„ ๋†’์—ฌ์ค€๋‹ค.

์†Œํ”„ํŠธ์›จ์–ด ๊ฐœ๋ฐœ์—์„œ ์˜์กด์„ฑ ์ฃผ์ž…์€ ๊ฐ์ฒด๊ฐ€ ์˜์กดํ•˜๋Š” ๋‹ค๋ฅธ ๊ฐ์ฒด๋ฅผ ๋ฐ›๋Š” ๊ฒƒ์ด๋‹ค. 
์˜์กด์„ฑ ์ฃผ์ž…์˜ ์˜๋„๋Š” ๊ฐ์ฒด์˜ ์ƒ์„ฑ๊ณผ ์‚ฌ์šฉ์˜ ๊ด€์‹ฌ์„ ๋ถ„๋ฆฌํ•˜๋Š” ๊ฒƒ์ด๋‹ค. → ๊ฐ€๋…์„ฑ๊ณผ ์ฝ”๋“œ ์žฌ์‚ฌ์šฉ์„ ๋†’์—ฌ์ค€๋‹ค.

์˜์กด์„ฑ ์ฃผ์ž…์˜ ์ ์šฉ ์œ ํ˜•
- ์ƒ์„ฑ์ž ์ฃผ์ž… : ํ•„์š”ํ•œ ์˜์กด์„ฑ์„ ๋ชจ๋‘ ํฌํ•จํ•˜๋Š” ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ค๊ณ , ๊ทธ ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด ์˜์กด์„ฑ ์ฃผ์ž…
- setter๋ฅผ ํ†ตํ•œ ์ฃผ์ž… : ์˜์กด์„ฑ์„ ์ž…๋ ฅ๋ฐ›๋Š” setter ๋ฉ”์†Œ๋“œ๋ฅผ ๋งŒ๋“ค๊ณ  ์ด๋ฅผ ํ†ตํ•ด ์˜์กด์„ฑ ์ฃผ์ž…
- ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ†ตํ•œ ์ฃผ์ž… : ์˜์กด์„ฑ์„ ์ฃผ์ž…ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ํฌํ•จํ•œ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ž‘์„ฑํ•˜๊ณ  ์ด ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋„๋ก ํ•จ์œผ๋กœ์จ ์‹คํ–‰ ์‹œ์— ์˜์กด์„ฑ ์ฃผ์ž…

 

์œ„ํ‚ค๋ฐฑ๊ณผ ์˜์กด์„ฑ ์ฃผ์ž…

 

OCP๋ฅผ ์ค€์ˆ˜ํ•˜๊ธฐ ์œ„ํ•œ ๋””์ž์ธ ํŒจํ„ด

์ „๋žตํŒจํ„ด

์ „๋žตํŒจํ„ด์ด๋ž€ ๊ธฐ์กด์˜ ์ฝ”๋“œ ๋ณ€๊ฒฝ์—†์ด ํ–‰์œ„๋ฅผ ์ž์œ ๋กญ๊ฒŒ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” OCP๋ฅผ ์ค€์ˆ˜ํ•œ ๋””์ž์ธ ํŒจํ„ด 

 

๋””์ž์ธ ํŒจํ„ด์˜ ๊ฝƒ

์ „๋žต์„ ์‰ฝ๊ฒŒ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ๋Š” ๋””์ž์ธ ํŒจํ„ด

ํ–‰์œ„๋ฅผ ํด๋ž˜์Šค๋กœ ์บก์Šํ™”ํ•ด ๋™์ ์œผ๋กœ ํ–‰์œ„๋ฅผ ์ž์œ ๋กญ๊ฒŒ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ํŒจํ„ด

์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ์˜ ์ถ”๊ฐ€๊ฐ€ ๊ธฐ์กด์˜ ์ฝ”๋“œ์— ์˜ํ–ฅ์„ ๋ฏธ์น˜์ง€ ๋ชปํ•˜๊ฒŒ ํ•˜๋ฏ€๋กœ OCP๋ฅผ ๋งŒ์กฑ

 

 

์ฐธ๊ณ ์ž๋ฃŒ

 

OCP(Open Closed Principle)์ด๋ž€?

 

nesoy.github.io

 

'๐ŸงProgramming' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Servlet  (0) 2024.01.19
Java๊ฐ€ ํŒŒ์ผ์„ ์ฝ๋„๋ก ํ•˜๋Š” ๋ฒ•  (0) 2024.01.14
Logging | SLF4J  (0) 2024.01.14
React | export์™€ export default์˜ ์ฐจ์ด  (0) 2023.08.12
Recoil | Selector  (0) 2021.12.01

Selector 

atom์˜ output์„ ๋ณ€ํ˜•์‹œํ‚ค๋Š” ํ•จ์ˆ˜๋กœ, atom์— ์˜์กดํ•˜๋Š” ๋™์ ์ธ ๋ฐ์ดํ„ฐ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค.

selecter๋Š” get ํ•จ์ˆ˜์™€ set ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. selector๋ฅผ ์ด์šฉํ•ด state๋ฅผ ๊ฐ€์ ธ์™€์„œ output์„ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

 

get

get: ({ get }) => {
	// ๊ฐ€์ ธ์˜ฌ ๋‚ด์šฉ
	// get(๊ฐ€์ ธ์˜ฌ ๊ฐ’)
}

์ค‘๊ด„ํ˜ธ๋ฅผ ์—ด๊ณ  ์“ฐ๋ฉด get์ด๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ๋‹ค์‹œ ๋ถˆ๋Ÿฌ์„œ state๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ์–ด๋–ค ๊ฐ’์„ return ํ•˜๋˜์ง€ ๊ฐ„์— ๊ทธ ๊ฐ’์€ selector์˜ ๊ฐ’์ด ๋œ๋‹ค. (atom์„ ๊ฐ€์ ธ์™€์„œ selector๋กœ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๋œป)

get์„ ์‚ฌ์šฉํ•ด atom๊ณผ ๋‹ค๋ฅธ selector์— ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜๋‹ค. ์ข…์† ๊ด€๊ณ„๊ฐ€ ์ƒ๊ธฐ๋ฏ€๋กœ ์ฐธ์กฐํ•œ ๊ฐ’์ด ์—…๋ฐ์ดํŠธ ๋˜๋ฉด ํ•ด๋‹น ํ•จ์ˆ˜๋„ ๋‹ค์‹œ ์‹คํ–‰๋œ๋‹ค.

 

set

set: ({ set }, newValue) => {
	// ๋ณ€๊ฒฝ๋‚ด์šฉ
	// set(๊ฐ€์ ธ์˜จ ๊ฐ’, ๋ณ€๊ฒฝ(return)์‹œํ‚ฌ ๊ฐ’)
}

set ํ•จ์ˆ˜๋Š” ๋ณ€๊ฒฝ๋  ์ƒˆ๋กœ์šด ๊ฐ’์„ ๋‘ ๋ฒˆ์งธ ์ธ์ž๋กœ  ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค. 

 

 

 

์˜ˆ์ œ

atom์œผ๋กœ minute๋ฅผ ์ƒ์„ฑํ•˜๊ณ , selector๋กœ atom์˜ ๊ฐ’์„ ๊ฐ€์ ธ์™€์„œ ๋ณ€๊ฒฝ์‹œํ‚ค๋Š” ์˜ˆ์ œ์ด๋‹ค. '๋ถ„'์„ ์ž…๋ ฅํ•˜๋ฉด '์‹œ๊ฐ„'์œผ๋กœ ๋ฐ”๊ฟ”์ฃผ๊ณ , '์‹œ๊ฐ„'์„ ์ž…๋ ฅํ•˜๋ฉด '๋ถ„'์œผ๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค.

// atom.tsx

import { atom, selector } from 'recoil';

export const minuteState = atom({
  key: 'minute',
  default: 0,
});

export const hourSelector = selector<number>({
  key: 'hours',
  get: ({ get }) => {
    const minutes = get(minuteState);
    return minutes / 60;
  },
  set: ({ set }, newValue) => {
    const minute = Number(newValue) * 60;
    set(minuteState, minute);
  },
});
// App.tsx

import React from 'react';
import { useRecoilState, useRecoilValue, useRecoilValueLoadable } from 'recoil';
import { hourSelector, minuteState } from './atom';

function App() {
  const [minutes, setMinutes] = useRecoilState(minuteState);
  const [hours, setHours] = useRecoilState(hourSelector);
  const onMinutesChange = (event: React.FormEvent<HTMLInputElement>) => {
    setMinutes(+event.currentTarget.value);
  };
  const onHoursChange = (event: React.FormEvent<HTMLInputElement>) => {
    setHours(+event.currentTarget.value);
  };

  return (
    <div>
      <input
        value={minutes}
        onChange={onMinutesChange}
        type="number"
        placeholder="Minutes"
      />
      <input
        value={hours}
        onChange={onHoursChange}
        type="number"
        placeholder="Hours"
      />
    </div>
  );
}

export default App;

 

์ถ”๊ฐ€๋กœ ์•Œ๊ฒŒ๋œ ๋ถ€๋ถ„

string์— ์—ฐ์‚ฐ์ž +๋ฅผ ๋ถ™์ด๋ฉด number๋กœ ํ˜• ๋ณ€ํ™˜์ด ๊ฐ€๋Šฅํ•˜๋‹ค. 

'๐ŸงProgramming' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Servlet  (0) 2024.01.19
Java๊ฐ€ ํŒŒ์ผ์„ ์ฝ๋„๋ก ํ•˜๋Š” ๋ฒ•  (0) 2024.01.14
Logging | SLF4J  (0) 2024.01.14
React | export์™€ export default์˜ ์ฐจ์ด  (0) 2023.08.12
OCP  (0) 2022.02.21

+ Recent posts