Design Patterns - Façade Pattern
Sample code
public class IProduct {
public static boolean CheckProductExists(string ProductName)
{
if (GetProductId(ProductName) ==0)
{
return false;
}
else return true;
}
Public static integer GetProductId(string ProductName)
{
Map<string, integer> Prod= iDataLoad.GetProduct();
if (prod.get(ProductName)== null)
{
return 0;
}
else
{return prod.get(ProductName);
}
}
}
----------------------------------------------------------
public class iDataLoad {
public static Map<string, integer> GetProduct()
{
Map<string, integer> Prod = new Map<string, integer> ();
prod.put( 'B1', 1);
prod.put('B2',2);
prod.put('B3', 3);
return prod;
}
public static Map<integer, string> ProdInStock()
{
Map<integer, string> Prod = new Map<integer, string> ();
prod.put( 1, 'B1');
prod.put(2,'B2');
return prod;
}
}
------------------------------------------------------------
public class ICart {
public static List<integer> Item = new List<Integer>();
public static Map<integer, List<List<object>>> Mapcat = new Map<integer, List<List<object>>>();
public static void AddtoCart (integer ProdId)
{
Item.add(ProdId);
}
public static Integer CheckOut(list<Integer> Prod, string Address)
{
List<List <object>> cat = new List<List <object>>();
List<string> add = new list<string>{Address};
Cat.add(Item);
cat.add(add);
Mapcat.Put(1, cat);
return 1;
}
}
-------------------------------------------------------------------
public class FacadePattern {
public Static string AddItemtoCart(string Item)
{
if (IProduct.CheckProductExists(Item))
{
integer ProdId = IProduct.GetProductId(item);
ICart.AddtoCart(ProdId);
return 'Item Added';
}
else return 'No Item found in stock.Please add another item';
}
// Testing only method
Public static Map<integer, List<List<object>>> Get()
{
return ICart.Mapcat;
}
public static integer CheckOut (string address)
{
return ICart.CheckOut( ICart.Item, Address);
}
public static string Pay(string CardInfo, string creditDate, string Code, integer CheckoutId)
{
return IPayInterface.Pay(CardInfo,creditDate,Code,CheckoutId);
}
}
Comments
Post a Comment