Вы находитесь на странице: 1из 102

SearchENgine From Priyanka: WindowsFormProject: References: Microsoft.CSharp SearchEngineLib System System.Core System.Data System.Data.DataSetExtensions System.Deployment System.Drawing System.Windows.Form System.

Xml System.Xml.Linq

create a libraryclass named SearchEngineLib. References: Microsoft.CSharp System System.Core System.Data System.Data.DataSetExtensions System.Xml System.Xml.Linq Roots(folder): AbstractRootFinder.cs ActiveRootFinder.cs InvalidRootDescriptionException.cs IRootFinder.cs RootFinderEnum (others->codefile) RootFinderFactory.cs

SystemRootFinder.cs Search(Folder): FileSearcher.cs SearchHistoryManager.cs SearchLog.cs SearchManager.cs SearchResult.cs

AbstractRootFinder.cs using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace SearchEngineLib.Roots { public abstract class AbstractRootFinder : IRootFinder { protected List<String> listOfRoots;

public AbstractRootFinder() { listOfRoots = new List<String>(); }

public abstract List<String> GetRoots();

public int GetNumberofRoots() { return listOfRoots.Count; } } }

ActiveRootFInder.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO;

namespace SearchEngineLib.Roots { public class ActiveRootFinder:AbstractRootFinder { public override List<String> GetRoots() { DriveInfo[] activeDrive = DriveInfo.GetDrives(); foreach (DriveInfo drive in activeDrive) { if (drive.IsReady) listOfRoots.Add(drive.Name); }

return listOfRoots;

} } }

InvalidRootDescriptionException.cs: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO;

namespace SearchEngineLib.Roots { public class ActiveRootFinder:AbstractRootFinder { public override List<String> GetRoots() { DriveInfo[] activeDrive = DriveInfo.GetDrives(); foreach (DriveInfo drive in activeDrive) { if (drive.IsReady) listOfRoots.Add(drive.Name); }

return listOfRoots; } } }

