Skip to main content
Code Review

Return to Answer

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

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.

added 636 characters in body
Source Link
Denis
  • 8.6k
  • 5
  • 33
  • 76
  1. 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.g NonElementalItem).
  2. 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.

  1. 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.g NonElementalItem).
  2. 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.

  1. 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.

added 94 characters in body
Source Link
Denis
  • 8.6k
  • 5
  • 33
  • 76
  1. Simplifies the creation of the item classes.
  2. There is no duplicate code.
  3. Item properties can be set in any order.
  4. You can assign better names to each class' method.
  5. You can have some methods in let's say class Weapon but not in Armor if you want to (you can do that with the builder pattern but it will require more work).
  6. You don't need a constructor at all.
  1. There is no duplicate code.
  2. You can assign better names to each class' method.
  3. You can have some methods in let's say class Weapon but not in Armor if you want to (you can do that with the builder pattern but it will require more work).
  4. You don't need a constructor at all.
  1. Simplifies the creation of the item classes.
  2. There is no duplicate code.
  3. Item properties can be set in any order.
  4. You can assign better names to each class' method.
  5. You can have some methods in let's say class Weapon but not in Armor if you want to (you can do that with the builder pattern but it will require more work).
  6. You don't need a constructor at all.
added 46 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
Loading
added 9257 characters in body
Source Link
Denis
  • 8.6k
  • 5
  • 33
  • 76
Loading
added 40 characters in body
Source Link
Denis
  • 8.6k
  • 5
  • 33
  • 76
Loading
Source Link
Denis
  • 8.6k
  • 5
  • 33
  • 76
Loading
lang-cs

AltStyle によって変換されたページ (->オリジナル) /