digitalmars.D.learn - Factory class
- KeepYourMind (44/44) Mar 19 2009 Hello. I'm try to write factory class but have some problems.
- Sean Kelly (2/4) Mar 19 2009 cast pizza to CoolPizza.
- KeepYourMind (2/7) Mar 19 2009 Problem is i dont know what class returned. "char[] name" is dynamic and...
- Simen Kjaeraas (9/17) Mar 19 2009 Then you need some test to see if it really is a CoolPizza.
- Denis Koroskin (7/27) Mar 19 2009 Not like that. Here is a better way:
- KeepYourMind (2/36) Mar 20 2009 Got it. If use one from this ways i'm not need factory, only switch. Bad...
- dennis luehring (3/9) Mar 19 2009 you interface has only doSome() and that is what factory returns
- KeepYourMind (2/14) Mar 20 2009 Got it. Thanks to all.
Hello. I'm try to write factory class but have some problems. Source example: // Pizza interface interface IPizza { void doSome(); } // Pizza class IMPizza : IPizza { void doSome() {} } // try to raise functinally class CoolPizza : IPizza { void doSome() {} void doSomeA() {} } // Factory static class PizzaFactory { static IPizza factory(char[] name) { switch (name) { case "im": return new IMPizza; break; case "cool": return new CoolPizza; break; default: throw new Exception("incorrect pizza name"); break; } } } // run void main() { auto pizza = PizzaFactory.factory(); pizza.doSome(); // OK pizza.doSomeA(); // error: IPizza.doSomeA() not found } What i need to do for pizza.doSomeA() begin work?
Mar 19 2009
KeepYourMind wrote:What i need to do for pizza.doSomeA() begin work?cast pizza to CoolPizza.
Mar 19 2009
Sean Kelly Wrote:KeepYourMind wrote:Problem is i dont know what class returned. "char[] name" is dynamic and getting from command-line arguments.What i need to do for pizza.doSomeA() begin work?cast pizza to CoolPizza.
Mar 19 2009
On Thu, 19 Mar 2009 18:06:37 +0100, KeepYourMind <vyacheslav blue-code.org> wrote:Sean Kelly Wrote:Then you need some test to see if it really is a CoolPizza. void main( string[] args ) { auto pizza = PizzaFactory.factory( args[1] ); pizza.doSome( ); if ( args[1] == "CoolPizza" ) { ( cast( CoolPizza ) pizza ).doSomeA( ); } }KeepYourMind wrote:Problem is i dont know what class returned. "char[] name" is dynamic and getting from command-line arguments.What i need to do for pizza.doSomeA() begin work?cast pizza to CoolPizza.
Mar 19 2009
On Thu, 19 Mar 2009 22:58:02 +0300, Simen Kjaeraas <simen.kjaras gmail.com> wrote:On Thu, 19 Mar 2009 18:06:37 +0100, KeepYourMind <vyacheslav blue-code.org> wrote:Not like that. Here is a better way: auto pizza = PizzaFactory.factory( name ); pizza.doSome(); if (auto cool = cast(CoolPizza)pizza) { cool.doSomeA(); }Sean Kelly Wrote:Then you need some test to see if it really is a CoolPizza. void main( string[] args ) { auto pizza = PizzaFactory.factory( args[1] ); pizza.doSome( ); if ( args[1] == "CoolPizza" ) { ( cast( CoolPizza ) pizza ).doSomeA( ); } }KeepYourMind wrote:Problem is i dont know what class returned. "char[] name" is dynamic and getting from command-line arguments.What i need to do for pizza.doSomeA() begin work?cast pizza to CoolPizza.
Mar 19 2009
Denis Koroskin Wrote:On Thu, 19 Mar 2009 22:58:02 +0300, Simen Kjaeraas <simen.kjaras gmail.com> wrote:Got it. If use one from this ways i'm not need factory, only switch. Bad. :-(On Thu, 19 Mar 2009 18:06:37 +0100, KeepYourMind <vyacheslav blue-code.org> wrote:Not like that. Here is a better way: auto pizza = PizzaFactory.factory( name ); pizza.doSome(); if (auto cool = cast(CoolPizza)pizza) { cool.doSomeA(); }Sean Kelly Wrote:Then you need some test to see if it really is a CoolPizza. void main( string[] args ) { auto pizza = PizzaFactory.factory( args[1] ); pizza.doSome( ); if ( args[1] == "CoolPizza" ) { ( cast( CoolPizza ) pizza ).doSomeA( ); } }KeepYourMind wrote:Problem is i dont know what class returned. "char[] name" is dynamic and getting from command-line arguments.What i need to do for pizza.doSomeA() begin work?cast pizza to CoolPizza.
Mar 20 2009
void main() { auto pizza = PizzaFactory.factory(); pizza.doSome(); // OK pizza.doSomeA(); // error: IPizza.doSomeA() not found }you interface has only doSome() and that is what factory returns and for all others: factories and cast don't belong together you interface should fit your overall needs
Mar 19 2009
dennis luehring Wrote:Got it. Thanks to all.void main() { auto pizza = PizzaFactory.factory(); pizza.doSome(); // OK pizza.doSomeA(); // error: IPizza.doSomeA() not found }you interface has only doSome() and that is what factory returns and for all others: factories and cast don't belong together you interface should fit your overall needs
Mar 20 2009