कोई भी विभिन्न टूल का उपयोग करके एक ही चीज़ को पूरा कर सकता है। तो क्या मैं नीचे उदाहरणों में हूं।PHP ओओपी: इंटरफ़ेस बनाम गैर-इंटरफ़ेस दृष्टिकोण - उदाहरण
एक इंटरफ़ेस/बहुरूपता का उपयोग दिखाता है (स्रोत: नेटटुट - मुझे लगता है)। एक और सीधा वर्ग इंटरैक्शन (मेरा) - जो कुछ बहुरूपता भी दिखाता है (call_tool() के माध्यम से)।
क्या आप मुझे बताएंगे, जो आप बेहतर तरीके से विचार करेंगे।
जो सुरक्षित, अधिक स्थिर, छेड़छाड़ प्रतिरोधी, भविष्य प्रमाण (afa कोड विकास संबंधित है) है।
कृपया दोनों में उपयोग किए गए दायरे/दृश्यता की जांच करें।
आपकी सामान्य सिफारिशें, जो सर्वोत्तम कोडिंग अभ्यास है।
इंटरफ़ेस:
class poly_base_Article { public $title; public $author; public $date; public $category; public function __construct($title, $author, $date, $category = 0, $type = 'json') { $this->title = $title; $this->author = $author; $this->date = $date; $this->category = $category; $this->type = $type; } public function call_tool() { $class = 'poly_writer_' . $this->type . 'Writer'; if (class_exists($class)) { return new $class; } else { throw new Exception("unsupported format: " . $this->type); } } public function write(poly_writer_Writer $writer) { return $writer->write($this); } } interface poly_writer_Writer { public function write(poly_base_Article $obj); } class poly_writer_xmlWriter implements poly_writer_Writer { public function write(poly_base_Article $obj) { $ret = ''; $ret .= '' . $obj->title . ''; $ret .= '' . $obj->author . ''; $ret .= '' . $obj->date . ''; $ret .= '' . $obj->category . ''; $ret .= ''; return $ret; } } class poly_writer_jsonWriter implements poly_writer_Writer { public function write(poly_base_Article $obj) { $array = array('article' => $obj); return json_encode($array); } } $article = new poly_base_Article('Polymorphism', 'Steve', time(), 0, $_GET['format']); echo $article->write($article->call_tool());
गैर इंटरफ़ेस
class npoly_base_Article { public $title; public $author; public $date; public $category; public function __construct($title, $author, $date, $category = 0, $type = 'json') { $this->title = $title; $this->author = $author; $this->date = $date; $this->category = $category; $this->type = $type; //encoding type - default:json } public function call_tool() { //call tool function if exist $class = 'npoly_writer_' . $this->type . 'Writer'; if (class_exists($class)) { $cls = new $class; return $cls->write($this); } else { throw new Exception("unsupported format: " . $this->type); } } } class npoly_writer_jsonWriter { public function write(npoly_base_Article $obj) { $array = array('article' => $obj); return json_encode($array); } } class npoly_writer_xmlWriter { public function write(poly_base_Article $obj) { $ret = ''; $ret .= '' . $obj->title . ''; $ret .= '' . $obj->author . ''; $ret .= '' . $obj->date . ''; $ret .= '' . $obj->category . ''; $ret .= ''; return $ret; } } $article = new npoly_base_Article('nPolymorphism', 'Steve', time(), 0, $_GET['format']); echo$article->call_tool();
MikeSW कोड
class poly_base_Article { private $title; private $author; private $date; private $category; public function __construct($title, $author, $date, $category = 0) { $this->title = $title; $this->author = $author; $this->date = $date; $this->category = $category; } public function setTitle($title) { return $this->title = $title; } public function getTitle() { return $this->title; } public function getAuthor() { return $this->author; } public function getDate() { return $this->date; } public function getCategory() { return $this->category; } } interface poly_writer_Writer { public function write(poly_base_Article $obj); } class poly_writer_xmlWriter implements poly_writer_Writer { public function write(poly_base_Article $obj) { $ret = ''; $ret .= '' . $obj->getTitle() . ''; $ret .= '' . $obj->getAuthor() . ''; $ret .= '' . $obj->getDate() . ''; $ret .= '' . $obj->getCategory() . ''; $ret .= ''; return $ret; } } class poly_writer_jsonWriter implements poly_writer_Writer { public function write(poly_base_Article $obj) { //array replacement //$obj_array = array('title' => $obj->getTitle(), 'author' => $obj->getAuthor(), 'date' => $obj->getDate(), 'category' => $obj->getCategory()); //$array = array('article' => $obj_array); $array = array('article' => $obj); //$obj arrives empty return json_encode($array); } } class WriterFactory { public static function GetWriter($type='json') { switch ($type) { case 'json': case 'xml': $class = 'poly_writer_' . $type . 'Writer'; return new $class; break; default: throw new Exception("unsupported format: " . $type); } } } $article = new poly_base_Article('nPolymorphism', 'Steve', time(), 0); $writer=WriterFactory::GetWriter($_GET['format']); echo $writer->write($article);
$ _GET - वह वास्तव में q का हिस्सा नहीं था। ;), इसलिए मैंने ज्यादा ध्यान नहीं दिया। लाइव कोड में यह प्रत्येक कोण से ठीक से देखा जाएगा। लेकिन आप सही हैं, मुझे इसे जाने नहीं देना चाहिए, भले ही वह क्यू से बाहर हो। गुंजाइश। – Jeffz
यदि आप poly_base_Article गुण निजी बनाते हैं, तो उदाहरण कैसे हो सकता है poly_writer_xmlWriter उन्हें एक्सेस? उनके बीच कोई संबंध नहीं है – Jeffz
WriterFactory - क्या यह सिर्फ अतिरिक्त ऑब्जेक्ट नहीं बना रहा है इसके बिना अतिरिक्त कार्यक्षमता जोड़ा गया है? – Jeffz