Pet Shop 4.0 – Abstract Factory Pattern

This is the last article about Pet Shop 4.0 source code. some other code such as singleton pattern will be too simple to explain.

When BLL Layer to get data from database, Pet Shop 4.0 uses “Abstract factory pattern”.
This pattern is classic in nowaday development and It is right example in real project.

1
2
3
     Category category = new Category();
     // Category from PetShop.BLL.
     // Check following code.  It has "dal" factory, which is created by DataAccess Object.
1
2
3
4
5
6
7
namespace PetShop.BLL {
 
    public class Category {
 
        private static readonly ICategory dal = PetShop.DALFactory.DataAccess.CreateCategory();
    }
}

DataAccess Loads DLL and creates new class Category in namespace “PetShop.SQLServerDAL”.
Now “dal” inside class “PetShop.BLL.Category” point to “PetShop.SQLServerDAL.Category”
Any Interface Method “dal” calls will be executed by “PetShop.SQLServerDAL.Category”.

check the following code.

1
2
3
4
5
6
7
8
9
namespace PetShop.DALFactory {
public sealed class DataAccess {
 
	public static PetShop.IDAL.ICategory CreateCategory() {
            string className = path + ".Category";
            return (PetShop.IDAL.ICategory)Assembly.Load(path).CreateInstance(className);
        }
   }
}
1
2
3
4
5
6
7
8
9
10
namespace PetShop.SQLServerDAL {
 
    public class Category : ICategory {
 
	public IList<CategoryInfo> GetCategories() {}
 
 
	public CategoryInfo GetCategory(string categoryId) {}
    }
}
This entry was posted in PetShop4.0. Bookmark the permalink.