Link to home
Create AccountLog in
Avatar of mathieu_cupryk
mathieu_cuprykFlag for Canada

asked on

Interface Alogrithm understanding the design?

I have the following attached
what is the purppose of this dsign?

Can someone help me write a little program using this interface.

Thanks.
public partial class _Default : System.Web.UI.Page 
{
    interface Algorithm
    {
        void calculate();
    }
 
    abstract class ComplexAlgorithm
    {
        protected ComplexAlgorithm(Algorithm a)
        {
            phase1_ = a;
        }
 
        public void calculate()
        {
            phase1_.calculate();
            runPhase2();
        }
        abstract protected void runPhase2();
        private Algorithm phase1_;
    }
 
 
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
}

Open in new window

SOLUTION
Avatar of nsanga
nsanga

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Seems to allow you 2 classes to do some calculations.
1 being a concreate class that derives from  ComplexAlgorithm
the other being a class that implements Algorithm
eg


public class MyAlgorithm : Algorithm
{
        public void Algorith.calculate()
          {
//Do something here???
}
}
public class MyClass : ComplexAlgorithm
{
void MyClass() : base(new MyAlgorithm()){}
protected override runPhase2()
{
//do something else here??
}
}
 
protected void Page_Load(object sender, EventArgs e)
    {
 MyClass a = new MyClass();
a.calculate();
    }

Open in new window

Avatar of mathieu_cupryk

ASKER

Hi I get an error:

namespace ConsoleApplication1
{

    class MyAlgorithm : Algorithm
    {
        static void Main(string[] args)
        {
            System.Console.WriteLine("Hello Interfaces");
            Demo refDemo = new Demo();
        }

        public void Algorith.calculate()
        {
            //Do something here
        }
     
    }


    interface Algorithm
    {

        void calculate();
    }

    public class MyClass : ComplexAlgorithm
    {
        protected override runPhase2()
        {                               ---------------
              //do something else here??
        }
    }
   
    abstract class ComplexAlgorithm
    {
        protected ComplexAlgorithm(Algorithm a)
        {
            phase1_ = a;
        }

        public void calculate()
        {
            phase1_.calculate();
            runPhase2();
        }
        abstract protected void runPhase2();
        private Algorithm phase1_;
    }

}
       
 
protected void Page_Load(object sender, EventArgs e)
{             -------
     MyClass a = new MyClass();
                                  -----------
     a.calculate();
}
I have an error:

public class MyClass : ComplexAlgorithm
    {
        protected override runPhase2()
        {                              ------------
              //do something else here??
        }
    }

using System;
using System.Collections.Generic;
using System.Text;
 
namespace ConsoleApplication2
{
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
 
using System;
using System.Collections.Generic;
using System.Text;
 
namespace ConsoleApplication1
{
 
    class MyAlgorithm : Algorithm
    {
        static void Main(string[] args)
        {
            System.Console.WriteLine("Hello Interfaces");
            MyClass a = new MyClass();
            a.calculate();
        }
 
        public void Algorith.calculate()
        {
            
        }
      
    }
 
 
    interface Algorithm
    {
 
        void calculate();
    }
 
    public class MyClass : ComplexAlgorithm
    {
        protected override runPhase2()
        {
              //do something else here??
        }
    }
    
    abstract class ComplexAlgorithm
    {
        protected ComplexAlgorithm(Algorithm a)
        {
            phase1_ = a;
        }
 
        public void calculate()
        {
            phase1_.calculate();
            runPhase2();
        }
        abstract protected void runPhase2();
        private Algorithm phase1_;
    }
 
}

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Great work guys