Factory pattern question!

C

CSharper

I have created a Factory pattern code. One thing I noticed after
coding, each factory has some methods they are exactly same and doing
the same code using the values specific to each factory, if I would
refactor the code and make it a seperate class and use the class in
each factory then the code look look and more DRY. My question is what
is the right way to do this in Factory Pattern. Refactoring them into
a seperate class is a solution? here is a sample code

public abstract class Fact
{
public void doit(int parm);
}

public sealed firstFact: Fact
{
int factValue = 10;
public override void doit(int parm)
{
dosomethingelse();
}

void dosomethingelse()
{
int t = factValue + 10;
}
}

public sealed secondFact: Fact
{
int factValue = 20;
public override void doit(int parm)
{
dosomethingelse();
}

void dosomethingelse()
{
int t = factValue + 10;
}
}


If you see the example above, the value of factValue changes based on
the factory you use. I over simplified the class for demo purpose. I
have bunch of more of facotroy specific values I need use.

Thanks
 
B

Brian Gideon

I have created a Factory pattern code. One thing I noticed after
coding, each factory has some methods they are exactly same and doing
the same code using the values specific to each factory, if I would
refactor the code and make it a seperate class and use the class in
each factory then the code look look and more DRY. My question is what
is the right way to do this in Factory Pattern. Refactoring them into
a seperate class is a solution? here is a sample code

public abstract class Fact
{
    public void doit(int parm);

}

public sealed firstFact: Fact
{
    int factValue = 10;
    public override void doit(int parm)
    {
           dosomethingelse();
    }

    void dosomethingelse()
    {
          int t = factValue + 10;
    }

}

public sealed secondFact: Fact
{
    int factValue = 20;
    public override void doit(int parm)
    {
           dosomethingelse();
    }

    void dosomethingelse()
    {
          int t = factValue + 10;
    }

}

If you see the example above, the value of factValue changes based on
the factory you use. I over simplified the class for demo purpose. I
have bunch of more of facotroy specific values I need use.

Thanks

public abstract class Fact
{
public override void doit(int parm)
{
dosomethingelse();
}

void dosomethingelse()
{
int t = factValue + 10;
}

protected abstract int FactValue { get; }
}

public sealed class firstFact : Fact
{
protected override int FactValue { get { return 10; } }
}

public sealed class secondFact : Fact
{
protected override int FactValue { get { return 20; } }
}
 
S

sloan

Huh?

You can check this small blog entry about the Simple Factory Pattern
(downloadable code as well).

http://sholliday.spaces.live.com/blog/cns!A68482B9628A842A!126.entry


The factory can return either a concrete implementation of an
Interface......
or a subclassed version of an Abstract Class.

My code leans toward the Interface side of things.


I would make sure I understand Interface versus Abstract class before
delving into alot of questions about the pattern.

Then throw some questions out there.

...........
 
B

Ben Voigt [C++ MVP]

public abstract class Fact
{
public override void doit(int parm)
{
dosomethingelse();
}

void dosomethingelse()
{
int t = factValue + 10;
}

protected abstract int FactValue { get; }

A little bit of case-sensitivity training needed ;) but otherwise good.
 
B

Ben Voigt [C++ MVP]

Brian said:
Let's pretend like that never happened :)

How 'bout listing case-sensitivity training, administered by Csc Exe, Inc.,
alongside race-sensitivity, gender-sensitivity, and all the other non-tech
stuff employers want to know if you've done?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top