All,
I'm struggling with an OOP concept. Let me try to define what I'm wanting by
using some classic examples.
Let's say that I have classes called Square & Circle that derive from class
Shape. Within class Shape, it has a method called Save to save the object to
disk. It's defined in Shape because the logic to save the shapes is uniform
across all shapes.
Furthermore, let's say I've defined an interface called IDraw that includes
a method called Draw.
Now, what I want to be able to do is use a factory pattern to produce Shape
objects. Something like:
Public Shape GetShape(int shapeID) {
if (shapeID==1)
return new Square;
else
return new Circle;
}
Obviously, that's oversimplified but hopefully you get the point. The idea
is that the factory will determine what type of Shape object to create. Now
I want to be able to call the Draw method on the Shape object as such:
Shape someShape=GetSh ape(shapeID);
someShape.Draw( ); // Defined in each class that derives from Shape.
someShape.Save( ); // Defined in Shape itself.
This is where I'm stumped. Unless I implement a stub Draw() method in the
Shape class, the compiler will complain that the Draw method doesn't exist.
I suppose if I implement a stub method, then at runtime the proper Draw
method would be called, right? But it seems kind of kludgy to have a Draw()
method implemented in Shape if in fact the classes that derive from Shape
will always have a Draw method and it's one of those methods that will
actually be called.
This makes me think that I'm just designing this wrong. How would I go
about designing this? Overall, I have a base class that can't be abstract
because it does have some common functionality that needs to be available to
all derived class. I have an interface that dictates what methods need to be
implemented. And I want to be able to call one of those methods on the base
class, although the base class isn't implementing the interface.
Confused.... 4 1512
Hi,
You are allowed to have non-abstract methods in an abstract base class that can
be used/shared by all the derived classes. As long as you won't be instatiating
any Shape objects directly (like new Shape()), you could make Shape abstract.
For example,
public interface IDraw
{
void Draw();
}
public abstract class Shape : IDraw
{
// make abstract so that derived classes have to implement
public abstract void Draw();
public virtual void Save()
{
// common save implementation available to all Shapes that can be
// overriden by derived classes if required
}
}
public class Circle : Shape
{
public override void Draw()
{
// circle's Draw implementation
}
}
Shape s1 = new Circle();
s1.Draw();
s1.Save();
Hope this helps...
--
Rodger
<http://www.SequenceDia gramEditor.com>
Sequence Diagram Editor - A quick and easy way to draw and edit sequence diagrams.
Rachel Devons wrote: All,
I'm struggling with an OOP concept. Let me try to define what I'm wanting by using some classic examples.
Let's say that I have classes called Square & Circle that derive from class Shape. Within class Shape, it has a method called Save to save the object to disk. It's defined in Shape because the logic to save the shapes is uniform across all shapes. Furthermore, let's say I've defined an interface called IDraw that includes a method called Draw.
Now, what I want to be able to do is use a factory pattern to produce Shape objects. Something like:
Public Shape GetShape(int shapeID) { if (shapeID==1) return new Square; else return new Circle; }
Obviously, that's oversimplified but hopefully you get the point. The idea is that the factory will determine what type of Shape object to create. Now I want to be able to call the Draw method on the Shape object as such:
Shape someShape=GetSh ape(shapeID); someShape.Draw( ); // Defined in each class that derives from Shape. someShape.Save( ); // Defined in Shape itself.
This is where I'm stumped. Unless I implement a stub Draw() method in the Shape class, the compiler will complain that the Draw method doesn't exist. I suppose if I implement a stub method, then at runtime the proper Draw method would be called, right? But it seems kind of kludgy to have a Draw() method implemented in Shape if in fact the classes that derive from Shape will always have a Draw method and it's one of those methods that will actually be called.
This makes me think that I'm just designing this wrong. How would I go about designing this? Overall, I have a base class that can't be abstract because it does have some common functionality that needs to be available to all derived class. I have an interface that dictates what methods need to be implemented. And I want to be able to call one of those methods on the base class, although the base class isn't implementing the interface.
Confused....
"Rachel Devons" <no*****@nonono n.com> wrote in message
news:Ov******** ******@TK2MSFTN GP14.phx.gbl... All,
I'm struggling with an OOP concept. Let me try to define what I'm wanting by using some classic examples.
Let's say that I have classes called Square & Circle that derive from class Shape. Within class Shape, it has a method called Save to save the object to disk. It's defined in Shape because the logic to save the shapes is uniform across all shapes. Furthermore, let's say I've defined an interface called IDraw that includes a method called Draw.
Now, what I want to be able to do is use a factory pattern to produce Shape objects. Something like:
Public Shape GetShape(int shapeID) { if (shapeID==1) return new Square; else return new Circle; }
Obviously, that's oversimplified but hopefully you get the point. The idea is that the factory will determine what type of Shape object to create. Now I want to be able to call the Draw method on the Shape object as such:
Shape someShape=GetSh ape(shapeID); someShape.Draw( ); // Defined in each class that derives from Shape. someShape.Save( ); // Defined in Shape itself.
This is where I'm stumped. Unless I implement a stub Draw() method in the Shape class, the compiler will complain that the Draw method doesn't exist. I suppose if I implement a stub method, then at runtime the proper Draw method would be called, right? But it seems kind of kludgy to have a Draw() method implemented in Shape if in fact the classes that derive from Shape will always have a Draw method and it's one of those methods that will actually be called.
That's why you mark Shape.Draw as abstract. Then Square must implement Draw
to be a concrete Shape.
This makes me think that I'm just designing this wrong. How would I go about designing this? Overall, I have a base class that can't be abstract because it does have some common functionality that needs to be available to all derived class.
Just because it's abstract doesn't mean it can't implement some methods. It
just means that it doesn't implement all its methods.
David
Thanks for all the input, everyone! This certainly gives me some ideas to
chew on.
"Rachel Devons" <no*****@nonono n.com> wrote in message
news:Ov******** ******@TK2MSFTN GP14.phx.gbl... All,
I'm struggling with an OOP concept. Let me try to define what I'm wanting by using some classic examples.
Let's say that I have classes called Square & Circle that derive from class Shape. Within class Shape, it has a method called Save to save the object to disk. It's defined in Shape because the logic to save the shapes is uniform across all shapes. Furthermore, let's say I've defined an interface called IDraw that includes a method called Draw.
Now, what I want to be able to do is use a factory pattern to produce Shape objects. Something like:
Public Shape GetShape(int shapeID) { if (shapeID==1) return new Square; else return new Circle; }
Obviously, that's oversimplified but hopefully you get the point. The idea is that the factory will determine what type of Shape object to create. Now I want to be able to call the Draw method on the Shape object as such:
Shape someShape=GetSh ape(shapeID); someShape.Draw( ); // Defined in each class that derives from Shape. someShape.Save( ); // Defined in Shape itself.
This is where I'm stumped. Unless I implement a stub Draw() method in the Shape class, the compiler will complain that the Draw method doesn't exist. I suppose if I implement a stub method, then at runtime the proper Draw method would be called, right? But it seems kind of kludgy to have a Draw() method implemented in Shape if in fact the classes that derive from Shape will always have a Draw method and it's one of those methods that will actually be called.
This makes me think that I'm just designing this wrong. How would I go about designing this? Overall, I have a base class that can't be abstract because it does have some common functionality that needs to be available to all derived class. I have an interface that dictates what methods need to be implemented. And I want to be able to call one of those methods on the base class, although the base class isn't implementing the interface.
Confused.... This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Tee |
last post by:
Hi,
I have a base usercontrol with a method (blank method, no code), I have
another few usercontrols that will inherit this base usercontrol, but I want
to force all the usercontrol that inheriting this base usercontrol to
override the method with its own code. How do I do it?
I have tried to make the base usercontrol an abstract class (mustinherits +
mustoverrides), this works, but all the usercontrols that inherit it will
not able...
|
by: Dan Sikorsky |
last post by:
If we were to define all abstract methods in an abstract class, thereby
making that class non-abstract, and then override the heretofore 'abstract'
methods in a derived class, wouldn't that remove the need to have abstract
class types in C#?
Derived classes from abstract base classes must overrided the abstract
method defininition anyway, so why not just provide a complete definition
for the abstract method when defining the containing...
|
by: thomson |
last post by:
Hi
I do have an Interface, which is implemented in lot of places, My
question is i observed that After quite some time if i need to add a
new method in the interface , i need to do a lot of rework in lot of
places , So i do see a flaw in my design, How best we can avoid this
situation
Thanks in Advance
thomson
|
by: _dee |
last post by:
Question about best use of interfaces:
Say there's a 'Master' class that needs to implement a few
interfaces:
class Master : I1, I2, I3
{
}
The actual code already exists in smaller classes that each
|
by: jason |
last post by:
In the microsoft starter kit Time Tracker application, the data access
layer code consist of three cs files.
DataAccessHelper.cs
DataAcess.cs
SQLDataAccessLayer.cs
DataAcccessHelper appears to be checking that the correct data type is
used
DataAcess sets an abstract class and methods
| |
by: __PPS__ |
last post by:
Hello all,
I was working on someone else's code to implement some classes. The
task seemed to be simple and I was confident that I won't spent alot of
time on that task... But! I've already spent more time to integrade
old working code with new design than it would take me to write it all
from scratch!
so... after having tons of headaches I desided to figure out what's
wrong with the design (or me) and why it's all so complicated.
|
by: David Zha0 |
last post by:
Hi,
"when we call a virtual method, the runtime will check the instance who
called the method and then choose the suitable override method, this may
causes the performance drop down", is this right?
And, why not use "new" instead of using "virtual"?
And the last question, what is the differences between a abstract method and
a interface?
|
by: =?Utf-8?B?UmljaA==?= |
last post by:
Greetings,
I am actually a VB.Net guy, but I have worked somewhat with C++ and C#. I
just want to ask about the relationship between Abstract Classes and
Interfaces.
My first question is if C# even has Iinterfaces. I took some Java
programming classes and Interfaces are a main staple of Java. And in VB.Net
I use interfaces for setting up definitions of classes. I am guessing that
Abstract classes in C# do the same thing as...
|
by: =?Utf-8?B?QmVu?= |
last post by:
Hi,
I have a couple of questions about the proper design of classes.
I'll use a simple Customer class for my question.
1) Lets say that I have this Customer class like I said, and I want to
distinguish
between different types of customers, for example private, business,
other etc.
This will allow me to filter customers based on their type. Should I
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| | |