IRootFinder.cs using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace SearchEngineLib.Roots { public interface IRootFinder { List<String> GetRoots();//returns list of drives int GetNumberofRoots();//returns number of drives } }

RootFinderEnum using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace SearchEngineLib.Roots { enum RootFinderEnum { ACTIVE=1, ALL

}; }

RootFInderFactory.cs using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace SearchEngineLib.Roots { public class RootFinderFactory {

static IRootFinder folder; //RootFinderFactory rootFinderFactory;

private RootFinderFactory() { //if (rootFinderFactory == null) // } rootFinderFactory = new RootFinderFactory();

public static IRootFinder Create(int i) { if (i == 1) folder = new ActiveRootFinder(); else if (i == 2)

folder = new SystemRootFinder(); else throw new InvalidRootDescriptionException("Invalid Roots"); return folder; } } }

SystemRootFinder.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO;

namespace SearchEngineLib.Roots { public class SystemRootFinder:AbstractRootFinder { public override List<String> GetRoots() { DriveInfo[] systemDrive = DriveInfo.GetDrives(); foreach (DriveInfo drive in systemDrive) { listOfRoots.Add(drive.Name); } return listOfRoots; }

} }

FileSearcher.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO;

namespace SearchEngineLib.Search { public class FileSearcher { public String fileToSearch { get; set; } public String driveToSearch { get; set; } //searchresult object private SearchResult sResult = new SearchResult();

public FileSearcher(String _fileToSearch, String _driveToSearch) { fileToSearch = _fileToSearch; driveToSearch = _driveToSearch; } //startsearch,searchforfile,reterive searched file

public void StartSearch() {

SearchForFile(fileToSearch, driveToSearch); }

public void SearchForFile(String fileToSearch, String driveToSearch) { try { if (File.Exists(driveToSearch + "\\" + fileToSearch)) { sResult.AddFileFound(driveToSearch + "\\" + fileToSearch); } sResult.NumberOfFoldersScanned++;

String[] dir = Directory.GetDirectories(driveToSearch); foreach (String p in dir) { SearchForFile(fileToSearch, p); } } catch (Exception ex) {

} }

public SearchResult GetSearchResult() { return sResult;

} }

SearchHistoryManager.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO;

namespace SearchEngineLib.Search { public class SearchHistoryManager { public void SaveSearchHistory(SearchLog searchLog, String fileName) { StreamWriter writer = new StreamWriter("E:SearchEngine" + fileName + ".txt"); foreach (String file in searchLog.GetRoots()) { SearchResult sr = searchLog.GetSearchResult(file); foreach (String s in sr.GetFileFound()) { writer.WriteLine(s); } } }

public List<String> GetSearchHistory(String driveName) {

List<String> fileLsit = new List<string>(); try { String line = ""; StreamReader sr = new StreamReader("E:SearchEngine" + driveName + ".txt");

while ((line = sr.ReadLine()) != null) {

fileLsit.Add(line); } } catch (Exception e) { } return fileLsit; } } }

SearchLog.cs using System; using System.Collections.Generic; using System.Linq;

using System.Text;

namespace SearchEngineLib.Search { public class SearchLog { Dictionary<String, SearchResult> searchLog; public SearchLog() { searchLog = new Dictionary<String, SearchResult>();//String(key)=Drivename }

public void AddSearchResult(String driveName, SearchResult result) { searchLog.Add(driveName, result); }

public SearchResult GetSearchResult(String driveName) { //return (SearchResult)searchLog[driveName];//only the search result without drivename return searchLog[driveName];//both drivename and search result }

public ICollection<String> GetRoots() { return searchLog.Keys; }

} }

SearchManager.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.IO;

namespace SearchEngineLib.Search { public class SearchManager { SearchLog sLog = new SearchLog(); int i = 0; public SearchLog StartSearch(List<String> drives, String fileName) { //object filesearch[] FileSearcher[] fileSearcher = new FileSearcher[drives.Count]; //object Thread[] Thread[] thread = new Thread[drives.Count];

FileSearcher fs1; Thread t; foreach (String d in drives) {

fs1 = new FileSearcher(fileName, d); fileSearcher[i] = fs1; t = new Thread(fileSearcher[i].StartSearch); t.Start(); thread[i] = t; i++; }

foreach(Thread th in thread) { try { th.Join(); } catch (ThreadInterruptedException e) { } catch (ThreadStateException ex) { } }

for (i = 0; i < fileSearcher.Length; i++) { sLog.AddSearchResult(drives[i], fileSearcher[i].GetSearchResult()); }

SearchHistoryManager searchHistoryMgr = new SearchHistoryManager();

searchHistoryMgr.SaveSearchHistory(sLog, fileName);

return sLog; } } }

SearchResult.cs using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace SearchEngineLib.Search { public class SearchResult { public int NumberOfFoldersScanned { get; set; } List<String> filefound; public SearchResult() { filefound = new List<String>(); }

public void AddFileFound(String file) { filefound.Add(file); }

public List<String> GetFileFound() { return filefound; } } }

Program.cs using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms;

namespace SearchEngineGUI { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); }

} }

Form1.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;

using SearchEngineLib.Roots; using SearchEngineLib.Search;

namespace SearchEngineGUI { public partial class Form1 : Form { public Form1() { InitializeComponent(); } List<String> driveSearch = new List<String>(); private void rbtnActive_CheckedChanged(object sender, EventArgs e) { listBox1.Items.Clear(); IRootFinder iRootFinderActive = RootFinderFactory.Create(1); foreach (String s in iRootFinderActive.GetRoots()) listBox1.Items.Add(s);

private void rbtnSystem_CheckedChanged(object sender, EventArgs e) { listBox1.Items.Clear(); IRootFinder iRootFinderSystem = RootFinderFactory.Create(2); foreach (String s in iRootFinderSystem.GetRoots()) listBox1.Items.Add(s); }

private void btnSearch_Click(object sender, EventArgs e)

{ if (listBox1.Items.Count > 0) { for (int i = 0; i < listBox1.Items.Count; i++) { driveSearch.Add(listBox1.Items[i].ToString()); } listBoxSearchResult.Items.Clear(); StartSearch(); } else { listBoxSearchResult.Items.Add("No Drive Selected......."); } }

private void btnHistory_Click(object sender, EventArgs e) { History(); }

public void StartSearch() { String fileName = txtBoxFileName.Text; SearchManager sManager = new SearchManager(); SearchLog log = sManager.StartSearch(driveSearch, fileName); DisplaySearchResult(log); }

public void History()

{ SearchHistoryManager sm = new SearchHistoryManager(); List<string > l = sm.GetSearchHistory(txtBoxFileName.Text); if (l.Count > 0) { listBoxSearchResult.Items.Add("History Available"); foreach(string f in l){ listBoxSearchResult.Items.Add(f); } } else { listBoxSearchResult.Items.Add("No History Available"); } }

public void DisplaySearchResult(SearchLog sLog) { foreach(string drive in driveSearch) { listBoxSearchResult.Items.Add("Drive "+drive+"---The number foler scanned : "+sLog.GetSearchResult(drive).NumberOfFoldersScanned); foreach(string file in sLog.GetSearchResult(drive).GetFileFound()) { listBoxSearchResult.Items.Add(file); } } } } }

---------------------------------------------------------------------------------------------ThreadsRevision:
Notes.Txt:
Delegate holds the address of objects(methods) two types of delegates: singlecast delegates, multicast delegates. Events are restricted form of delegates. ---------------------------------------------------------------------------Separation of Concern:(OO Principle SRP:Single Responsibility Principle) A class must have a single task and all its methods must do smthng related to that single task only Ex: code to Send alerts must be placed in separate class and code for deposit and withdraw must be placed in a separate class. -----------------------------------------------------------------------------------

OCP:Open CLosed Principle: A class must have only one reason to modify. Classes must be loosely coupled that is they must no depend more on other classes. If we call the SendEmail method of ALert class in Deposit method of Account class to send the emial immediately after a deposit is done, then it is tightly coupled.That is Account class is dependent on Alert class. There is no need for Account class to kno anything about any changes done on the Alert mechanism so we shouldn't call methods of Alert class in Account class. Ex of loosely coupled: If the person wants to kno abt deposits and withdraws he will subscribe for alerts and then only email or sms must be sent. Here Account doesn't have any code to call Alert methods. ALso if there is any change in Alert class Account class need not be changed. Thus they are loosely couple.

BUt still we want to send alert whenever there is a withdrawl or deposit. FOr this we must call Alert methods in Account class. Since aCCOUNT class and Alert class are loosely coupled the Account class doesn't kno the methods of Alert class. Now to call the methods without using their name we use DELEGATEs.

Program.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading;

namespace ThreadsRevision { class Program { static void Main1(string[] args) { Console.WriteLine("Main Thread executed by {0}", Thread.CurrentThread.ManagedThreadId); // displays the id of the thread that is executing here. The id is given br the clr

ThreadStart ts1 = new ThreadStart(Method1);// ts1 is a delegate and it holds the address of Method1. If we want to execute a method in a separate thread other than the main thread, it must and should be void and should not take any parameters. If we want to execute a method with return type other than void in a separate thread then we must call it inside another method which returns void and with no parameters. ThreadStart ts2 = new ThreadStart(Method2);

ParameterizedThreadStart pts1 = new ParameterizedThreadStart(Method4);// pts1 is a delegate which holds the address of Method4. We use ParameterizedThreadStart is a delegate that can hold the address of a method that has a parameter and returns void.

Thus we can run a parameterized method that returns void in a separate thread only if it takes a parameter of type Object.

SomeTask task=new SomeTask(); task.Data1="data1"; task.Data2="data2";

ThreadStart ts=new ThreadStart(task.Run); Thread t=new Thread(ts); t.Start(); t.Join(); Console.WriteLine("Result {0}",task.Result);//will display 0 as the main thread and child thread are executing parallelly.(chid thread executes the method and main thread executes the COnsole statement).

Thread t1 = new Thread(ts1); Thread t2 = new Thread(ts2); Thread t3 = new Thread(pts1);

t1.Start(); t2.Start(); t3.Start("hi");

} static void Method1() { Console.WriteLine("Main Thread executed by {0}", Thread.CurrentThread.ManagedThreadId); Console.WriteLine("Method1"); }

static void Method2() { Console.WriteLine("Main Thread executed by {0}", Thread.CurrentThread.ManagedThreadId); Console.WriteLine("Method2"); } static int Method3(string s) { return 1; } static void Method4(Object o) { Console.WriteLine(o); } }

//to execute a method that is parameterized and returns anything other than void place it in another class and all the parameters it takes and the values it returns must declared as properties. //the call it inside a method with no return type and void. Noe in another class create a task using SomeTask() and give the task to the delegate ThreadStart . class SomeTask { public string Data1 { get; set; } public string Data2 { get; set; } public int Result { get; set; }

public void Run() { Result=Method3(Data1,Data2);

} public int Method3(string data1, string data2) { return data1.Length + data2.Length; } }

ShortFormToRunParallelThreads.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Diagnostics;// for StopWatch() using System.Threading.Tasks;

namespace ThreadsRevision { class ShortformTO_run_ParallelThreads { static void Main(string[] args) { Stopwatch watch=Stopwatch.StartNew(); Console.WriteLine("Executing sequence...."); //ThreadStart ts1 = new ThreadStart(Method1);// ts1 is a delegate and it holds the address of Method1. If we want to execute a method in a separate thread other than the main thread, it must and should be void and should not take any parameters. If we want

to execute a method with return type other than void in a separate thread then we must call it inside another method which returns void and with no parameters. //ThreadStart ts2 = new ThreadStart(Method2); //Thread t1 = new Thread(ts1); //Thread t2 = new Thread(ts2); //t1.Start(); //t2.Start(); //t1.Join(); //t2.Join(); Parallel.Invoke(Method1, Method2);

Console.WriteLine(watch.ElapsedMilliseconds); //System.Threading.Tasks.Parallel } static void Method1() { //for (int i = 1; i <= 10; i++) Parallel.For(1, 10, i => {

Thread.Sleep(500); });

} static void Method2() { //for (int i = 1; i <= 20; i++) Parallel.For(1, 10, i => {

Thread.Sleep(500); });

} }

DelegatesRevision:
Program.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace DelegatesRevision { class Program { static void Main1(string[] args) { Account acc1 = new Account(); acc1.notify += Alert.SendEmail;//Subscription for email acc1.notify += Alert.SendSMS;//Subscription for email acc1.Deposit(1000); Console.WriteLine(acc1.Balance); acc1.notify -= Alert.SendEmail;//Unsubscription for email //acc1.notify("10000000000000 deposited in your account"); causes error as

"notify" is an event in class Acount and hence cant be raised in any class other than Account. acc1.Deposit(1000); Console.WriteLine(acc1.Balance); acc1.Withdraw(500); Console.WriteLine(500); } }

public delegate void Notify(string msg);

class Account { public double Balance { get; private set; } public event Notify notify;//=new Notify(Alert.SendMail); we simply just declare a reference variable as Account class doesn't kno the methods of Alert class. //notify is an event and so it can be raised only within this class. We cant raise it in any other class even by using the object of this class. If notify is not an event the it can be called in another class(main program) using account object that is any authorised Account holder can call notify even without doing deposit or withdrawl using the stmt acc1.notify("10000000000000 deposited in your account"); As we made notify as event it cant be done. public void Deposit(double amount) { Balance += amount; if(notify!=null) notify("amount deposited......."); //Alert.SendEmail("amount deposited......."); } public void Withdraw(double amount) {

Balance -= amount; if (notify != null) notify("amount withdrawn........"); // Alert.SendEmail("amount withdrawn......"); }

class Alert { public static void SendEmail(string msg) { //write logic to send email notification Console.WriteLine("Email:{0} " + msg); } public static void SendSMS(string msg) { //write logic to send email notification Console.WriteLine("SMS:{0} " + msg); } } }

Program1.cs
using System; using System.Collections.Generic; using System.Linq;

using System.Text; using System.Diagnostics;

namespace DelegatesRevision { public delegate bool Filter(Process p); class Program2 { static void Main(string[] args) { //ShowProcessList(); //ShowProcessList("c"); // ShowProcessList(10 * 1024 * 1024); Filter filter = new Filter(FilterByName); ShowProcessList(ByMemSize); ShowProcessList(p => p.ProcessName.EndsWith("a")); //we dont kno on wat basis the client wants to display the processes. so here we use delegates. } static bool ByMemSize(Process p) { if (p.ProcessName.EndsWith("a")) return true; else return false; } static bool FilterByName(Process p) { if (p.ProcessName.EndsWith("a"))

return true; else return false; } static void ShowProcessList(Filter filter)//display all processes { foreach (Process p in Process.GetProcesses()) { if(filter(p)) Console.WriteLine(p.ProcessName); } } //static void ShowProcessList(string sw) //display all processes that start with a particular letter //{ // // // // // //} //static void ShowProcessList(long size)//display all processes whose memory >= given size //{ // // // // // } foreach (Process p in Process.GetProcesses()) { if (p.WorkingSet64>=size) Console.WriteLine(p.ProcessName); } foreach (Process p in Process.GetProcesses()) { if(p.ProcessName.StartsWith(sw)) Console.WriteLine(p.ProcessName);

//} } }

Design Fundamentals: Design1:


Interface1.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design1 { interface Interface1 { string getUnit(); string getParadigm(); string getName(); } }

ObjectOriented.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design1 { abstract class ObjectOriented:Interface1 { public string getUnit() { return "Class"; }

public string getParadigm() { return "ObjectOriented"; }

abstract public string getName();

} }

Procedural.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design1 { abstract class Procedural:Interface1

public string getUnit() { return "Function"; }

public string getParadigm() { return "Procedural"; }

abstract public string getName(); //{ // //} } } throw new NotImplementedException();

LangJava.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design1 { class LangJava:ObjectOriented

{ public override string getName() { return "Java"; } } }

LangCSharp.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design1 { class LangCSharp:ObjectOriented { public override string getName() { return "CSharp"; } } }

LangC.cs:
using System; using System.Collections.Generic; using System.Linq;

using System.Text;

namespace Design1 { class LangC:Procedural { public override string getName() { return "C"; } } }

Program.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design1 { class Program { static void Main(String[] args) { Interface1 i=null; char c;

int choice; do{

Console.WriteLine("1:Java"); Console.WriteLine("2:C#"); Console.WriteLine("3:C"); Console.WriteLine("Enter your Choice"); choice=int.Parse(Console.ReadLine());

switch(choice) { case 1: i=new LangJava(); break; case 2: i=new LangCSharp(); break; case 3: i=new LangC(); break; }; Console.WriteLine("Unit :"+i.getUnit()); Console.WriteLine("Paradigm :"+i.getParadigm()); Console.WriteLine("Name :"+i.getName());

Console.WriteLine("Press y to continue"); c=char.Parse(Console.ReadLine()); }while(c=='y'); }

} }

Design2:
Heterogeneous.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design1 { abstract class Heterogeneous:ProgrammingLanguageInterface { Procedural p ; ObjectOriented o ; public Heterogeneous() { p = new LangC(); o = new LangJava(); } public string getUnit() { string unit; unit=p.getUnit() + " and " + o.getUnit(); return unit;

public string getParadigm() { string paradigm; paradigm= p.getParadigm() + " and " + o.getParadigm(); return paradigm; }

abstract public string getName();

} }

LangC.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design1 { class LangC:Procedural { public override string getName() { return "C";

} } }

LangCPP.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design1 { class LangCPP:Heterogeneous { public override string getName() { return "Language CPP"; } } }

LangCSharp.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design1 { class LangCSharp:ObjectOriented { public override string getName() { return "CSharp"; } } }

LangJava.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design1 { class LangJava:ObjectOriented { public override string getName() { return "Java"; } }

ObjectOriented.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design1 { abstract class ObjectOriented:ProgrammingLanguageInterface { public string getUnit() { return "Class"; }

public string getParadigm() { return "ObjectOriented"; }

abstract public string getName();

} }

Procedural.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design1 { abstract class Procedural:ProgrammingLanguageInterface {

public string getUnit() { return "Function"; }

public string getParadigm() { return "Procedural"; }

abstract public string getName(); //{ // //} } throw new NotImplementedException();

Program.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design1 { class Program { static void Main(String[] args) { ProgrammingLanguageInterface i=null; char c; int choice; do{

Console.WriteLine("1:Java"); Console.WriteLine("2:C#"); Console.WriteLine("3:C"); Console.WriteLine("4:CPP"); Console.WriteLine("5:Exit"); Console.WriteLine("Enter your Choice"); choice=int.Parse(Console.ReadLine());

switch(choice) { case 1: i=new LangJava(); break; case 2: i=new LangCSharp(); break; case 3: i=new LangC(); break; case 4: i = new LangCPP(); break; case 5: return; }; Console.WriteLine("Unit :"+i.getUnit()); Console.WriteLine("Paradigm :"+i.getParadigm()); Console.WriteLine("Name :"+i.getName());

Console.WriteLine("Press y to continue"); c=char.Parse(Console.ReadLine()); }while(c=='y'); } } }

ProgrammingLanguageInterface.cs:
using System; using System.Collections.Generic;

using System.Linq; using System.Text;

namespace Design1 { interface ProgrammingLanguageInterface { string getUnit(); string getParadigm(); string getName(); } }

Design3:
Heterogeneous.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design2 { abstract class Heterogeneous:ProgrammingLanguageInterface { Procedural p; ObjectOriented o;

public Heterogeneous() { p = new ProceduralImplementation(); o = new ObjectOrientedImplementation(); } public string getUnit() { string unit = p.getUnit() +" and "+ o.getUnit(); return unit; }

public string getParadigm() { string paradigm; paradigm = p.getParadigm() + " and " + o.getParadigm(); return paradigm; }

abstract public string getName(); } }

LangC.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design2 { class LangC : Procedural { public override string getName() { return "C"; } } }

LangCPP.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design2 { class LangCPP : Heterogeneous { public override string getName() { return "Java"; } }

LangCSharp.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design2 { class LangCSharp:ObjectOriented { public override string getName() { return "CSharp"; } } }

LangJava.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design2 { class LangJava:ObjectOriented { public override string getName() { return "Java"; } }

ObjectOriented.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design2 { abstract class ObjectOriented:ProgrammingLanguageInterface { public string getUnit() { return "Class"; }

public string getParadigm() { return "ObjectOriented"; }

abstract public string getName(); //{ // //} } } throw new NotImplementedException();

ObjectOrientedImplementation.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design2 { class ObjectOrientedImplementation:ObjectOriented { public override string getName() { return ""; } }

Procedural.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design2 { abstract class Procedural:ProgrammingLanguageInterface { public string getUnit() { return "Function"; }

public string getParadigm() { return "Procedural"; }

abstract public string getName(); //{ // //} } throw new NotImplementedException();

ProceduralImplementation.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design2 { class ProceduralImplementation:Procedural {

public override string getName() { return ""; } } }

Program.cs:
using System; using System.Collections.Generic; using System.Linq;

using System.Text;

namespace Design2 { class Program { static void Main(string[] args) { ProgrammingLanguageInterface i = null; char c; int choice; do {

Console.WriteLine("1:Java"); Console.WriteLine("2:C#"); Console.WriteLine("3:C"); Console.WriteLine("4:CPP"); Console.WriteLine("5:exit"); Console.WriteLine("Enter your Choice"); choice = int.Parse(Console.ReadLine());

switch (choice) { case 1: i = new LangJava(); break; case 2: i = new LangCSharp(); break;

case 3: i = new LangC(); break; case 4: i = new LangCPP(); break; case 5: return; }; Console.WriteLine("Unit :" + i.getUnit()); Console.WriteLine("Paradigm :" + i.getParadigm()); Console.WriteLine("Name :" + i.getName());

Console.WriteLine("Press y to continue"); c = char.Parse(Console.ReadLine()); } while (c == 'y'); } } }

ProgrammingLanguageInterface.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design2 { interface ProgrammingLanguageInterface {

string getUnit(); string getParadigm(); string getName(); } }

Design4:
Heterogeneous.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design3 { abstract class Heterogeneous:ProgrammingLanguageInterface { class ProceduralImplementation : Procedural//inner class (private) { public override string getName() { return ""; } }

class ObjectOrientedImplementation : ObjectOriented

{ public override string getName() { return ""; } }

Procedural p ; ObjectOriented o ; public Heterogeneous() { p = new ProceduralImplementation(); o = new ObjectOrientedImplementation(); } public string getUnit() { string unit = p.getUnit() + " and " + o.getUnit(); return unit; }

public string getParadigm() { string paradigm; paradigm = p.getParadigm() + " and " + o.getParadigm(); return paradigm; }

abstract public string getName();

} }

LangC.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design3 { class LangC : Procedural { public override string getName() { return "C"; } } }

LangCPP.cs:
using System; using System.Collections.Generic; using System.Linq;

using System.Text;

namespace Design3 { class LangCPP:Heterogeneous { public override string getName() { return "Java"; } } }

LangCSharp.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design3 { class LangCSharp:ObjectOriented { public override string getName() { return "CSharp"; }

} }

LangJava.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design3 { class LangJava:ObjectOriented { public override string getName() { return "Java"; } }

ObjectOriented.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design3 { abstract class ObjectOriented:ProgrammingLanguageInterface { public string getUnit() { return "Class"; }

public string getParadigm() { return "ObjectOriented"; }

abstract public string getName(); //{ // //} } } throw new NotImplementedException();

Procedural.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design3 { abstract class Procedural:ProgrammingLanguageInterface { public string getUnit() { return "Function"; }

public string getParadigm() { return "Procedural"; }

abstract public string getName(); //{ // //} } } throw new NotImplementedException();

Program.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design3 { class Program { static void Main(string[] args) { ProgrammingLanguageInterface i = null; char c; int choice; do {

Console.WriteLine("1:Java"); Console.WriteLine("2:C#"); Console.WriteLine("3:C"); Console.WriteLine("4:CPP"); Console.WriteLine("5:exit"); Console.WriteLine("Enter your Choice"); choice = int.Parse(Console.ReadLine());

switch (choice) { case 1: i = new LangJava(); break; case 2: i = new LangCSharp(); break; case 3: i = new LangC();

break; case 4: i = new LangCPP(); break; case 5: return; }; Console.WriteLine("Unit :" + i.getUnit()); Console.WriteLine("Paradigm :" + i.getParadigm()); Console.WriteLine("Name :" + i.getName());

Console.WriteLine("Press y to continue"); c = char.Parse(Console.ReadLine()); } while (c == 'y'); } } }

ProgrammingLanguageInterface.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design3 { interface ProgrammingLanguageInterface { string getUnit();

string getParadigm(); string getName(); } }

Design5:
Heterogeneous.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design4 { abstract class Heterogeneous:ProgrammingLanguageInterface { Procedural p ; ObjectOriented o ; public Heterogeneous() { p = new Procedural(); o = new ObjectOriented(); } public string getUnit() { string unit = p.getUnit() + " and " + o.getUnit(); return unit;

public string getParadigm() { string paradigm; paradigm = p.getParadigm() + " and " + o.getParadigm(); return paradigm; }

abstract public string getName();

} }

LangC.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design4 { class LangC : ProgrammingLanguageInterface { public string getName() { return "C";

Procedural p = new Procedural(); public string getUnit() {

return p.getUnit(); }

public string getParadigm() { return p.getParadigm(); } } }

LangCPP.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design4 { class LangCPP:Heterogeneous { public override string getName()

{ return "Language CPP"; } } }

LangCSharp.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design4 { class LangCSharp:ProgrammingLanguageInterface { ObjectOriented o = new ObjectOriented(); public string getUnit() { return o.getUnit(); }

public string getParadigm() { return o.getParadigm(); }

public string getName() { return "CSharp"; } } }

LangJava.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design4 { class LangJava:ProgrammingLanguageInterface { ObjectOriented o = new ObjectOriented(); public string getName() { return "Java"; }

public string getUnit() { return o.getUnit(); }

public string getParadigm() { return o.getParadigm(); } }

ObjectOriented.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design4 { class ObjectOriented { public string getUnit() { return "Class"; }

public string getParadigm() { return "ObjectOriented";

} }

Procedural.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design4 { class Procedural { public string getUnit() { return "Function"; }

public string getParadigm() { return "Procedural"; }

} }

Program.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design4 { class Program { static void Main(string[] args) { ProgrammingLanguageInterface i = null; char c; int choice; do {

Console.WriteLine("1:Java"); Console.WriteLine("2:C#"); Console.WriteLine("3:C"); Console.WriteLine("4:CPP"); Console.WriteLine("5:Exit"); Console.WriteLine("Enter your Choice");

choice = int.Parse(Console.ReadLine());

switch (choice) { case 1: i = new LangJava(); break; case 2: i = new LangCSharp(); break; case 3: i = new LangC(); break; case 4: i = new LangCPP(); break; case 5: return; }; Console.WriteLine("Unit :" + i.getUnit()); Console.WriteLine("Paradigm :" + i.getParadigm()); Console.WriteLine("Name :" + i.getName());

Console.WriteLine("Press y to continue"); c = char.Parse(Console.ReadLine()); } while (c == 'y'); } } }

ProgrammingLanguageInterface.cs:
using System;

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

namespace Design4 { interface ProgrammingLanguageInterface { string getUnit(); string getParadigm(); string getName(); } }

Design6:
LangC.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design5 { class LangC : ProgrammingLanguage { public LangC()

{ p = new Procedural(); }

public override string getName() { return "C Language"; } } }

LangCPP.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design5 { class LangCPP:ProgrammingLanguage { public LangCPP() { o = new ObjectOriented(); p = new Procedural(); } public override string getName()

{ return "Language CPP"; } } }

LangCSharp.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design5 { class LangCSharp:ProgrammingLanguage { public LangCSharp() { o = new ObjectOriented(); }

public override string getName() { return "CSharp"; } } }

LangJava.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design5 { class LangJava:ProgrammingLanguage { public LangJava() { o = new ObjectOriented(); } public override string getName() { return "Java"; }

Notes.txt:

The drawback of this design is we are creating too many objects....

ObjectOriented.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design5 { class ObjectOriented { public string getUnit() { return "Class"; }

public string getParadigm() { return "ObjectOriented"; } } }

Procedural.cs:
using System; using System.Collections.Generic;

using System.Linq; using System.Text;

namespace Design5 { class Procedural { public string getUnit() { return "Function"; }

public string getParadigm() { return "Procedural"; } } }

Program.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design5 {

class Program { static void Main(string[] args) { ProgrammingLanguage pl = null; char c; int choice; do {

Console.WriteLine("1:Java"); Console.WriteLine("2:C#"); Console.WriteLine("3:C"); Console.WriteLine("4:CPP"); Console.WriteLine("5:Exit"); Console.WriteLine("Enter your Choice"); choice = int.Parse(Console.ReadLine());

switch (choice) { case 1: pl = new LangJava(); break; case 2: pl = new LangCSharp(); break; case 3: pl = new LangC(); break; case 4: pl = new LangCPP(); break;

case 5: return; }; Console.WriteLine("Unit :" + pl.getUnit()); Console.WriteLine("Paradigm :" + pl.getParadigm()); Console.WriteLine("Name :" + pl.getName());

Console.WriteLine("Press y to continue"); c = char.Parse(Console.ReadLine()); } while (c == 'y'); } } }

ProgrammingLanguage.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design5 { abstract class ProgrammingLanguage { public ObjectOriented o = null;//new ObjectOriented(); public Procedural p = null;//new Procedural(); public string getUnit() {

string unit=null; if (o != null) unit = o.getUnit(); if (p != null) unit += p.getUnit(); return unit; }

public string getParadigm() { string paradigm = null; if (o != null) paradigm = o.getParadigm(); if (p != null) paradigm += p.getParadigm(); return paradigm; }

abstract public string getName(); } }

Design7:
LangC.cs:
using System; using System.Collections.Generic;

using System.Linq; using System.Text;

namespace Design6 { class LangC : ProgrammingLanguage { public override string getName() { return "C Language"; } } }

LangCPP.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design6 { class LangCPP:ProgrammingLanguage { public override string getName() { return "Language CPP";

} } }

LangCSharp.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design6 { class LangCSharp:ProgrammingLanguage { public override string getName() { return "CSharp"; } } }

LangJava.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design6 { class LangJava:ProgrammingLanguage { public override string getName() { return "Java"; }

Methodology.cs:
using System; using System.Collections; using System.Linq; using System.Text;

namespace Design6 { class Methodology { public static IList Objects = new ArrayList(); static Methodology() {

Objects.Add( new Procedural()); Objects.Add(new ObjectOriented()); } public IList getList() { return Objects; } } }

Notes.txt:
we are using two references (drawback in the case of memory management). The drawback of this design is conditional Delegation(checking for condition and then performing the action) in the class ProgrammingLanguage....If there are more types rather than just Procedure and ObjectOriented then usin if else is not feasible. We are not using generic programming in this.

ObjectOriented.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design6 { class ObjectOriented { public string getUnit()

{ return "Class"; }

public string getParadigm() { return "ObjectOriented"; } } }

Procedural.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design6 { class Procedural { public string getUnit() { return "Function"; }

public string getParadigm()

{ return "Procedural"; } } }

Program.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design6 { class Program { static void Main(string[] args) { ProgrammingLanguage pl = null; char c; int choice; do {

Console.WriteLine("1:Java"); Console.WriteLine("2:C#"); Console.WriteLine("3:C");

Console.WriteLine("4:CPP"); Console.WriteLine("5:Exit"); Console.WriteLine("Enter your Choice"); choice = int.Parse(Console.ReadLine());

switch (choice) { case 1: pl = new LangJava(); break; case 2: pl = new LangCSharp(); break; case 3: pl = new LangC(); break; case 4: pl = new LangCPP(); break; case 5: return; }; Console.WriteLine("Unit :" + pl.getUnit()); Console.WriteLine("Paradigm :" + pl.getParadigm()); Console.WriteLine("Name :" + pl.getName());

Console.WriteLine("Press y to continue"); c = char.Parse(Console.ReadLine()); } while (c == 'y'); } } }

ProgrammingLanguage.cs:
using System; using System.Collections; using System.Linq; using System.Text;

namespace Design6 { abstract class ProgrammingLanguage { public string getUnit() { string unit=null; Procedural p=null; ObjectOriented o=null; if(GetType().Name.Equals("LangC"))// (getName().Equals("C Language")) { p = (Procedural)Methodology.Objects[0]; } else if ((GetType().Name.Equals("LangCSharp")) || (GetType().Name.Equals("LangJava"))) { o = (ObjectOriented)Methodology.Objects[1]; } else { p = (Procedural)Methodology.Objects[0]; o = (ObjectOriented)Methodology.Objects[1];

} if (o != null) unit = o.getUnit(); if (p != null) unit += p.getUnit(); return unit; }

public string getParadigm() { string paradigm = null; Procedural p = null; ObjectOriented o = null; if (GetType().Name.Equals("LangC"))// (getName().Equals("C Language")) { p = (Procedural)Methodology.Objects[0]; } else if ((GetType().Name.Equals("LangCSharp")) || (GetType().Name.Equals("LangJava"))) { o = (ObjectOriented)Methodology.Objects[1]; } else { p = (Procedural)Methodology.Objects[0]; o = (ObjectOriented)Methodology.Objects[1]; } if (o != null)

paradigm = o.getParadigm(); if (p != null) paradigm += p.getParadigm(); return paradigm; }

abstract public string getName(); } }

Design8:
LangC.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design7 { class LangC : ProgrammingLanguage { public LangC() { pi = (Procedural)Methodology.Objects[0]; }

public override string getName() { return "C Language"; } } }

LangCPP.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design7 { class LangCPP:ProgrammingLanguage { ParadigmInterface p1; public LangCPP() { pi = (ObjectOriented)Methodology.Objects[1]; p1 = (Procedural)Methodology.Objects[0]; } public override string getUnit() { return pi.getUnit() + " and " + p1.getUnit(); }

public override string getParadigm() { return pi.getParadigm() + " and " + p1.getParadigm(); } public override string getName() { return "Language CPP"; } } }

LangCSharp.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design7 { class LangCSharp:ProgrammingLanguage { public LangCSharp() { pi = (ObjectOriented)Methodology.Objects[1]; } public override string getName() {

return "CSharp"; } } }

LangJava.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design7 { class LangJava:ProgrammingLanguage { public LangJava() { pi = (ObjectOriented)Methodology.Objects[1]; } public override string getName() { return "Java"; }

Methodology.cs:
using System; using System.Collections; using System.Linq; using System.Text;

namespace Design7 { class Methodology { public static IList Objects = new ArrayList(); static Methodology() { Objects.Add( new Procedural()); Objects.Add(new ObjectOriented()); } public IList getList() { return Objects; } } }

Notes.txt:
In this design only two objects are created and placed in a list.

Then we create only one reference in ProgrammngLanguage and used it every where.

what we have learnt in all these designs:

TightCoupling avoided Conditional Delegation to be avoided Refractory principlr reduce multilevel inheritance

ObjectOriented.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design7 { class ObjectOriented:ParadigmInterface { public string getParadigm() { return "ObjectOriented"; }

public string getUnit() { return "Class";

} } }

ParadigmInterface.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design7 { interface ParadigmInterface { string getParadigm(); string getUnit(); } }

Procedural.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design7 { class Procedural:ParadigmInterface { public string getParadigm() { return "Procedural"; }

public string getUnit() { return "Function"; } } }

Program.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace Design7 { class Program { static void Main(string[] args)

{ ProgrammingLanguage pl = null; char c; int choice; do {

Console.WriteLine("1:Java"); Console.WriteLine("2:C#"); Console.WriteLine("3:C"); Console.WriteLine("4:CPP"); Console.WriteLine("5:Exit"); Console.WriteLine("Enter your Choice"); choice = int.Parse(Console.ReadLine());

switch (choice) { case 1: pl = new LangJava(); break; case 2: pl = new LangCSharp(); break; case 3: pl = new LangC(); break; case 4: pl = new LangCPP(); break; case 5: return; }; Console.WriteLine("Unit :" + pl.getUnit());

Console.WriteLine("Paradigm :" + pl.getParadigm()); Console.WriteLine("Name :" + pl.getName());

Console.WriteLine("Press y to continue"); c = char.Parse(Console.ReadLine()); } while (c == 'y'); } } }

ProgrammingLanguage.cs:
using System; using System.Collections; using System.Linq; using System.Text;

namespace Design7 { abstract class ProgrammingLanguage { protected ParadigmInterface pi = null; public virtual string getUnit() { string unit = ""; unit = pi.getUnit(); return unit; }

public virtual string getParadigm() { return pi.getParadigm(); }

abstract public string getName(); } }

Вам также может понравиться