Tag Archives: Design Pattern


MVC stands for Model-View-Controller and is a recognized design patterns for developing software applications. ASP.NET MVC based applications are made up of:

  • Models – These are classes that model the data of the application.
  • Views – These are templated files that generate HTML to be sent to the web browser. A view typically deals with displaying data from a model.
  • Controllers – These are classes that process incoming requests, obtain data model, and typically return this data to a view for displaying as HTML. Controllers may contain logic for filtering data based on information sent by the request.

Source: ASP.NET MVC with Entity Framework and CSS, Naylor L., pp. 1

Singleton Pattern

Singleton Pattern is a design pattern that prevents to create various objects of the same class when it is only necessary one.

For example, imagine a scenario where you have to insert your credentials (username and password). Before you go to a window after you have logged in successfully, behind the login form, the application creates a database connection and validate your credentials.

The database connection has been created and you want insert a new record to table called ‘Orders’. Will the application create another database connection or use the previous one?

Without a singleton pattern, the application will create another database connection (so the application will have two connections). With singleton pattern, the application will use the database connection created before.

The singleton pattern requires:

  • a static variable (e.g.: private static DBHelper uniqueInstance)
  • a static method (e.g.: public static DBHelper GetInstance(){…})
  • a private constructor (e.g.: private DBHelper(){…})

Remember, it uses a keyword ‘static’ because you needn’t instantiate the class. Below there are two classes (written in C#) to help you understand better.

public class DBHelper

	private static string connectionString = 
		"Server=PC\\SQLEXPRESS;" +
		"Database=myDatabase;" +
		"Integrated Security=SSPI;" +
		"User Id=John;" +
	private static SqlConnection sqlConnection;
	private string query;
	private SqlDataAdapter sqlDataAdapter;
	private DataTable dataTable;
	private int rows;

	private static DBHelper uniqueInstance;

	private DBHelper ()
		sqlConnection = new SqlConnection (connectionString);

	public static DBHelper GetInstance ()
		if (uniqueInstance == null)
			uniqueInstance = new DBHelper ();
		return uniqueInstance;

	public void OpenConnection ()
		if (!sqlConnection.State.Equals (ConnectionState.Open))
			sqlConnection.Open ();

	public void CloseConnection ()
		if (!sqlConnection.State.Equals (ConnectionState.Closed))
			sqlConnection.Close ();

	public int ValidateUser (string username, string password)
		query = "select * from utilizadores where username = '" + username + "' and password = '" + password + "' ";
		sqlDataAdapter = new SqlDataAdapter (query, sqlConnection);
		dataTable = new DataTable ();
		rows = sqlDataAdapter.Fill (dataTable);
		return rows;
public class Login : MonoBehaviour
	public GameObject username;
	public GameObject password;

	private MD5 md5;
	private DBHelper mDBHelper;

	// Use this for initialization
	void Start ()
		username = GameObject.Find ("userInputField");
		password = GameObject.Find ("passwordInputField");
		mDBHelper = DBHelper.GetInstance ();
		md5 = new MD5CryptoServiceProvider();
	// Update is called once per frame
	void Update ()

	//void OnMouseDown()
	public void OnLogin ()
		UnityEngine.UI.InputField user = username.GetComponent<InputField> ();
		UnityEngine.UI.InputField pass = password.GetComponent<InputField> ();
		string passwordEncrypted = md5Hash(pass.text);

		if (user.text.Length != 0 && pass.text.Length != 0) {
			if (mDBHelper.ValidateUser(user.text, passwordEncrypted) == 1) {
				Application.LoadLevel ("SceneIntro");
 				Debug.Log("Invalid Login!");
		} else {
			Debug.Log("Invalid Login!");

	private string md5Hash(string password){
		md5.ComputeHash (ASCIIEncoding.ASCII.GetBytes (password));

		byte[] result = md5.Hash;

		StringBuilder stringBuilder = new StringBuilder ();
		for (int i=0; i < result.Length; i++) {
			//change it into 2 hexadecimal digits for each byte
			stringBuilder.Append(result[i].ToString ("x2"));
		return stringBuilder.ToString ();

Note: this design pattern was the first I have ever implemented at my career. 😀

Facade Pattern

I was studying how to develop enterprise applications through this book. After I have followed all steps indicated from the book I was curious about files from jpacontroller folder. All those files contains word Facade at the end.

So what that means? I presume that means a design pattern called Facade Pattern is used. From that, I have another question: what is Facade Pattern job?

According to this source, Facade Pattern simplify the interface to large body code. Look at the image below and compare the difference.

Facade Pattern - Image to understand the job of Facade Pattern
Facade Pattern


Facade Pattern is used when it is necessary to have a cleaner code and a separation of front-end presentation and back end functionality.

Data Access Object

Data Access Object also know as DAO is a J2EE Design Pattern learned from this book.

There is a site explains this design pattern.


You want to encapsulate data access and manipulation in a separate layer.

  • You want to implement data access mechanisms to access and manipulate data in a persistent storage.
  • You want to decouple the persistent storage implementation from the rest of your application.
  • You want to provide a uniform data access API for a persistent mechanism to various types of data sources, such as RDBMS, LDAP, OODB, XML repositories, flat files, and so on.
  • You want to organize data access logic and encapsulate proprietary features to facilitate maintainability and portability.

Use a Data Access Object to abstract and encapsulate all access to the persistent store. The Data Access Object manages the connection with the data source to obtain and store data.