什么模式可用于创建一个 class 的对象,但以不同的方式填充其属性?

What pattern can be used to create an object of one class, but fill its properties in different ways?

我有一个class这样的

public class OwnerWithholding
    {

        private decimal ManagementFeePct;

        private decimal TotalManagementFee;

        private decimal OperationalFeesPct;

        private decimal TotalOperationalFees;

}

而且我有创建这个 class 对象的计算方法,用一些算术运算填充它,return 这个对象。

public OwnerWithholding CalculationMethod1(Reservation r, SqlConnection conn)
        {
            OwnerWithholding result = new OwnerWithholding();

           // result.ManagementFeePct  = some data from Fees table in DB + value 
           //from another db - constant value..

           // Also with other properties - with some operations on data

           //result.TotalManagementFee = ..
           // result.OperationalFeesPct = ..
           // result. TotalOperationalFees = ..

            return result;
        }

现在可以正常使用了。 但这种计算方法只是填充数据的一种选择。

还有另一种计算方法,以完全不同的方式实现,但填充了完全相同的对象属性。我可能会有更多。

我需要一个允许我创建相同对象的模式 class,只是指明需要的计算方法。 我喜欢策略模式,其中算法将是填充调用它们的对象的方法。 但它看起来不太好。 也许工厂方法在这里更合适,但我不知道如何实现。

编辑:现在按照 OP 的评论,看起来 class 中的一个方法需要以多种方式设置。 模板模式(或构建器)更适合这种情况,而不是工厂模式。

模板模式。

一个。设置默认属性的抽象基础 class,但遗漏了一个 属性(获取成分)以由具体 classes 填充。

    public abstract class PizzaCreator
    {
        public abstract string GetIngredients { get; }
        public string Bake { get; set; } = "Bake at 400F for 30 minutes";
        public string Deliver { get; set; } = "Deliver in custom box";
    }

b。两个 Pizza classes,现在只是覆盖抽象 属性

    public class CheesePizza : PizzaCreator
    {
        public override string GetIngredients
        {
            get { return GetMyIngredients(); }
        }

        string GetMyIngredients()
        {
            return "Lots of Cheese!";
        }
    }

    public class PepperoniPizza : PizzaCreator
    {
        public override string GetIngredients
        {
            get { return GetMyIngredients(); }
        }

        string GetMyIngredients()
        {
            return "Lots of Meats!";
        }
    }

我在这里创建比萨饼的实例

            var pepPizza = new PepperoniPizza();
            var chessePizza = new CheesePizza();

您甚至可以让这些作品通过工厂 class/method。

原回答: 这是抽象工厂模式。

此代码进入 Factory class 库。

a.ICar界面

    public interface ICar
    {
        string Name { get; set; }
        int Doors { get; set; }
        string EngineCapacity { get; set; }
    }

b.Abstract汽车厂

    public abstract class AbstractCarFactory
    {
        public abstract ICar CreateCar(CarType type);
    }

c.Two 混凝土车 -

    internal class NissanPickUpTruck : ICar
    {
        public string Name { get; set; } 
        public int Doors { get; set ; }
        public string EngineCapacity { get ; set ; }
    }

    internal class NissanSportsCar: ICar
    {
        public string Name { get; set; } 
        public int Doors { get; set; }
        public string EngineCapacity { get; set; }
    }

d.Concrete工厂

    public class NissanFactory : AbstractCarFactory
    {
        public override ICar CreateCar(CarType type)
        {
            switch (type)
            {
                case CarType.PickupTruck:
                    return new NissanPickUpTruck{Name = "Titan", Doors = 6, EngineCapacity = "V12"};
                case CarType.SportsCar:
                    return new NissanSportsCar{Name = "350Z", Doors = 2, EngineCapacity = "V6"};
                default:
                    throw new Exception();
            }
        }
    }

最后是来自外部项目的调用

            var nissanFactory = new NissanFactory();
            var sportsCar = nissanFactory.CreateCar(CarType.SportsCar);
            var pickUpTruck = nissanFactory.CreateCar(CarType.PickupTruck);

但与其他评论一样,Builder 也值得一试。