Search This Blog

2013/11/21

Bridge Design Pattern & Logging System

   While going through some basic stuff about design pattern, I come across the Bridge design pattern & decided to implement it for Logging system. 
  My take is to separate concern about What Is Being Logged & where is being Logged.
An application can log errors as well as user actions too. Same time logging can go to various places like Database, EventLog or a File. 
   Here is Code for Classes that implement Logging system as Bridge Design pattern.
Code here is from a console application 'POALoggingAgent' with a class file 'LoggingAgent.cs' & implicitly created entry point i.e. Program.cs.

[LoggingAgent.cs]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Data;
using System.Data.SqlClient;

namespace POALoggingAgent
{
    //Process of Logging Should be Decoubled from Loggable Message
    abstract class LogImplementor
    {
        public abstract bool Log(string Message);
    }

    //Let us to change Logging Implementation at any latter moment when new requirement arise
    class LogAbstraction
    {
        protected LogImplementor Implementor;
        protected string Message;

        public LogAbstraction(string Msg)
        {
            Message = Msg;
        }

        // Implementor
        public LogImplementor LogImplementor
        {
            set
            {
                Implementor = value;
            }
        }

        public virtual  bool Log()
        {
            return Implementor.Log(Message);
        }
    }

    //what is to be logged an error,a user event,application event etc
    class ErrorLogAbstraction : LogAbstraction
    {
        Exception LogggbleException;
        public ErrorLogAbstraction(Exception exp):base(string.Empty)
        {
            LogggbleException = exp;
        }

        public override bool Log()
        {
            /*manipulate exception object propeties to build */
            Message = FormatMessage();
            return Implementor.Log(Message);
        }

        public string FormatMessage()
        {
            //Do Some work on LogggbleException to create formated message and set it to Message inherited Variable
            return "***" + LogggbleException .Message+ "***";
        }
    }

    //initial logging was on console
    class ConsoleLogImplementor : LogImplementor
    {
        public override bool Log(string Message)
        {
            try
            {
                /*BEGIN - Code To Log Message To Console*/
                /*END   - Code To Log Message To Console*/

                //Place Holder
                Console.WriteLine("Class:ConsoleLogImplementor,Method:Log,Message:" + Message);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }

    //logging to database is new requirement generated
    class DbLogImplementor : LogImplementor
    {
        public override bool Log(string Message)
        {
            try
            {
                /*BEGIN - Code To Log Message To DB*/
                /*END   - Code To Log Message To Db*/

                //Place Holder
                Console.WriteLine("Class:DbLogImplementor,Method:Log,Message:" + Message);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}

Calling above code from our console Application

[Program.cs]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace POALoggingAgent
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                int i = 0;
                int j = 1;
                Console.WriteLine("Trying to Divide 1 by 0,Output = " + Convert.ToString(j / i));
            }
            catch (Exception exp)
            {
                LogAbstraction LogAb = new ErrorLogAbstraction(exp);

                //  choose console Logging Implementation
                LogAb.LogImplementor = new ConsoleLogImplementor();
                LogAb.Log();

                // choose Database Logging Implementation
                LogAb.LogImplementor = new DbLogImplementor();
                LogAb.Log();
            }
            // Wait for user
            Console.ReadKey();
        }
    }
} 
   To be Frank, I am not sure if Bridge Design Pattern is suitable case for Logging system as there are Singleton & Command, Chain of Responsibility & Subscriber- Publisher; that are often used with logging system.

   Anyone who willing to help me to improve above design of so called Logging system is more than welcomed!

2013/11/19

UL LI based Drag & DROP with Pure Javascipt & HTML 5

