C# Implementing Abstract factory pattern -


ive abstract factory pattern in 1 of projects: http://www.dofactory.com/patterns/patternabstract.aspx

code:

public class questaoresposta : questaobaseresposta, iquestao,iquestionario {     public int idquestaoresposta { get; set; } }  public class questaofactory : questoesfactory {     public override questaobaseresposta createquestao()     {         return new questaoresposta();     } }  public abstract class questoesfactory {     public abstract questaobaseresposta createquestao(); }  public class questaobaseresposta : iquestao, imarcas, iquestionario {     // constructor want create concrete instance      // of class inherits questaobaseresposta using questoesfactory      // abstract class, , assign current instance of     // questaobaseresposta class     public questaobaseresposta(questoesfactory qf)     {         = qf.createquestao();     } } 

problem cant assing value current class using "this" keyword.

example:

questaobaseresposta qs = new questaobaseresposta(new questaofactory());  // here want qs intance type of questaoresposta // since im passing questaofactory argument,without cast anything. qs.idquestaoresposta = 0; 

what suggest cast questaobaseresposta class inherit type (questaoresposta),without cast?

no can't cast without cast. usefulness of factory comes fact xyou not need know exact derived class of returned object. different if donÄt use factory instead calling constructor know exact type.

why don't way:

questaobaseresposta qs = new questaofactory().createquestao(); 

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -