Your Armor
& Weapon
class have too much parameters making the initialization ugly also they seem like a immutable objects, you might want to apply the builder design pattern, you can check my answer on t3chb0t's question question here here, which if you configure properly can make your intialization a lot prettier + your objects will be immutable.
Your Armor
& Weapon
class have too much parameters making the initialization ugly also they seem like a immutable objects, you might want to apply the builder design pattern, you can check my answer on t3chb0t's question here, which if you configure properly can make your intialization a lot prettier + your objects will be immutable.
Your Armor
& Weapon
class have too much parameters making the initialization ugly also they seem like a immutable objects, you might want to apply the builder design pattern, you can check my answer on t3chb0t's question here, which if you configure properly can make your intialization a lot prettier + your objects will be immutable.
- You will need a separate builder for each new type of item. (If you create more items of type
ElementalsItem
you will be able to reuse the old builder but if you create new type you will need a new builder e.gNonElementalItem
). - It will be hard to create good relation or hierarchy between the builders in order to avoid duplicate code (you will see what I mean in a second).
If there's a wayYou can do something along the lines in the BaseItemBuilder
protected string _name;
protected int _cost;
public TBuilder WithNameAndCost<TBuilder>(string name, int cost)
where TBuilder : BaseItemBuilder
{
_name = name;
_cost = cost;
return (TBuilder)this;
}
And later inherit the BaseItemBuilder
from ElementalItemBuilder
which will allow you to fix thatremove the designduplicate code but you will perfectly fit your project, maybe someone inhave to specify the comments can point something usefultype argument upon invocation like this :
BaseItemBuilder itemBuilder = new BaseItemBuilder();
Item itemStone = itemBuilder
.WithNameAndCost<BaseItemBuilder>("Stone", 1)
.Build();
itemBuilder = new ElementalItemBuilder();
Armor helmet = itemBuilder
.WithNameAndCost<ElementalItemBuilder>("Steel Helmet", 80)
.WithPoints(30)
.WithEarth(1)
.WithFire(2)
.Build<Armor>();
Which pretty much solves the problem with the duplicate code.
- You will need a separate builder for each new type of item. (If you create more items of type
ElementalsItem
you will be able to reuse the old builder but if you create new type you will need a new builder e.gNonElementalItem
). - It will be hard to create good relation or hierarchy between the builders in order to avoid duplicate code (you will see what I mean in a second).
If there's a way to fix that the design will perfectly fit your project, maybe someone in the comments can point something useful.
- It will be hard to create good relation or hierarchy between the builders in order to avoid duplicate code (you will see what I mean in a second).
You can do something along the lines in the BaseItemBuilder
protected string _name;
protected int _cost;
public TBuilder WithNameAndCost<TBuilder>(string name, int cost)
where TBuilder : BaseItemBuilder
{
_name = name;
_cost = cost;
return (TBuilder)this;
}
And later inherit the BaseItemBuilder
from ElementalItemBuilder
which will allow you to remove the duplicate code but you will have to specify the type argument upon invocation like this :
BaseItemBuilder itemBuilder = new BaseItemBuilder();
Item itemStone = itemBuilder
.WithNameAndCost<BaseItemBuilder>("Stone", 1)
.Build();
itemBuilder = new ElementalItemBuilder();
Armor helmet = itemBuilder
.WithNameAndCost<ElementalItemBuilder>("Steel Helmet", 80)
.WithPoints(30)
.WithEarth(1)
.WithFire(2)
.Build<Armor>();
Which pretty much solves the problem with the duplicate code.
- Simplifies the creation of the item classes.
- There is no duplicate code.
- Item properties can be set in any order.
- You can assign better names to each class' method.
- You can have some methods in let's say class
Weapon
but not inArmor
if you want to (you can do that with the builder pattern but it will require more work). - You don't need a constructor at all.
- There is no duplicate code.
- You can assign better names to each class' method.
- You can have some methods in let's say class
Weapon
but not inArmor
if you want to (you can do that with the builder pattern but it will require more work). - You don't need a constructor at all.
- Simplifies the creation of the item classes.
- There is no duplicate code.
- Item properties can be set in any order.
- You can assign better names to each class' method.
- You can have some methods in let's say class
Weapon
but not inArmor
if you want to (you can do that with the builder pattern but it will require more work). - You don't need a constructor at all.