   Currently I am working on UL& LI based DRAG & DROP functionality on my own with pure javascript just for fun as HTML 5 is interesting stuff to explore.
  The functionality to drag an element from other UL is fine but some party is still edgy like re arranging LI's within UL.

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function dragStart(ev) {
            ev.dataTransfer.effectAllowed = 'move';
            ev.dataTransfer.setData("outerHTML", ev.target.outerHTML);
            ev.dataTransfer.setData("item", ev.target.innerHTML);
            ev.dataTransfer.setData("parent_id", ev.target.parentNode.id);
            ev.dataTransfer.setDragImage(ev.target, 0, 0);
            return true;
        }
        function dragEnter(ev) {
            ev.preventDefault();
        }
        function dragOver(ev) {
            ev.preventDefault()
        }
        function dragDrop(ev) {
           var dragged_li_outer_html = ev.dataTransfer.getData("outerHTML");
            var parent_id = ev.dataTransfer.getData("parent_id");
            var current_id = ev.target.id;
          

            //remove from current location
            for (i = 0; i < document.getElementById(parent_id).children.length; i++) {
                if (document.getElementById(parent_id).children[i].outerHTML == dragged_li_outer_html) {
                    document.getElementById(parent_id).removeChild(document.getElementById(parent_id).children[i]);
                }
            }

            if (parent_id == current_id) {
                //rearrage li of same ui
                alert('Not Supported Yet!');
                return false;
            } else {
                //dragged from other ul and dropped
              
            }
            ev.target.innerHTML = ev.target.innerHTML + dragged_li_outer_html;
            ev.stopPropagation();
            return false;
        }
        function ChilddragDrop(ev) {
            var parent_id = ev.dataTransfer.getData("parent_id");
            var current_id = ev.target.parentNode.id;

            var item = ev.dataTransfer.getData("item");

            if (parent_id == current_id) {
                //rearrage li of same ui
                alert('Not Supported Yet!');
                return false;
            } else {
                //dragged from other ul and dropped

            }

            //remove from source
            for (i = 0; i < document.getElementById(parent_id).children.length; i++) {
                if (document.getElementById(parent_id).children[i].outerHTML == ev.target.outerHTML) {
                    document.getElementById(parent_id).removeChild(document.getElementById(parent_id).children[i]);
                }
            }

            var obj = document.createElement("li");
            for (i = 0; i < ev.target.attributes.length; i++) {
                obj.setAttribute(ev.target.attributes[i].name, ev.target.attributes[i].value);
            }
            var t = document.createTextNode(item);
            obj.appendChild(t);
           
            ev.target.parentNode.insertBefore(obj, ev.target);
            //no bubble up
            ev.stopPropagation();
            return false;
        }
        function dragEnd(ev) {
            ev.preventDefault();
        }
        function mouseup(elt)
        {
           
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
       <div style="border:1px solid black;width:300px;height:300px">
           <span style="text-align:center;display:block;font-weight:bold;">Empty Bucket</span>
           <ul id="dest" draggable="false" 
                          ondragenter="return dragEnter(event)"
                          ondrop="return dragDrop(event)"
                          ondragover="return dragOver(event)"
                          style="border:1px solid red;width:200px;height:200px;background-color:yellow;margin:auto;">

           </ul>
       </div>
        <div style="border:1px solid black;width:300px;height:300px;">
            <span style="text-align:center;display:block;font-weight:bold;">Fruit Basket</span>
            <ul id="source" draggable="false" style="border:1px solid red;width:200px;height:200px;background-color:red;margin:auto;"
                          ondragenter="return dragEnter(event)"
                          ondrop="return dragDrop(event)"
                          ondragover="return dragOver(event)" >
                        <li draggable="true" ondragstart="return dragStart(event)" onmouseup="mouseup(this)" ondragend="dragEnd(event)" ondrop="return ChilddragDrop(event)">mango</li>
                        <li draggable="true" ondragstart="return dragStart(event)" onmouseup="mouseup(this)" ondragend="dragEnd(event)" ondrop="return ChilddragDrop(event)">orange</li>
                        <li draggable="true" ondragstart="return dragStart(event)" onmouseup="mouseup(this)" ondragend="dragEnd(event)" ondrop="return ChilddragDrop(event)">apple</li>
                        <li draggable="true" ondragstart="return dragStart(event)" onmouseup="mouseup(this)" ondragend="dragEnd(event)" ondrop="return ChilddragDrop(event)">banana</li>
                        <li draggable="true" ondragstart="return dragStart(event)" onmouseup="mouseup(this)" ondragend="dragEnd(event)" ondrop="return ChilddragDrop(event)">pineapple</li>
                        <li draggable="true" ondragstart="return dragStart(event)" onmouseup="mouseup(this)" ondragend="dragEnd(event)" ondrop="return ChilddragDrop(event)">guava</li>
                        <li draggable="true" ondragstart="return dragStart(event)" onmouseup="mouseup(this)" ondragend="dragEnd(event)" ondrop="return ChilddragDrop(event)">jackfruit</li>
                        <li draggable="true" ondragstart="return dragStart(event)" onmouseup="mouseup(this)" ondragend="dragEnd(event)" ondrop="return ChilddragDrop(event)">cashew</li>
            </ul>
        </div>
    </div>
    </form>
</body>
</html>

Here is Screenshot:



Happy Coding !