Dofactory.com
Dofactory.com
 Back to list
Views:   5.7K
Replies:  0
Archived

Please review my Factory Pattern implementation

Hi I am starting a school project for which decided to go by an Interface pattern.
Please review the code and let me will it work out for future implementation example adding WCF project.

Thank you!


class Program
{
    static void Main(string[] args)
    {
        StudentView view = new StudentView();
        view.ShowStudent();
    }
}

//Student UI 
public class StudentView
{
    IAcademic _academic = new Academic();
    IParents _parents = new Parents();
    IService _service = new BusinessFactory("Student");
    IStudent _student;

    public StudentView()
    {
        _student = (IStudent)_service.GetService();
        _academic.Year = "2010";
        _parents.MotherName = "Test";
        _student.StudentName = "Imran";
        _student.Academic = _academic;
        _student.Parents = _parents;
    }

    public void ShowStudent()
    {
        _student.ShowStudent();
    }
}

//Academic Details Interface 
public interface IAcademic
{
    string Year { get; set; }
}

//Parent Details Interface 
public interface IParents
{
    string MotherName { get; set; }
}

//Student Detials Interface 
public interface IStudent
{
    IAcademic Academic { get; set; }
    IParents Parents { get; set; }
    string StudentName { get; set; }
    void ShowStudent();
}

//Common Service 
public interface IService
{
    IService GetService();
}

//Object Factory 
public class BusinessFactory : IService
{
    string _name = string.Empty;

    public BusinessFactory(string name)
    {
        _name = name;
    }

    #region IService Members

    public IService GetService()
    {
        switch (_name)
        {
            case "Student":
                return new StudentManager().GetService();
                break;
            default: return null;
        }
    }

    #endregion
}

//Academic UserControl 
public class Academic : IAcademic
{
    string _year;

    #region IAcademic Members

    public string Year
    {
        get
        {
            return _year;
        }

        set
        {
            _year = value;
        }
    }

    #endregion
}

//Parent UserControl 
public class Parents : IParents
{
    string _motherName;

    #region IParents Members

    public string MotherName
    {
        get
        {
            return _motherName;
        }

        set
        {
            _motherName = value;
        }
    }

    #endregion
}

//Student Manager(BL) 

public class StudentManager : IStudent, IService
{
    IAcademic _iacademic;
    IParents _iparents;
    string _studentName;

    #region IStudent Members

    public IAcademic Academic
    {
        get
        {
            return _iacademic;
        }

        set
        {
            _iacademic = value;
        }
    }

    public IParents Parents
    {
        get
        {
            return _iparents;
        }

        set
        {
            _iparents = value;
        }
    }

    public string StudentName
    {
        get
        {
            return _studentName;
        }

        set
        {
            _studentName = value;
        }
    }

    #endregion

    #region IStudent Members

    public void ShowStudent()
    {
        Console.WriteLine("Name:{0}\nMother Name:{1}\nYear:{2}", _studentName, _iparents.MotherName, _iacademic.Year);
        Console.ReadLine();
    }

    #endregion

    #region IService Members
    public IService GetService()
    {
        return this;
    }

    #endregion
}

 
Imran Khan, Apr 06, 2011
Stay Inspired!
Join other developers and designers who have already signed up for our mailing list.
Terms     Privacy     Cookies       Do Not Sell       Licensing      
Made with    in Austin, Texas.  - vsn 44.0.0
© Data & Object Factory, LLC.