www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Factory class

reply KeepYourMind <vyacheslav blue-code.org> writes:
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
next sibling parent reply Sean Kelly <sean invisibleduck.org> writes:
KeepYourMind wrote:
 
 What i need to do for pizza.doSomeA() begin work?
cast pizza to CoolPizza.
Mar 19 2009
parent reply KeepYourMind <vyacheslav blue-code.org> writes:
Sean Kelly Wrote:

 KeepYourMind wrote:
 
 What i need to do for pizza.doSomeA() begin work?
cast pizza to CoolPizza.
Problem is i dont know what class returned. "char[] name" is dynamic and getting from command-line arguments.
Mar 19 2009
parent reply "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On Thu, 19 Mar 2009 18:06:37 +0100, KeepYourMind <vyacheslav blue-code.org>
wrote:

 Sean Kelly Wrote:

 KeepYourMind wrote:
 What i need to do for pizza.doSomeA() begin work?
cast pizza to CoolPizza.
Problem is i dont know what class returned. "char[] name" is dynamic and getting from command-line arguments.
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( ); } }
Mar 19 2009
parent reply "Denis Koroskin" <2korden gmail.com> writes:
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:

 Sean Kelly Wrote:

 KeepYourMind wrote:
 What i need to do for pizza.doSomeA() begin work?
cast pizza to CoolPizza.
Problem is i dont know what class returned. "char[] name" is dynamic and getting from command-line arguments.
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( ); } }
Not like that. Here is a better way: auto pizza = PizzaFactory.factory( name ); pizza.doSome(); if (auto cool = cast(CoolPizza)pizza) { cool.doSomeA(); }
Mar 19 2009
parent KeepYourMind <vyacheslav blue-code.org> writes:
Denis Koroskin Wrote:

 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:

 Sean Kelly Wrote:

 KeepYourMind wrote:
 What i need to do for pizza.doSomeA() begin work?
cast pizza to CoolPizza.
Problem is i dont know what class returned. "char[] name" is dynamic and getting from command-line arguments.
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( ); } }
Not like that. Here is a better way: auto pizza = PizzaFactory.factory( name ); pizza.doSome(); if (auto cool = cast(CoolPizza)pizza) { cool.doSomeA(); }
Got it. If use one from this ways i'm not need factory, only switch. Bad. :-(
Mar 20 2009
prev sibling parent reply dennis luehring <dl.soluz gmx.net> writes:
 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
parent KeepYourMind <vyacheslav blue-code.org> writes:
dennis luehring Wrote:

 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
Got it. Thanks to all.
Mar 20 2009