First of all: Sorry for my English, that said...
I am developing an obligatory for my University, so far it has a class that provides the user with options to connect to a server (once connected it should offer other functionalities, but I only reached the connection option so far), it only shows console options, and checks they are correctly chosen, nothing else.
The class has everything (including itself) static, the reason is simple: I didn't considered necessary to instantiate that class, not even once (what is allowed by using Singleton Pattern), and so I decided going everything static inside it.
I showed my code to my teacher, who said It was an awful design more or less :( well, he suggested I used Singleton and I explained that I understood Singleton to be only useful when you wanted (needed) only one instance for the class, I don't need even one!
He said static variables are global, which could cause conflict with other variable names and then I would have to remember the static variable names I used so as to avoid conflict, so I highlighted that the class has everything private (except a method called ShowStartMenu() which is internal, and is the one called in the Main) and then he almost exploded...
He told me it wasn't a good design because it didn't allow extensibility (what if I wanted to add more options to the menu, I had to add them in that class... which is exactly what I intended, is that wrong?), and had other disadvantages I could ask my design teacher ¬¬
So, I want to show you my code and ask you what do you think about it.
using System;
namespace ARIP.Client.Console
{
internal static class ClientOptions
{
private const int START_MENU_CONNECT_TO_SERVER_OPTION = 1;
private const int START_MENU_END_APPLICATION_OPTION = 0;
private static int _minOption;
private static int _maxOption;
private static int _givenOption;
#region General Client-Option's Functions
private static void askForUserOption()
{
System.Console.WriteLine("");
System.Console.Write("Su opción: ");
}
private static void readUserOption(Action methodCaller)
{
try
{
string givenOptionAsString = System.Console.ReadLine();
_givenOption = int.Parse(givenOptionAsString);
}
catch (FormatException)
{
System.Console.WriteLine("");
System.Console.WriteLine("Atención! Sólo se permite ingresar dígitos. Intente nuevamente.");
methodCaller();
}
}
private static void checkUserOptionIsBetweenMinAndMaxOptions(Action methodCaller)
{
if (!givenOptionBetweenMinAndMaxOptions())
{
showInvalidOptionMessage();
methodCaller();
}
}
private static bool givenOptionBetweenMinAndMaxOptions()
{
return _givenOption >= _minOption && _givenOption <= _maxOption;
}
private static void showInvalidOptionMessage()
{
System.Console.WriteLine("");
System.Console.WriteLine("Atención! La opción ingresada no es válida, debe ingresar un número entero entre " + _minOption + " y " + _maxOption + ".");
}
#endregion
#region Start Menu
internal static void ShowStartMenu()
{
System.Console.WriteLine("Menú Principal");
System.Console.WriteLine("");
System.Console.WriteLine("Ingrese la opción deseada:");
System.Console.WriteLine(START_MENU_CONNECT_TO_SERVER_OPTION + "-Conectarse a un Servidor");
System.Console.WriteLine("");
System.Console.WriteLine(START_MENU_END_APPLICATION_OPTION + "-Terminar Aplicación");
setMinAndMaxOptionsForStartMenu();
askUserOptionForStartMenu();
startMenuResponsalToOptionGiven();
}
private static void setMinAndMaxOptionsForStartMenu()
{
_minOption = START_MENU_END_APPLICATION_OPTION;
_maxOption = START_MENU_CONNECT_TO_SERVER_OPTION;
}
private static void askUserOptionForStartMenu()
{
askForUserOption();
readUserOption(askUserOptionForStartMenu);
checkUserOptionIsBetweenMinAndMaxOptions(askUserOptionForStartMenu);
}
private static void startMenuResponsalToOptionGiven()
{
if (_givenOption == START_MENU_CONNECT_TO_SERVER_OPTION)
{
//TODO Conectarse a un Servidor y desplegar las demás opciones
//TODO crear las demas opciones una vez conectado
throw new NotImplementedException();
}
}
#endregion
}
}
Main code:
namespace ARIP.Client.Console
{
class Program
{
static void Main(string[] args)
{
ClientOptions.ShowStartMenu();
}
}
}
static
code is procedural. It's not inherently "bad code", but it's certainly not idiomatic. That said I hope you get good reviews! \$\endgroup\$static class
you work with types, not objects - I find it odd that Singleton is the first thing they're teaching... it's actually pretty rare that an object requires existing in exactly one single instance. You'll learn a lot from the answers you'll get here, promise! \$\endgroup\$