R
RSH
Hi,
I have been reading on interfaces working on samples I've run across on the
web. For the life of me I cannot seem to grasp them.
It appears to me that interfaces are simply blueprints to a class, that when
implemented, they require the implementing calss to make sure that each of
the properties, functions etc. are handled by the class. (????)
What I am really having a problem with is how they overcome the limitation
of multiple inheritance? It would appear that interfaces simply require that
a class handles a predetermined number of properties events etc. So if I
have a class that implements an interface and I need to add functionality to
that class...I still have to add it to the interface and handle it in the
initial class...this seems redundant to me. Not to mention modifying the
interface to include the new required functionality, now makes all other
classes that implement the interface "break" and now require that I modify
all classes that implement the interface to handle the newly added
functionality.
Now I openly admit I am no OOP guru, and I am simply trying to understand
interfaces...I am certainly not bashing them.
The code below illustrates my initial attempt at understanding
interfaces...pieced together from a few tutorials on the web.
The classes and intefaces apper below. and the instantiation of the objects
appears at the top...and will probably shed light on my lack of
understanding of interfaces.
The first instantiation illustrates the use of the interfaces:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
IPencil p = new Pencil();
p.Type = "Pencil";
IPencil mp = new MechanicalPencil();
mp.Type = "MechanicalPencil";
IPencil pen = new Pen();
pen.Type = "Pen";
PencilSharpener sharpener = new PencilSharpener();
sharpener.Sharpen(p);
sharpener.Sharpen(mp);
sharpener.Sharpen(pen);
}
Now where I get confused is that I can quite simply get the same results by
bypassing the interfaces and never calling the PencilSharpener class:
protected void Page_Load(object sender, EventArgs e)
{
IPencil p = new Pencil();
p.Type = "Pencil";
p.OnSharpened();
IPencil mp = new MechanicalPencil();
mp.Type = "MechanicalPencil";
IPencil pen = new Pen();
pen.Type = "Pen";
//PencilSharpener sharpener = new PencilSharpener();
//sharpener.Sharpen(p);
//sharpener.Sharpen(mp);
//sharpener.Sharpen(pen);
}
So this is where my paridigm needs some shifting
Can you please help shed some light on this? Thank you very much!
Ron
// Interfaces +++++++++++++++++++++++++++++++++
//Pencil Interface
public interface IPencil
{
string Type { get; set; }
int CurrentSharpness { get; set; }
bool IsSharp { get; }
void Write();
void OnSharpened();
}
//Pencil Sharpener Interface
public interface IPencilSharpener
{
void Sharpen(IPencil pencil);
}
// Classes +++++++++++++++++++++++++++++++++++++
// Pencil Class
public class Pencil : IPencil
{
private string m_message;
private string m_sharpenedMessage;
private string m_type;
private int m_currentSharpness;
private int m_charsUsed;
private Boolean m_Sharpened = false;
// Constructor
public Pencil()
{
m_type = string.Empty;
m_currentSharpness = 0;
m_message = string.Empty;
m_sharpenedMessage = string.Empty;
m_charsUsed = 0;
}
// Property
public string Message
{
get { return m_message; }
}
// Property
public string SharpenedMessage
{
get { return m_sharpenedMessage; }
}
// Property
public string Type
{
get { return m_type; }
set {
m_type = value;
m_message = "This is my " + m_type + " writing away!";
m_sharpenedMessage = "This is one sharp " + m_type + "!";
}
}
// Property
public int CurrentSharpness
{
get { return m_currentSharpness; }
set { m_currentSharpness = value; }
}
// Property
public bool IsSharp
{
get { return m_charsUsed <= m_currentSharpness; }
}
// Function
public void Write()
{
foreach (char c in m_message)
{
if (m_Sharpened == false)
{
if (IsSharp)
{
HttpContext.Current.Response.Write(c);
}
else
{
HttpContext.Current.Response.Write("#");
}
}
else
{
HttpContext.Current.Response.Write(c);
}
m_charsUsed++;
}
HttpContext.Current.Response.Write("<BR>");
}
// Function
public void OnSharpened()
{
while(this.m_currentSharpness < m_message.Length)
{
m_charsUsed = 0;
m_currentSharpness++;
Write();
}
m_Sharpened = true;
m_message = m_sharpenedMessage;
Write();
}
public void Sharpen(IPencil pencil)
{
pencil.OnSharpened();
}
}
// Pencil Sharpener Class
public class PencilSharpener : IPencilSharpener
{
public void Sharpen(IPencil pencil)
{
HttpContext.Current.Response.Write("<br>Begin sharpening " + pencil.Type +
"...<br>");
pencil.OnSharpened();
}
}
// Pen Class
class Pen : IPencil
{
private string m_message;
private string m_sharpenedMessage;
private string m_type;
private int m_currentSharpness;
private int m_charsUsed;
private Boolean m_Sharpened = false;
// Constructor
public Pen()
{
m_type = string.Empty;
m_currentSharpness = 0;
m_message = string.Empty;
m_sharpenedMessage = string.Empty;
m_charsUsed = 0;
}
// Property
public string Message
{
get { return m_message; }
}
// Property
public string SharpenedMessage
{
get { return m_sharpenedMessage; }
}
// Property
public string Type
{
get { return m_type; }
set { m_type = value; }
}
// Property
public int CurrentSharpness
{
get { return m_currentSharpness; }
set { m_currentSharpness = 0; }
}
// Property
public bool IsSharp
{
get { return m_charsUsed <= m_currentSharpness; }
}
public void Write()
{
HttpContext.Current.Response.Write(m_message + "<BR>");
}
// Function
public void OnSharpened()
{
m_Sharpened = true;
m_message = "A Pen cannot be sharpened!.";
Write();
}
}
// Mechanical Pencil Class
class MechanicalPencil : IPencil, IPencilSharpener
{
private string m_message;
private string m_sharpenedMessage;
private string m_type;
private int m_currentSharpness;
private int m_charsUsed;
private Boolean m_Sharpened = false;
// Constructor
public MechanicalPencil()
{
m_type = string.Empty;
m_currentSharpness = 0;
m_message = string.Empty;
m_sharpenedMessage = string.Empty;
m_charsUsed = 0;
}
// Property
public string Message
{
get { return m_message; }
}
// Property
public string SharpenedMessage
{
get { return m_sharpenedMessage; }
}
// Property
public string Type
{
get { return m_type; }
set { m_type = value; }
}
// Property
public int CurrentSharpness
{
get { return m_currentSharpness; }
set { m_currentSharpness = value; }
}
// Property
public bool IsSharp
{
get { return m_charsUsed <= m_currentSharpness; }
}
// Function
public void Write()
{
foreach (char c in m_message)
{
if (m_Sharpened == false)
{
if (IsSharp)
{
HttpContext.Current.Response.Write(c);
}
else
{
HttpContext.Current.Response.Write("#");
}
}
else
{
HttpContext.Current.Response.Write(c);
}
m_charsUsed++;
}
HttpContext.Current.Response.Write("<BR>");
}
// Function
public void OnSharpened()
{
m_Sharpened = true;
m_message = "The Mechanical Pencil is self sharpening.";
Write();
}
public void Sharpen(IPencil pencil)
{
}
}
}
I have been reading on interfaces working on samples I've run across on the
web. For the life of me I cannot seem to grasp them.
It appears to me that interfaces are simply blueprints to a class, that when
implemented, they require the implementing calss to make sure that each of
the properties, functions etc. are handled by the class. (????)
What I am really having a problem with is how they overcome the limitation
of multiple inheritance? It would appear that interfaces simply require that
a class handles a predetermined number of properties events etc. So if I
have a class that implements an interface and I need to add functionality to
that class...I still have to add it to the interface and handle it in the
initial class...this seems redundant to me. Not to mention modifying the
interface to include the new required functionality, now makes all other
classes that implement the interface "break" and now require that I modify
all classes that implement the interface to handle the newly added
functionality.
Now I openly admit I am no OOP guru, and I am simply trying to understand
interfaces...I am certainly not bashing them.
The code below illustrates my initial attempt at understanding
interfaces...pieced together from a few tutorials on the web.
The classes and intefaces apper below. and the instantiation of the objects
appears at the top...and will probably shed light on my lack of
understanding of interfaces.
The first instantiation illustrates the use of the interfaces:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
IPencil p = new Pencil();
p.Type = "Pencil";
IPencil mp = new MechanicalPencil();
mp.Type = "MechanicalPencil";
IPencil pen = new Pen();
pen.Type = "Pen";
PencilSharpener sharpener = new PencilSharpener();
sharpener.Sharpen(p);
sharpener.Sharpen(mp);
sharpener.Sharpen(pen);
}
Now where I get confused is that I can quite simply get the same results by
bypassing the interfaces and never calling the PencilSharpener class:
protected void Page_Load(object sender, EventArgs e)
{
IPencil p = new Pencil();
p.Type = "Pencil";
p.OnSharpened();
IPencil mp = new MechanicalPencil();
mp.Type = "MechanicalPencil";
IPencil pen = new Pen();
pen.Type = "Pen";
//PencilSharpener sharpener = new PencilSharpener();
//sharpener.Sharpen(p);
//sharpener.Sharpen(mp);
//sharpener.Sharpen(pen);
}
So this is where my paridigm needs some shifting
Can you please help shed some light on this? Thank you very much!
Ron
// Interfaces +++++++++++++++++++++++++++++++++
//Pencil Interface
public interface IPencil
{
string Type { get; set; }
int CurrentSharpness { get; set; }
bool IsSharp { get; }
void Write();
void OnSharpened();
}
//Pencil Sharpener Interface
public interface IPencilSharpener
{
void Sharpen(IPencil pencil);
}
// Classes +++++++++++++++++++++++++++++++++++++
// Pencil Class
public class Pencil : IPencil
{
private string m_message;
private string m_sharpenedMessage;
private string m_type;
private int m_currentSharpness;
private int m_charsUsed;
private Boolean m_Sharpened = false;
// Constructor
public Pencil()
{
m_type = string.Empty;
m_currentSharpness = 0;
m_message = string.Empty;
m_sharpenedMessage = string.Empty;
m_charsUsed = 0;
}
// Property
public string Message
{
get { return m_message; }
}
// Property
public string SharpenedMessage
{
get { return m_sharpenedMessage; }
}
// Property
public string Type
{
get { return m_type; }
set {
m_type = value;
m_message = "This is my " + m_type + " writing away!";
m_sharpenedMessage = "This is one sharp " + m_type + "!";
}
}
// Property
public int CurrentSharpness
{
get { return m_currentSharpness; }
set { m_currentSharpness = value; }
}
// Property
public bool IsSharp
{
get { return m_charsUsed <= m_currentSharpness; }
}
// Function
public void Write()
{
foreach (char c in m_message)
{
if (m_Sharpened == false)
{
if (IsSharp)
{
HttpContext.Current.Response.Write(c);
}
else
{
HttpContext.Current.Response.Write("#");
}
}
else
{
HttpContext.Current.Response.Write(c);
}
m_charsUsed++;
}
HttpContext.Current.Response.Write("<BR>");
}
// Function
public void OnSharpened()
{
while(this.m_currentSharpness < m_message.Length)
{
m_charsUsed = 0;
m_currentSharpness++;
Write();
}
m_Sharpened = true;
m_message = m_sharpenedMessage;
Write();
}
public void Sharpen(IPencil pencil)
{
pencil.OnSharpened();
}
}
// Pencil Sharpener Class
public class PencilSharpener : IPencilSharpener
{
public void Sharpen(IPencil pencil)
{
HttpContext.Current.Response.Write("<br>Begin sharpening " + pencil.Type +
"...<br>");
pencil.OnSharpened();
}
}
// Pen Class
class Pen : IPencil
{
private string m_message;
private string m_sharpenedMessage;
private string m_type;
private int m_currentSharpness;
private int m_charsUsed;
private Boolean m_Sharpened = false;
// Constructor
public Pen()
{
m_type = string.Empty;
m_currentSharpness = 0;
m_message = string.Empty;
m_sharpenedMessage = string.Empty;
m_charsUsed = 0;
}
// Property
public string Message
{
get { return m_message; }
}
// Property
public string SharpenedMessage
{
get { return m_sharpenedMessage; }
}
// Property
public string Type
{
get { return m_type; }
set { m_type = value; }
}
// Property
public int CurrentSharpness
{
get { return m_currentSharpness; }
set { m_currentSharpness = 0; }
}
// Property
public bool IsSharp
{
get { return m_charsUsed <= m_currentSharpness; }
}
public void Write()
{
HttpContext.Current.Response.Write(m_message + "<BR>");
}
// Function
public void OnSharpened()
{
m_Sharpened = true;
m_message = "A Pen cannot be sharpened!.";
Write();
}
}
// Mechanical Pencil Class
class MechanicalPencil : IPencil, IPencilSharpener
{
private string m_message;
private string m_sharpenedMessage;
private string m_type;
private int m_currentSharpness;
private int m_charsUsed;
private Boolean m_Sharpened = false;
// Constructor
public MechanicalPencil()
{
m_type = string.Empty;
m_currentSharpness = 0;
m_message = string.Empty;
m_sharpenedMessage = string.Empty;
m_charsUsed = 0;
}
// Property
public string Message
{
get { return m_message; }
}
// Property
public string SharpenedMessage
{
get { return m_sharpenedMessage; }
}
// Property
public string Type
{
get { return m_type; }
set { m_type = value; }
}
// Property
public int CurrentSharpness
{
get { return m_currentSharpness; }
set { m_currentSharpness = value; }
}
// Property
public bool IsSharp
{
get { return m_charsUsed <= m_currentSharpness; }
}
// Function
public void Write()
{
foreach (char c in m_message)
{
if (m_Sharpened == false)
{
if (IsSharp)
{
HttpContext.Current.Response.Write(c);
}
else
{
HttpContext.Current.Response.Write("#");
}
}
else
{
HttpContext.Current.Response.Write(c);
}
m_charsUsed++;
}
HttpContext.Current.Response.Write("<BR>");
}
// Function
public void OnSharpened()
{
m_Sharpened = true;
m_message = "The Mechanical Pencil is self sharpening.";
Write();
}
public void Sharpen(IPencil pencil)
{
}
}
}