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...)
See Also ReplaceTempWithQuery