473,395 Members | 1,941 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Implementing a folder object?

I'm puzzling over the best design for a Folder object.

I have two basic domain objects; leat's call them an Apple and an Orange.
The objects are maintained in separate hierarchies, and each hierarchy is
organized by Folder objects. A Folder object can contain either Apples, or
Oranges, or other Folders. A Folder contains only one type of object. For
example, a Folder won't contain Apples and other Folders, or Apples and
Oranges.

What's the best design for the Folder object? I was going to use a GoF
Composite pattern, but it doesn't really seem to fit, since a Folder is
neither an Apple nor an Orange. At this point, I'm leaning toward an
abstract Folder, from which I derive an AppleFolder and an OrangeFolder. But
I'm still puzzling over how clients tell whether a Folder contains other
Folders or fruit objects.

Any advice would be greatly appreciated. Thanks in advance.

--
Dave Veeneman
Chicago
Nov 16 '05 #1
4 3087
I think the abstract folder method is the way to go. You cold provide an
enum on the abstract folder that would return the type of the derived
folder, but that would mean having to update the enum if you add a new
folder type. Not a big deal, but to me it is counter to clear coding.

Presumably the client code will be familiar with the types of folders it
knows how to process and you could just use the 'as' or 'is' operator to
test the type.

Colin

"Dave Veeneman" <da****@nospam.com> wrote in message
news:Of**************@TK2MSFTNGP09.phx.gbl...
I'm puzzling over the best design for a Folder object.

I have two basic domain objects; leat's call them an Apple and an Orange.
The objects are maintained in separate hierarchies, and each hierarchy is
organized by Folder objects. A Folder object can contain either Apples, or
Oranges, or other Folders. A Folder contains only one type of object. For
example, a Folder won't contain Apples and other Folders, or Apples and
Oranges.

What's the best design for the Folder object? I was going to use a GoF
Composite pattern, but it doesn't really seem to fit, since a Folder is
neither an Apple nor an Orange. At this point, I'm leaning toward an
abstract Folder, from which I derive an AppleFolder and an OrangeFolder. But I'm still puzzling over how clients tell whether a Folder contains other
Folders or fruit objects.

Any advice would be greatly appreciated. Thanks in advance.

--
Dave Veeneman
Chicago

Nov 16 '05 #2
I think Composite pattern still applies here. Yes you are correct in saying
that apples are not oranges, and oranges/apples are not folders, but they
may have same ancestor. An analogy would be like
humans, dogs and mice, even though they are different they still are
mammals.

So following Composite pattern, your Orange and Apple objects would descend
from Leaf, whereas Folder object would descend from Composite object. You
could modify the Composite pattern in a way that you would remove Add/Remove
methods from the Component object and introduce them on Composite object, so
only Folder objects would have these methods. Then you could add an enum
property on Folder object which would tell you what type of objects the
Folder contains. Something like (following Composite pattern) :

public enum ObjectType
{
otOrange,
otApple,
otFolder
}
public abstract class MyComponent
{
private ObjectType myType;

public MyComponent()
{
}

protected void SetType(ObjectType aType)
{
myType = aType;
}

public ObjectType MyType
{
get { return myType; }
}
}
public abstract class Composite : MyComponent
{
public abstract void AddChild(MyComponent aChild);
public abstract void RemoveChild(MyComponent aChild);
public abstract MyComponent GetChild(int index);
}

public abstract class Leaf : MyComponent
{
...
}

public class Orange : Leaf
{
public Orange()
{
SetType(otOrange);
}
...
}

public class Apple : Leaf
{
public Orange()
{
SetType(otLeaf);
}
...
}

public class Folder : Composite
{
private ObjectType mContainsType;
public Folder (ObjectType aType)
{
SetType(otFolder);
mContainsType = aType;
}

public override void AddChild(MyComponent aChild)
{
if(aChild.MyType == mContainsType)
// add it to the collection
else
// throw an excpetion since the type is different
}

public abstract void RemoveChild(MyComponent aChild)
{
...
}

public abstract MyComponent GetChild(int index)
{
...
}
}
Hope this helps

