Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
Hi I'm a unity developer working on a bar manger game and I'm just wondering on the best way to implement a drinks system where it sets the value of the drink, number of servings in the barrel, name of the drink price of the whole barrel.
Here is some code I was working on before:
This is the interface method
public interface IDrinkSystem
{
string SetDrinkName(string nameToSet);
float SetDrinkValue(float drinkValue);
int AmmountOfServingsInBarrel(int Servings);
float PriceOfBarrel(float price);
}
This is the class method of doing it
public class DrinkSystem
{
public void NewDrink(string drinkName, float drinkValue, int barrelServings, float barrelPrice)
{
// Have getters and setters for all values in separate methods
}
}
What is the best way for making it easy to expand and at a push can I make an array of the NewDrink to store all the drinks i have or is there abetter way of doing this.
Use ScriptableObjects, as if they were files.
For every type of drink, have a ScriptableObject which you can fill in from Unity easily (even your artist can do this), and then at runtime, you load them into objects of a single DrinkEntity class which loads these values.
This way you don't end up with dozens or hundreds of "DrinkSystemBeer"/"DrinkSystemWhiskey"/etc classes, but you can still keep all your code clean and pattern-friendly.
IMPORTANT: NEVER operate with the ScriptableObjects directly. Simply load them into a DrinkEntity on its constructor. Treat ScriptableObject as if they were xml or json files from which you read your data.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 days ago.
This post was edited and submitted for review 8 days ago.
Improve this question
My question is related to inheritance it.
Let say we have a class called Animal. All animals share some behaviour like walking, pooping, hunting, etc. After few days I decided to add a Fish class that extend Animal class. But here fish swims but does not hunt or walk.
Is it OK to use only some parts of the base class? Because Fish class can still calls walk and hunt but does not execute the behaviour, I guess.
I could go around the problem where i make a function called behaviour in base class and let the child class override it.
Like this:
public class Animal
{
public virtual void behaviour() {
Console.WriteLine("//do something");
}
}
public class GoldFish : Animal
{
public override void behaviour() {
Console.WriteLine("goldfish swims");
}
}
Any suggestion on how should go about this with best practices of programming?
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I am trying to figure out the best way to change an existing class.
So the class is called ExcelReport and it has one method Create(data,headings). This is live and used in many places. Now recently I want to change the method so I can format columns in Excel.
Create(data, headings, columnformats)
So as not to upset my existing programs the best I can come up with is to add another method Create2(data,headings,columnformats) to the class.
I got a lot of suggestions saying I should modify the existing class with a overloaded method, which I did. But does this not break the Open/Close Principle as my existing class was in production?
Should I have created a new class ExcelReport2(and Interface) with the new improved method and passed this into my new program using dependency injection?
OCP
In object-oriented programming, the open–closed principle states "software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification";[1] that is, such an entity can allow its behaviour to be extended without modifying its source code.
Your solution
You will most likely want to create more options later on for this.
And since you asked for an open/closed principle answer we need to take that into account (open for extension, closed for change).
A more robust alternative is to create a new overload:
void Create(CreationOptions options);
Looks trivial, right? The thing is that any subclass can introduce their own options like MyPinkThemedFormattedCellsCreationOptions.
So your new option class would look like this as of now:
public class CreationOptions
{
public SomeType Data { get; set; }
public SomeType Headings { get; set; }
public SomeType[] ColumnFormats { get; set; }
}
That's open for extension and closed for change as new features doesn't touch the existing API, since now you only have to create sub classes based on CreationOptions for new features.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
Say I have a base method GetSong() that fetches songs from a database, and two methods that call this - GetRockSong() and GetPopSong().
Both methods take a string input of the song name, then pass that input to the GetSong() method, along with a genre.
Should the base method be in charge of validating the string input?
I would think the first two methods should, but this would lead to repeating the exact same code (i.e. checking the string isn't empty).
Obviously I have used a hugely simplistic illustration, but the problem is pretty much the same.
Example code:
public Song GetRockSong(string title)
{
// could null check title here before calling the method?
return GetSong(title, "Rock");
}
public Song GetPopSong(string title)
{
// could null check title here before calling the method?
return GetSong(title, "Pop");
}
public Song GetSong(string title, string genre)
{
// example validation, if null checking title above
// then could just check genre here
if (!string.IsNullOrEmpty(title) && !string.IsNullOrEmpty(genre))
{
// fetch song logic here
}
// etc
}
I personally think if the validation is the same then do the check in the shared method.
This will allow for you to maintain the code easier and it is always best to have the method that does the heavy lifting also validate the values passed to it.
I have done similar thing and found that the amount of code I needed to write and maintain is half of what it could have been if I had put the validation in each method that called it.
I hope this helps!
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have huge class that implements usage of some client:
public class Client : IClient
{
internal Client(string username, string password){
//login process here
}
//some private methods that make sure connection stays alive, etc
public void Action1(string param1){
//something here...
}
public void Action2(string param1, string param2){
//something else here...
}
}
As it currently is, it's 5000+ lines long mainly because of lots of different public methods.
I'm wondering what is the best practice to properly organize and refactor this, preferably without making method calls more complicated?
Use partial classes and group things into logical sets per each partial class.
Also, if some methods make logical set, consider wrapping them into separate class.
Those 2 should reduce your lines of code per file dramatically.
Usually big class are "hiding" inside other classes (see uncle Bob on "Clean Code").
In your case I'd split the class creating Action classes and making some machanics that lets the Client use some sort of IAction or BaseAction. Thus splitting the logic of every action into a separate class.
To be more precise I'd rather need some more info and code.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
i've been figuring a little about abilities... like in DOTA, LoL and other MOBA / MMORPG games. If i would make a game with character selection in the beginning, and in-game it would have spells / abilities and such..
What would a correct way of doing this be? Should i make one Champion class, with all values and textures, or initialize it in Game1.cs like Champion champ = new Champion(texture, name, Q-ability name, Q-cooldown, Q-damage, Q-manacost) - but that would take TONS of diffrent declarations...
Should i make a Champion folder and make a seperate class of them all? And also the abilities - i'm thinking about maaking an enum to keep them, or would a List or another variable work better? Thanks!
For such a game, I'd recommend the use of an entity system (http://entity-systems.wikidot.com/). These allow you to create separate spell, ability, player, character etc classes and to compose them into highly configurable entities. I ported the popular Ash entity framework to C#, which is available at https://github.com/DavidArno/Ash.NET, which may be of use to you.
Beside of an entity system which David Arno suggested, if I wanted to do this, I'd go with a single Hero class and then with a list of abilities inside it. Abilities are all the same, different thing is their factors. For instance, how much damage an ability can deliver and etc. This way you have to instantiate all of your champions and their abilities.