473,769 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How broad should an interface be? Advice needed.

I have not heavily used interfaces in the past so I am looking for
advice. An interface may not be the best solution but it was the first
idea I had and was working at first.

Here is the situation, I have been asked to create an application that
will provide a "script" of things to say, yes/no questions to ask,
information to look up etc.

This could be used by multiple clients to I am creating a class library
to implement the functionality. I have created a class Script that
contains a collection of steps in the script and a property CurrentItem
that returns the current step in the script.

I originally thought I would create an interface iScriptItem, have each
type of item implement it, and make the CurrentItem property of Script
an iScriptItem.

iScriptItem contained three properties: ID, Text, and Type

So from the client, we could move through the items in the script like
this:

Dim myScript as New Script

Private Sub btnNext_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles btnNext.Click
myScript.MoveNe xt()
ConsoleWriteLin e(myScript.Curr entItem.Text)

End Sub
This works great. The problem is when I get to the point where my
script steps have different properties, for example,
I have a "Statement" step that displays text to the user. It has the
property "NextItem"
I also have a "Question" step that has two properties called "YesItem"
and "NoItem" and does not need a NextItem property

The problem is, in the code above, these properties are not available
because CurrentItem is defined an an iScriptItem.

So here is my question. Is it better to have the interface contain all
the properties that may be used in the different item types (even
though they won't all be used by all the types), leave the interface
simple and use CType to convert CurrentItem to the correct type of
object (based on the type property), or some better way to do it? Is
my design idea just way off?

Again, I don't have a lot of experience with interfaces so this may
have been the wrong way to go about this. I don't have a lot of code
written as I am really in the early design phase and it's mostly on my
white board :)

Nov 28 '06 #1
3 1090
This works great. The problem is when I get to the point where my
script steps have different properties, for example,
I have a "Statement" step that displays text to the user. It has the
property "NextItem"
I also have a "Question" step that has two properties called "YesItem"
and "NoItem" and does not need a NextItem property

The problem is, in the code above, these properties are not available
because CurrentItem is defined an an iScriptItem.

