E D R , A S I H C RSS

Query Method

Query Method

๊ฐ์ฒด์˜ ์†์„ฑ์„ ํ…Œ์ŠคํŠธํ•˜๋ ค๋ฉด ์–ด๋–ป๊ฒŒ ํ• ๊ฒƒ์ธ๊ฐ€? ๋‘ ๊ฐ€์ง€๋ฅผ ๊ฒฐ์ •ํ•ด์•ผ ํ•œ๋‹ค. ์ฒซ์งธ๋Š” ๋ฌด์—‡์„ ๋ฆฌํ„ดํ•  ๊ฒƒ์ธ๊ฐ€์ด๊ณ , ๋‘˜์งธ๋Š” ์ด๋ฆ„์„ ์–ด๋–ป๊ฒŒ ์ง“๋А๋ƒ๋‹ค.

~cpp 
class Switch
{
private:
	string status;
	/* ... */
public:
	void makeOn() {
		status = "on";
	}
	void makeOff() {
		status = "off";
	}
	string& getStatus() {
		return status;
	}
	/* ... */
};

class Light
{
/* ... */
	void makeOn() {
		/* ... */
	}
};

class WallPlate
{
private:
	Switch* switch;
	Light* light;
/* ... */
public:
/* ... */
	void update() {
		if( switch->getStatus() == "on" )
			light->makeOn();
		else if( switch->getStatus() == "off" )
			light->makeOff();
	}
};
์ด๊ฒƒ์€ ๋‘ ๊ฐ์ฒด ์ค‘์— ํ•˜๋‚˜๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ๋”ฑ ๋ณด๊ธฐ์—๋„ ๋ญ”๊ฐ€ ์ฐ์ฐํ•ด ๋ณด์ธ๋‹ค. ํ•˜์ง€๋งŒ ์ด ๋ฐฉ๋ฒ•์€ ํด๋ผ์ด์–ธํŠธ๋กœ ํ•˜์—ฌ๊ธˆ Switch๊ฐ€ ์ƒํƒœ๋ฅผ ์–ด๋–ป๊ฒŒ ์ €์žฅํ•˜๊ณ  ์žˆ๋Š”์ง€ ์•Œ์•„์•ผ๋งŒ ํ•˜๊ฒŒ ํ•ด์ค€๋‹ค.

์ด๋ฅผ ํ•ด๊ฒฐํ•ด๊ธฐ ์œ„ํ•ด, ํ•˜๋‚˜์˜ ๋ฉ”์„ธ์ง€ - Boolean์„ ๋ฆฌํ„ดํ•˜๋Š” - ์—๋‹ค๊ฐ€ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.
~cpp 
class Switch
{
private:
	bool status;
	/* ... */
public:
	bool isOn() {
		return status;
	}
	/* ... */
};

class Light
{
/* ... */
	void makeOn() {
		/* ... */
	}
};

class WallPlate
{
private:
	Switch* switch;
	Light* light;
/* ... */
public:
/* ... */
	void update() {
		if ( switch->isOn() )
			light->makeOn();
		else
			light->makeOff();
	}
};

Boolean์„ ๋ฆฌํ„ดํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ๋งŒ๋“ค๊ณ , ์ด๋ฆ„์€ ์ ‘๋‘์‚ฌ์— be๋™์‚ฌ์˜ ์—ฌ๋Ÿฌ ํ˜•ํƒœ๋ฅผ ์ ์–ด์ค€๋‹ค.(is,was...)


Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:24:09
Processing time 0.0074 sec