Share via

Software pattern

Markus Freitag 3,791 Reputation points
2022-09-03T18:35:09.197+00:00

Hello,
Do you know any examples about the basic software patterns?

  • SingleTon
  • Factory
  • static

When is it better to use a static variable, class etc?
When not?
Where are the advantages/disadvantages.
Static is global, not an instantiation.
I but solve everything yet without static.
Maybe there is also a good pattern site with good short examples.
I would be asked why I work so little with static. It's not that clear to me, so I ask here.
The main question is
When do I take what? That there is, is already clear.
What are the criteria for the choice?

Maybe there is a simple explanation. Thank you.

Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments

Answer accepted by question author

  1. Karen Payne MVP 35,606 Reputation points Volunteer Moderator
    2022-09-04T22:31:36.76+00:00

    This is how I recommend to use a singleton.

    using static System.DateTime;  
    namespace WinFormsApp2.Classes;  
      
    public sealed class Singleton  
    {  
        private static readonly Lazy<Singleton> Lazy =   
            new(() => new Singleton());  
      
        public static Singleton Instance => Lazy.Value;  
        public DateTime DateTime { get; set; }   
        private Singleton()  
        {  
            DateTime = Now;  
        }  
    }  
    

    Was this answer helpful?

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Karen Payne MVP 35,606 Reputation points Volunteer Moderator
    2022-09-03T22:10:22.647+00:00

    In regards to a static class, if nothing needs instantiating this is a clear reason for a static class. I've not created a class in decades that needed instantiation. Look at extension methods, they need to be in a static class and the extension method must be static.

    Simple extension method

    internal static class DateOnlyExtensions  
    {  
        public static string Formatted(this DateOnly sender) => sender.ToString("MM/dd/yy");  
    }  
    

    A static class where we call DataOperations.CategoriesArray() of CategoriesArray() with a static using statement, similar to say DateTime.Now. or Console.WriteLine, we can use a static using statement so not to type everything out (good for some, bad for others)

    namespace MenuConsoleApp.Classes  
    {  
        public class DataOperations  
        {  
      
            public static Categories[] CategoriesArray() =>   
                JsonConvert.DeserializeObject<List<Categories>>(  
                    File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "json", "categories.json")))  
                    .ToArray();  
      
    
        }  
    }  
      
    

    In regards to Singleton, used for dependency injection for services in Startup.cs and Program.cs which make coding easier.

    public void ConfigureServices(IServiceCollection services)  
    {  
     services.AddRazorPages();  
     services.AddServerSideBlazor();  
      
     services.AddSingleton<RssNews>();  
    }  
      
    

    That's the tip of the iceberg, best to do research and come to your own conclusions.

    See also

    Was this answer helpful?

    1 person found this answer helpful.

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.