So here is my question. Is it better to have the interface contain all
the properties that may be used in the different item types (even
though they won't all be used by all the types), leave the interface
simple and use CType to convert CurrentItem to the correct type of
object (based on the type property), or some better way to do it? Is
my design idea just way off?
You seem to have 3 types of items: Scripts, Statements, and Questions.
So, define 3 interfaces: Scripts, Statements, and Questions.

Some of your objects might have one implemented, others might have two.

Try: Implements IScript, IStatement. The new TryCast statement might
be of use..

Interfaces are more useful between projects. This way you can have a
consumer of your logic pass in a custom object to be processed.

I do not see any gains from having unused properties on some objects.
Nov 28 '06 #2
Script has a property called CurrentItem. This property can contain
one of any type of item that can be in the script: Statement, Question,
etc. (I think there are 5 total)

What I was trying to do was make CurrentItem a "generic" type
iScriptItem that would return any of these types of items.

The ways I'm thinking of doing it are:
Have Statement, Question, etc implement iScriptItem OR
Have Statement, Question,, etc inherit from an abstract base class
ScriptItem
In either of these scenarios, the client code will need to convert the
CurrentItem property to a object of the correct type before accessing
the derived class' specific properties.

Are either of these approaches the "right" way or am I missing
something?

The other option I can think of is have only one type of object
ScriptItem that contains ALL the properties including a "Type" property
that indicates what this ScriptItem object represents

I guess what it boils down to is I have an element(a property in this
case but it could as easily be a function) that could return one of
multiple types of objects.
Robert wrote:
You seem to have 3 types of items: Scripts, Statements, and Questions.
So, define 3 interfaces: Scripts, Statements, and Questions.

Some of your objects might have one implemented, others might have two.

Try: Implements IScript, IStatement. The new TryCast statement might
be of use..

Interfaces are more useful between projects. This way you can have a
consumer of your logic pass in a custom object to be processed.

I do not see any gains from having unused properties on some objects.
Nov 28 '06 #3

er*********@usb ank.com wrote:
<snip>
Here is the situation, I have been asked to create an application that
will provide a "script" of things to say, yes/no questions to ask,
information to look up etc.
<snip>
I originally thought I would create an interface iScriptItem, have each
type of item implement it, and make the CurrentItem property of Script
an iScriptItem.

iScriptItem contained three properties: ID, Text, and Type

So from the client, we could move through the items in the script like
this:

Dim myScript as New Script

Private Sub btnNext_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles btnNext.Click
myScript.MoveNe xt()
ConsoleWriteLin e(myScript.Curr entItem.Text)

End Sub
This works great. The problem is when I get to the point where my
script steps have different properties, for example,
I have a "Statement" step that displays text to the user. It has the
property "NextItem"
I also have a "Question" step that has two properties called "YesItem"
and "NoItem" and does not need a NextItem property
<snip>

Instead of interface, I'd base the different items on a base class.
That's because the base class plays the role of stablishing a base type
*and also* providing a common ground functionality for things such as
the MoveNext method you refer in the snippet above. I'd take the
interface route only if planning to have completely disparate object
types to become script items. Even then, maybe I'd prefer a
"ScriptItemAdap ter" type (based on the base class) that would accept
implementors of a given interface.

Also, I guess you'll find it helpfull to have an enumeration for the
different types of ScriptItems. It will make it easier to select a
specific cast (instead of using TryCast for each different type):

<aircode>
Dim SKind As ScriptItemKind = ThisItem.Kind

Select Case SKind
Case ScriptItemKind. Statement
Dim S As Statement = CType(ThisItem, Statement)
...
Case ScriptItemKind. Question
Dim S As Question = CType(ThisItem, Question)
...
Case Else
'Oops, an unknown kind!?
'raise error or else
End Select
</aircode>

HTH,

Regards,

Branco.

Nov 28 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
1908
by: Pierre Barbier de Reuille | last post by:
Ok, I first want to stress that I looked through the newsgroups archives (on Google) for an answer to my question, but I didn't find it. It's about the interface of the set classes as defined in the PEP 218. I don't understand why the sets has an interface of mapping object without associated value and not an interface of sequence ? For me, sets are first sequences. I use set where lists are inefficient or inadapted, for example when I...
20
2272
by: Simon Harvey | last post by:
Festive greetings fellow programmers! I've been programming now for about 4, maybe 5 years now. 4 of those years were at university so and I havent had much work experience of making real world applications (although I trying to make some now). There is still a lot I don't know when it comes to making programs. I know all the theory, but not how (and why) certain things are done in real world projects. My current ponderings are about...
9
4651
by: Anon Email | last post by:
Hi people, I'm learning about header files in C++. The following is code from Bartosz Milewski: // Code const int maxStack = 16; class IStack
2
1790
by: Oenone | last post by:
I could use a little advice to help prevent me making a possible mess of a project. :) In VB6, I once created a project that exposed a public interface class. I then Implemented this in various plug-in DLLs so that I could early-bind to the plug-ins by declaring objects of the interface class type. This worked fine, until one day I found that I needed to add a new method to the interface class. Of course, everything broke immediately...
8
1480
by: khalprin | last post by:
Hello, I'm trying to create a component that will be used from .net clients and COM clients. I've got an object model that looks something like this: ISystem IRuntime IConfiguration ICollectionOfConfigurableThings IConfigurableThing
25
2497
by: lovecreatesbeauty | last post by:
Could you talk something about the general rules on the interface (function) design in C program that recognized publically? Or introduce some good advice of yourself. How do you think about some of those advices like following? a. keep the interface clean and clear (What does clean or clear mean and how to achieve that?). b. avoid using static variables in local function if possible. c. avoid using global variables for local function...
20
6087
by: Luc Kumps | last post by:
(Sorry about the previous post, it got transmitted before it was complete) We try to separate implementation and interface defintions, but we run into a problem. I hope the guru's can solve this, as we seem to lack only a single 'step' to have "full separation"... We have a first project, namespace Ninterface, that contains the interface definitions in class1_interface.cs, like this: namespace Ninterface { public interface IClass1{
1
1625
by: YellowfinTeam | last post by:
Laws and Myths of Usability & Interface Design Introduction Usability and relevance have been identified as the major factors preventing mass adoption of Business Intelligence applications. What we have today are traditional BI tools that don't work nearly as well as they should, even for analysts and power users. The reason they haven't
4
2223
by: =?iso-8859-1?B?S2VyZW0gR/xtcvxrY/w=?= | last post by:
Hi, i am looking for an example that shows how to implement a COM Interface (with events) and pass it to a Windows API Call via pointer. Since this is really new to me, i dont know where to start,... The Problem is still the EditSecurity Windows API call, that expects a pointer to a ISecurityInformation
0
9589
marktang
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...
0
9423
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,...
0
10045
jinu1996
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...
1
9994
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
tracyyun
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...
0
8870
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.