OOP Class11

esse quam videri
Revision as of 16:31, 10 June 2019 by Janell (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search


Duck Study

Private instance data members - accessors

Microsoft has stared calling private variables with accessors Properties

Private class memebers the use get and set keyword to set and retrieve data. Get and set are known as accessor methods private members are helpful when you want to also do other things when a data member is changed or change the rutern value under certain conditions. C# Programmer's Reference - Accessors http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfaccessorspg.asp. Lastly accessor also make read only and write only variables possible

//private string color read/write
private string color;

public string Color
{
  get
  {
   return color;
  }
  set
  {
   color = value;
  }
}

//private string color read only
private string color;

public string Color
{
  get
  {
   return color;
  }
}


Another property that is a good candidate for a private instance data member is the dogs age

private string age;

public int Age
{
  //age can only be accessed with get there is no set accessor
  //age must be set with HappyBirthday()
  get
  {
   return age;
  }
}

public int HappyBirthday()
{
  age++;
  return age;
}



Problems with inheritance

Rocket Ducks!!!!

RocketDuckCapture1.PNG

public class Duck
    {
        public string DuckSound;
       

        public string Fly()
        {
            return string.Format("(0) us flying", this);
        }

        public string Quack()
        {
            return this.DuckSound;
        }
    }
public class Rocket
    {
        public bool IsIgnited;
    
        public string Fly()
        {
            return string.Format("(0) us flying", this);
        }

        public void Launch()
        {
            this.IsIgnited = true;
        }
    }

Notice both have a Fly() method.

I want a RocketDuck. One that can fly with wings or rockets. I could try inheritance...

RocketDuckCapture2.PNG

public class Duck : Rocket
    {
        public string DuckSound;
...

Now I get a warning

Warning	1	'FlyingDucks.Duck.Fly()' hides inherited member 'FlyingDucks.Rocket.Fly()'
This doesn't work which fly does it call. C# hides the Rocket fly.

RocketDuckCapture3Nope.PNG

Neither does this

RocketDuckCapture4Nope.PNG

What we need is multiple inheritance or Interfaces. C# doesn't have multiple inheritance so let's try interfaces. C++ does have multiple inheritance but I'll save this for another class.

Interfaces

IFlyable

interface IFlyable
    {
        int CurrentAltitude { get; set; }
        bool IsFyling { get; set; }

        void FlyUP();
        void FlyDown();
        void Fly();
    }

Implementation

class Helicopter : IFlyable
    {

        protected int currentAltitude;  //Private instance data member
        public bool IsFyling { get; set; }

        public Helicopter()
        {
            this.CurrentAltitude = 0;
        }

        public int CurrentAltitude  //Accessor 
        {
            get
            {
                return currentAltitude;
            }
            set
            {
                currentAltitude = value;
            }
        }

        public void FlyUP()
        {
            this.CurrentAltitude++;
        }

        public void FlyDown()
        {
            this.CurrentAltitude--;
        }


        public void Fly()
        {
            this.IsFyling = true;
        } 
    }

Bird is also IFlyable

public class Bird : IFlyable

A Duck is a Bird

public class Duck : Bird

And a RocketDuck is a Duck that is also IRocketable

public class RocketDuck : Duck, IRocketable, IQuackable


RocketDuckWithInterfacesPNG.PNG

Interfaces allow for Multiple Inheritance so a RockerDuck is a Duck that implements Flyable and Quackable

Phone interface

Look as phone project. Discuss polymorphic phones interchangeability. Look for possible interfaces to extract.

Strategy Pattern

http://www.dofactory.com/Patterns/PatternStrategy.aspx

Strategy Pattern

Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

Ducks From Book

http://iam.colum.edu/oop/browser/browser.aspx?f=/classsource/class11/Duck

http://www.dofactory.com/Patterns/PatternStrategy.aspx

Pretty good example of the strategey method in action.

Characters

Phones

http://iam.colum.edu/oop/classsource/class12/ConsoleApplicationPhoneNoInterface.zip

Review Homework

Problems


Homework

Extract interfaces for phone class. Make a web interface for the phone classes.

You should be able to choose an phone type and then dial it and send messages if it is ITxtable.

http://iam.colum.edu/oop/classsource/class12/ConsoleApplicationPhoneNoInterface.zip