In my previous post about singleton pattern in C#, it is very nice flexible singleton code which make us be able to create a singleton instance of a non singleton class. But we can still be able to create a non singleton instance of the target class.
In this post I want to implement it in Java. As far as I know even though both C# and Java have generic feature, generic in C# quite different from the one in Java. While converting of what I did about generic singleton in C# to Java, I feel that the way I do in Java is not as elegant as in C#. The following code is the generic singleton code I write in Java.
Again, if we see from the sample code above it seems that generic singleton in C# is more elegant than in Java.
The main thing that I should do is I have to use HashMap collection to hold any instance of what that want to be as singleton and reuse for the future need in the life cycle of my application.
Anyway, if you have better and much more elegant solution how to implement generic singleton pattern in Java I am very appreciate for any suggestion. Finally I would like to thank you very much for visiting and reading my blog, and have a nice day.
Update March 11, 2016
If you want to the java singleton above, please ensure you have remove / clean up method to clean up HashMap contents if the singleton objects are not needed anymore. At least at the end of application lifecycle, the HashMap and its contents should be removed completely from memory.
In software engineering, the singleton pattern is a design pattern that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system. The concept is sometimes generalized to systems that operate more efficiently when only one object exists, or that restrict the instantiation to a certain number of objects. The term comes from the mathematical concept of a singleton.[1]
Honestly in my daily coding activity, sometimes I need to implement singleton pattern. Jon wrote a good singleton article in C# here. Another case I want to implement singleton pattern in generic way. I convert what has been wrotten by Jon into generic.
/// <summary>/// Generic class which makes non singleton class to be singleton./// Calling singleton instance of class should be Singleton<T>.Instance, otherwise the returned class is not singleton one. ////// </summary>/// <typeparam name="T">Genric class type to be singletonized</typeparam>publicsealedclassSingleton<T>whereT:class,new(){/// <summary>/// Private constuctor to avoid this class instantiated/// </summary>Singleton(){}/// <summary>/// Property to get access to singleton instance/// </summary>publicstaticTInstance{get{returnNested.instance;}}/// <summary>/// Private nested class which acts as singleton class instantiator. This class should not be accessible outside <see cref="Singleton<T>"/>/// </summary>classNested{/// <summary>/// Explicit static constructor to tell C# compiler not to mark type as beforefieldinit/// </summary>staticNested(){}/// <summary>/// Static instance variable/// </summary>internalstaticreadonlyTinstance=newT()}}
The following snipped code is a sample how to use the generic singleton class.
From the sample above is shown that we can make a non singleton class to be singleton at runtime. But we can still create a non singleton instance as we need. That’s really nice flexible thing.
At some point we need to store information when dealing with web application.
Storing information in web application can be implemented in some ways such as session and cookies.
Dealing with session case, previously I never got a situation where I should store the session state in persistance way till I got a condition implementing this way.
The scenario is we want to implement load balancer to our applications and services (web service, wcf) that hosted in IIS. The aim of this enhancement basically to improve availability and scalability of existing in house application without doing huge refactoring.
In this post I just want to share the short steps I did during researching implementing session state on a database.
First of all open command prompt and navigate to .NET framework installation path. In this case, my installation path is C:\Windows\Microsoft.NET\Framework64\v4.0.30319>
We will use a tool called aspnet_regsql.exe that shipped with .NET framework installation to generate database that will be used to store session state. To display detail argument needed by this tool we can use the following command:
The command will create database, tables and store procedures.
In web.config, don’t forget to add session state configuration by pointing the generated database.
<!--session state configuration--><system.web><sessionstatemode="SQLServer"allowcustomsqldatabase="true"sqlconnectionstring="Data Source={server\instance};Initial Catalog={databaseName}; user={username}; password={password}"cookieless="false"timeout="20"></sessionstate></system.web>
To test our session storage, just create simple aspx page.
In this post I will write about setup and configure mono on OS X. I need mono on my Mac since C# is one of my favorite programming language ;)
First of all let’s download Mono SDK and MonoDevelop. The order installation is Mono SDK first then MonoDevelop. The installation of Mono SDK and MonoDevelop should be easy by following the installation wizard.
When the installation finish, open terminal :
# mono version
mono -V
We should get info about the version of mono installed in our machine.
Try to create an ASP.NET Web Application project by selecting menu :
File > New > Solution. Select C# > ASP.NET > Web Application
Click Run > Run With > Mono Soft Debugger for ASP.NET
ASP.NET on Apache Web Server
To run ASP.Net on Apache web server, we need Mod mono module. It is an Apache 2.0/2.2 module that provides ASP.Net support for Apache web server. The steps installation are :
Install Apache web server. The steps for OS X Mountain Lion can be found in my previous post
Install Xcode, OS X development tool. It’s free and can be downloaded via App Store. We need it to compile mod mono installation source
Download Mod mono source installation here. At the time of write this article I use mod_mono-2.10.tar.bz2
Install mod mono module. Open terminal
Add mod_mono.conf reference in httpd.conf
Add the following code at the end of httpd.conf
Create a web directory with the path ~/Projects/Mono/TestMonoApache/TestMonoApache/
In that directory create an index.aspx file
Add an Apache configuration file for mono.
Add the following line at the end of mod_mono.conf
Restart apache server with command :
Open browser and hit http://localhost/testmonoapache/index.aspx
Cool, now our apache web server can receive request for asp.net page. Thanks for reading and see you in the next post :)
In this blog post will show us about step installing Apache Tomcat on OS X Mountain Lion. I assume we already have Java installed on our machine. The first step is downloading Tomcat binary distribution in Tomcat website. In this case I use version 7.0.30.
Extract the downloaded binary distribution onto ~/Download/apache-tomcat-7.0.30.
Create directory in /usr/local if we don’t have it yet.
sudo mkdir /usr/local
Move ~/Download/apache-tomcat-7.0.30 to /usr/local
To make it easy to replace this release with future releases, we are going to create a symbolic link that we are going to use when referring to Tomcat. Then we also will change owner and make script executable