Fitim Skenderi
"Dave Veeneman" <da****@nospam.com> wrote in message
news:Of**************@TK2MSFTNGP09.phx.gbl...
I'm puzzling over the best design for a Folder object.

I have two basic domain objects; leat's call them an Apple and an Orange.
The objects are maintained in separate hierarchies, and each hierarchy is
organized by Folder objects. A Folder object can contain either Apples, or
Oranges, or other Folders. A Folder contains only one type of object. For
example, a Folder won't contain Apples and other Folders, or Apples and
Oranges.

What's the best design for the Folder object? I was going to use a GoF
Composite pattern, but it doesn't really seem to fit, since a Folder is
neither an Apple nor an Orange. At this point, I'm leaning toward an
abstract Folder, from which I derive an AppleFolder and an OrangeFolder. But I'm still puzzling over how clients tell whether a Folder contains other
Folders or fruit objects.

Any advice would be greatly appreciated. Thanks in advance.

--
Dave Veeneman
Chicago

Nov 16 '05 #3
Thanks. I had pretty much come to the same conclusion, because it makes
things much easier for the client. What I'm going to do is create composite
Apple and Orange objects. Each object will have a Children property, which
will be a collection of objects of the same type. If an Apple or Orange has
children, the client knows its a node, so it will display a folder icon and
ignore anything except the children. If the Apple or Orange has no children,
the client knows the object is a leaf, so it will display it as such.
Nov 16 '05 #4
OK. I missunderstood you and thought that only folders can contain children.
Anyway
good luck with your project

Fitim Skenderi
"Dave Veeneman" <da****@nospam.com> wrote in message
news:ej**************@TK2MSFTNGP09.phx.gbl...
Thanks. I had pretty much come to the same conclusion, because it makes
things much easier for the client. What I'm going to do is create composite Apple and Orange objects. Each object will have a Children property, which
will be a collection of objects of the same type. If an Apple or Orange has children, the client knows its a node, so it will display a folder icon and ignore anything except the children. If the Apple or Orange has no children, the client knows the object is a leaf, so it will display it as such.

Nov 16 '05 #5

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

Similar topics

6
by: Martyn Lawson | last post by:
Hi, I am currently working as an Analyst on a .NET Web Project using ASP.NET and C#.NET. I have a couple of, at least what should be, quick questions: 1. My understanding of UML says that...
6
by: Dave Veeneman | last post by:
I'm puzzling over the best design for a Folder object. I have two basic domain objects; leat's call them an Apple and an Orange. The objects are maintained in separate hierarchies, and each...
14
by: Ray5531 | last post by:
I have a console application in my local computer which I like to use remoting in it,to instanciate an object (MyClass.dll) in a web application(its bin folder) in a completely seperated box(in the...
7
by: Scott M. | last post by:
In a typical class, do I need to indicate that it implements the IDisposable interface and then create a Dispose method that implements the Dispose required by the IDisposable interface or can I...
4
by: phl | last post by:
hi, My question is: 1. To avoid possible memory leaks, when you use this pattern, after you have dealth with the unmanaged resources and before you take your object off the finalize queue,...
2
by: kashif456 | last post by:
Hi, I am trying to deploy a third party object into GAC and seeing some errors. Below is the code I have added in web.config Code: <httpHandlers> <add verb="*" path="LanapCaptchaImage.aspx"...
6
by: Raj Wall | last post by:
Hi, I am trying to implement the IEqualityComparer interface for a struct so I can use it as the Key for a Dictionary. My struct declaration has: public struct Ring : IEqualityComparer {...
5
by: Noozer | last post by:
I'm looking for a "smart folder" program to run on my Windows XP machine. I'm not having any luck finding it and think the logic behind the program is pretty simple, but I'm not sure how I'd...
4
by: Mohamed Mansour | last post by:
Hello, What is the purpose of implementing the Observer Pattern if we can trigger an event easily? For example (from books), You have a "Forecaster" which notifies "Observable" when a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
Oralloy
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,...
0
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...
0
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...
0
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,...

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.