Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
March 19, 2009 Factory class | ||||
---|---|---|---|---|
| ||||
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? |
March 19, 2009 Re: Factory class | ||||
---|---|---|---|---|
| ||||
Posted in reply to KeepYourMind | KeepYourMind wrote:
>
> What i need to do for pizza.doSomeA() begin work?
cast pizza to CoolPizza.
|
March 19, 2009 Re: Factory class | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sean Kelly | 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.
|
March 19, 2009 Re: Factory class | ||||
---|---|---|---|---|
| ||||
Posted in reply to KeepYourMind | 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( );
}
}
|
March 19, 2009 Re: Factory class | ||||
---|---|---|---|---|
| ||||
Posted in reply to KeepYourMind | > 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
|
March 19, 2009 Re: Factory class | ||||
---|---|---|---|---|
| ||||
Posted in reply to Simen Kjaeraas | 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();
}
|
March 20, 2009 Re: Factory class | ||||
---|---|---|---|---|
| ||||
Posted in reply to Denis Koroskin | 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. :-(
|
March 20, 2009 Re: Factory class | ||||
---|---|---|---|---|
| ||||
Posted in reply to dennis luehring | 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.
|
Copyright © 1999-2021 by the D Language Foundation