开发者

C# Class Factories

开发者 https://www.devze.com 2023-02-16 16:07 出处:网络
I have a class called Foo that has a function that looks like the following List<Bar> LoadData();

I have a class called Foo that has a function that looks like the following

List<Bar> LoadData();

Both Foo and 开发者_StackOverflowBar are in a library that I want to reuse in other projects. Now I am working on a new project and I want to subclass Bar. Let's call it NewBar.

What is a simple and flexible way to get Foo.LoadData to return a list of NewBar? I think that a factory is needed or perhaps just a delegate function. Can anyone provide an example?

Thanks, Andy


Maybe even simplier, depends on what you need, but generic constraints may suffice if Bar is constructed with parameterless constructor:

public List<TBar> LoadData<TBar>()
    where TBar : Bar, new()
{
    // return a list populated with `new TBar()`;
}

Usage:

var newBars = Foo.LoadData<NewBar>();


The easiest way would be to have an interface implemented by Bar and NewBar, and have your function return List<IBar> instead.

It's that or return object and cast galore.


There are a few problems here. Since you are using List<Bar> this would not work if you return List<Baz> since List<> does not do covariance.

My suggestion is to redesign it to return IEnumerable<Bar> and make it virtual so that in the new project create a SubFoo and you can return IEnumerable<Baz>.


UPDATE

OK, according to the new information you provided (you use it for populating list from XML), I would create a virtual protected CreateBar() which creates a new bar object and is called by LoadData() to create new Bar in the loop. In the SubFoo I override and return Baz instead. In the SubFoo I will call base.LoadData() and populate the Baz list and then add the logic to populate new properties of Baz which are not in Bar.

Or perhaps

just use XML Serilization and I get all of that for free!


Make LoadData() virtual. Then override it in a derived factory.

If the derived factory method would still return a list of Bar, but you could specify in the method implementation that all of the Bar instances are actually NewBar. This could be altered by using a generic method like in Snowbear's example.


It's time you learn about covariance and contravariance. It's not a small thing to learn. Basically I'd suggest creating an IBar interfarce, it'll make your life much easier.

Read about the concepts msdn, but you should keep looking for other examples too.


Having read the answers (thanks!) I came up with the following. Why is this not a good idea?

public interface IBarFactory
{
    Bar Create();
}

Then change LoadData to:

List<Bar> LoadData(IBarFactory Factory)
{
    List<Bar> MyList = new List<Bar>();

    if (Factory == null)
    {
        MyList.Add(new Bar());
    }
    else
    {
        MyList.Add(Factory.Create());
    }

    // etc, etc
}

Then I just need to create the following:

public class BazFactory : IBarFactory
{
    Bar Create() { return new Baz() };
}

Finally call it:

MyList = LoadData(BazFactory);
0

精彩评论

暂无评论...
验证码 换一张
取 消