Uses:- They have flexibility that can create documents flexible.
Create a console application name it anything paste all code given below;
Class MyApplication
{
Static void Main(String []args)
{
////////Constructor that calls factory method//////////////////////////
Document[] documents = new Documents[2];
documents[0] = new Resume();
documents[1] = new Report();
foreach( Documents doc in documents)
{
Console.WriteLine("\n"+ doc.getType().Name);
foreach(Page p in documents.Pages)
{
Console.WriteLine("\n"+ page.getType().Name);
}
}
Console.ReadLine();
}
}
//***********************Product Abstract Class*************************************
abstract Class Page
{
}
//*********************Concrete product Classes*************************************
Class skillPage : Page { }
Class EducationPage : Page { }
Class ExperiancePage : Page { }
//*****************************Abstract Class Creator*****************************
abstract Class Document
{
private List<Page> _mypage = new List<Page> ();
public Document()
{
this.CreatePages(); ------- Call to Abstrac Factory Method
}
public List<Page> Pages
{
get { return _mypage; }
}
//////////////////////Factory Method/////////////////
public abstract void CreatePages();
}
}
//********************Concrete Creator Class*********************************
Class Resume : Document
{
/////////////////Factory Method Implementation///////////
public override void CreatePages()
{
Pages..add( new skillPage () );
Pages..add( new EducationPage () );
Pages..add( new ExperiancePage () );
}
}
//************************Concrete Creator Class***************************
Class Report : Document
{
public override void CreatePages()
{
Pages..add( new NewClassNameWhichYouNeed() );
Pages..add( new NEWCLASSNAME() );
}
}
******************************************************************************
0 comments:
Post a Comment