Featured post

c# - Usage of Server Side Controls in MVC Frame work -

i using asp.net 4.0 , mvc 2.0 web application. project requiremrnt have use server side control in application not possibl in noraml case. ideally want use adrotator control , datalist control. i saw few samples , references in codepleax mvc controllib howwver found less useful. can tell how utilize theese controls in asp.net application along mvc. note: please provide functionalities related adrotator , datalist controls not equivalent functionalities thanks in advace. mvc pages not use normal .net solution makes use of normal .net components impossible. a normal .net page use event driven solution call different methods service side mvc use actions , view completly different way handle things. also, mvc not use viewstate normal .net controlls require. found article discussing mixing of normal .net , mvc.

oop - Is it correct for an object to reject it self? -


i want parse several files formats. wondering if it's correct oop "take risk" create object nothing.

class parserfactory {     private fn;      public function parserfactory(fn)     {         this->fn = fn;     }     public function getparser()     {         = new formataparser(this->fn);         if ( a->isvalid() )         {             return( );         }         b = new formatbparser(this->fn);         // ... , on...     } }  class formataparser {     /*         object telling if able continue work...         **clean or dirty design ?**     */     public function isvalid()     {                     header = someconversionandreadingstuff();         if ( header != "formata" )         {             return(false)         }         return(true);     }     public function parse()     {         /*         parsing, using conversion stuff done in isvalid         */     } } 

thanks

edit
had answers. so, ok object check own validity. anyway, code un-oop because of procedural way select parser (format detection). improve this, better user factory pattern 1 (php code) :

class parser {     protected $raw;      public function setraw($raw)     {         $this->raw = $raw;     } }  class parsera extends parser {     public function __construct()     {         echo "parsera constructor\n";     }      public function isvalid()     {         if ( $this->raw == "a" )         {             return( true );         }         return(false);     } }  class parserb extends parser {     public function __construct()     {         echo "parserb constructor\n";     }     public function isvalid()     {         if ( $this->raw == "b" )         {             return( true );         }         return(false);     } }  class parserfactory {     static private $parserclasses = array();      public static function registerparser($parserclassname)     {         self::$parserclasses[] = $parserclassname;     }      public static function getparser($raw)     {         foreach( self::$parserclasses $parserclass )         {             $parser = new $parserclass();             $parser->setraw($raw);             if ( $parser->isvalid() )             {                 return( $parser );             }         }     } }  parserfactory::registerparser("parsera"); parserfactory::registerparser("parserb"); parserfactory::getparser("b"); 

a more common design

public function getparser() {     if (formataparser::isrecognizedheader(this->fn)     {         return new formataparser(this->fn);     }     if (formatbparser::isrecognizedheader(this->fn)     {         return new formatbparser(this->fn);     } } 

this uses class, not object method check header. obvious variation collect different parsers in list, instead of manually looping on them this. requires ability add classes (not objects) list.


Comments

Popular posts from this blog

c# - Usage of Server Side Controls in MVC Frame work -

cocoa - Nesting arrays into NSDictionary object (Objective-C) -

ios - Very simple iPhone App crashes on UILabel settext -