473,756 Members | 3,686 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Design Problem

Design Problem
===============

Would appreciate any help or suggestion on this design issue. I have
spent a great deal of time and effort for an elegant solution but it
seems I am not getting anywhere...

I have simplified my design problem to the following:

Consider the following classes:
Entity, Point, Polygon, Shape

Entity is the base class of Point, Polygon and Shape

Point is obvious
Polygon is defined using a set of points
Shape is a "set" of polygons BUT it will have one outside polygon and
zero or more inside polygons (ie. holes)

If I apply the composite pattern, I can make Entity to be the component
and Point will be a leaf. [There are other leaves in my original
design. Remember this is a simple example]

Then Polygon will be a composite and so will Shape.
The datastructure to use for the polygon could be an array or a list.

However, the datastructure to use for Shape is debatable. Unlike a
polygon where its points can happily reside side by side in an array or
a list, the polygons in a shape have a hierarchial relationship. The
outside polygon is the parent of the inside polygons. It therefore
seems reasonable to use a tree data structure in Shape to store the
polygons. GOF suggest using different data structure to store the
children of a composite and Shape seems to offer an object where a tree
would be appropriate.

If the above solution is OK so far then a number of questions arise.

1) the parent pointer for a composite pattern that is declared in the
Entity(componen t) can be set for points of a polygon when we add them
to a polygon. So each point of a polygon knows its parent(polygon) . But
what about the parent of a polygon in a shape. Will this pointer point
to the Shape? Ie all the polygons of a shape (outside and inside) will
know their parent in the composite? Or should this pointer be used to
indicate the relationship between the polygons. In other words an
inside polygon(hole) will have its parent pointer set to the outside
polygon of the shape. Equally the parent pointer of the outside polygon
of a shape will point to the shape(its parent). Is the parent pointer
confined to the tree defined by the composite pattern and not the tree
inside a shape? Can we traverse up from a hole to the outside polygon
and then to the shape?

2) Should we have an abstract base class named say Composite for
Polygon and Shape and if so what will be its interface? The interface
for a Composite in GOF pattern suggests methods such as Add, Remove and
GetChild. Will these be enough for the Shape which has a tree data
structure? Or perhaps we will ahve two sets of interfaces one for
Polygon(list) and one for Shape(tree)?

3) It may be the case that a shape (an outside polygon and some inside
polygon(holes)) could have another shape inside one of the holes.
Should we create a new class for such a composite named NestedShape or
just use Shape. What will be the datastructure to store shape in
NestedShape? Tree again? I like to be able to ask a NestedShape to give
me all the holes for example...

4) If I do use a tree data structure for shape whats the best option?
Implement the tree or use a library? My tree has to be general (not
binary etc) and most libraries out there implement Binary trees, Avl,
etc. Does anyone know a good implementation of a general tree data
structure in c++?

5) Are there examples of the use of the Composite Pattern with tree
data structure used to store the children? All examples I have seen use
a list or an array.
Many thanks in advance for suggestions...

Merlin

Oct 7 '05 #1
3 2004
* Merlin:
Design Problem
===============


What's the C++ question?

I think this is _most probably_ off-topic in clc++, and suggest you post this
question in an oo and/or software engineering group.

That said, it seems to me you're barking up the wrong windmill: just define
the external functionality you require and implement the thing, which
shouldn't take more than say 15 minutes; then, if it's too inefficient, apply
common sense, measure, and optimize (that may take a bit longer). Do forget
about parenting and such stuff, if it isn't part of the required external
functionality. Also, you should probably remove your Entity class, which
seems to only play the role of a Universal Base Class, which is Bad (TM).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 7 '05 #2
On 6 Oct 2005 17:24:29 -0700, "Merlin" <me********@hot mail.com> wrote:
Design Problem
============== =

Would appreciate any help or suggestion on this design issue. I have
spent a great deal of time and effort for an elegant solution but it
seems I am not getting anywhere...

I have simplified my design problem to the following:

Consider the following classes:
Entity, Point, Polygon, Shape

Entity is the base class of Point, Polygon and Shape

Point is obvious
Polygon is defined using a set of points
Shape is a "set" of polygons BUT it will have one outside polygon and
zero or more inside polygons (ie. holes)
Not sure about this, but it sounds like you are eliminating certain
kinds of shapes from your Shapes (e.g. circles, ellipses, Moebius
strips, etc.) Usually, "Shape" is abstract and doesn't know how it
will be drawn.
If I apply the composite pattern, I can make Entity to be the component
and Point will be a leaf. [There are other leaves in my original
design. Remember this is a simple example]
As to the design, a Circle or Ellipse only needs to know just a few
points. A circle can be described by one point (i.e. the center) and a
length (i.e. the radius). The ellipse needs a few more data to
describe its circumference.
Then Polygon will be a composite and so will Shape.
The datastructure to use for the polygon could be an array or a list.
Polygon would indeed be described sufficiently as an array of points.
Since the order of the points appears to be important, I would use a
vector, and not a list, to describe them...unless you need to add or
subtract certain points very fast, in which case std::list<> might be
more appropriate.
However, the datastructure to use for Shape is debatable. Unlike a
polygon where its points can happily reside side by side in an array or
a list, the polygons in a shape have a hierarchial relationship. The
outside polygon is the parent of the inside polygons. It therefore
seems reasonable to use a tree data structure in Shape to store the
polygons. GOF suggest using different data structure to store the
children of a composite and Shape seems to offer an object where a tree
would be appropriate.


[rest snipped]

Don't get caught up in the data representation until you have decided
how you will use your structures. Maybe an array of points is not even
necessary?

--
Bob Hairgrove
No**********@Ho me.com
Oct 7 '05 #3
Thanks for the reply. Please bear in mind this is striclty a simplified
model and its not meant to be a hierarchy for other shapes such as
circles etc. I am only concerned here with the datastructures to use
for the composites in the context of Composite Design Pattern

Regards

Merlin

Oct 7 '05 #4

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

Similar topics

3
3151
by: Omer van Kloeten | last post by:
The Top Level Design: The class Base is a factory class with a twist. It uses the Assembly/Type classes to extract all types that inherit from it and add them to the list of types that inherit from it. During run time, using a static method, the class creates an instance of the derived class using the Activator class and returns it. This design pattern is very similar to the design pattern applied by the Assembly class. The twist is...
7
3017
by: Shimon Sim | last post by:
I have a custom composite control I have following property
22
4739
by: Krivenok Dmitry | last post by:
Hello All! I am trying to implement my own Design Patterns Library. I have read the following documentation about Observer Pattern: 1) Design Patterns by GoF Classic description of Observer. Also describes implementation via ChangeManager (Mediator + Singleton) 2) Pattern hatching by John Vlissides Describes Observer's implementation via Visitor Design Pattern. 3) Design Patterns Explained by Alan Shalloway and James Trott
1
6341
by: Nogusta123 | last post by:
Hi, I have had a lot of problems getting web pages, master pages and content pages to render in VS2005 design view the same as they would in Internet Explorer. I did a lot of looking on the internet for answers but didn't have much luck. Anyway I believe I have found the causes of the problems and thought I should share them in case any one else is feeling the pain and also to find out what other peoples opinions are, on whether these...
0
2508
by: YellowFin Announcements | last post by:
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 reached the masses is because most of the tools are so difficult to use and reveal so little
17
4851
by: roN | last post by:
Hi, I'm creating a Website with divs and i do have some troubles, to make it looking the same way in Firefox and IE (tested with IE7). I checked it with the e3c validator and it says: " This Page Is Valid XHTML 1.0 Transitional!" but it still wouldn't look the same. It is on http://www.dvdnowkiosks.com/new/theproduct.php scroll down and recognize the black bottom bar when you go ewith firefox(2.0) which isn't there with IE7. Why does...
9
3660
by: AceKnocks | last post by:
I am working on a framework design problem in which I have to design a C++ based framework capable of solving three puzzles for now but actually it should work with a general puzzle of any kind and I need your able help in this activity. Objective - In this activity you will design a framework capable of solving any puzzle of a specific type and, as a test of this framework, use the framework to solve a very simple puzzle. In this first...
5
1477
by: istillshine | last post by:
Particularly for medium-sized (10,000 ~ 20,000 lines) programs, what are useful strategies to design them before coding? My strategies are: 1. Imagine what the final program would look like. Write down options. 2. Write down many structures, such as
4
2467
by: Ken Fine | last post by:
I've been living with a frustrating issue with VS.NET for some months now and I need to figure out what the problem is. Hopefully someone has run into the same issue and can suggest a fix. I suspect some permissions problem. I'm running VS.NET 2008 in Vista. Symptoms and observations: * ASP.NET's native ImageMap and Image controls work just fine and provide a design-time preview of images that are referenced via the ImageUrl property *...
2
9840
by: existential.philosophy | last post by:
This is a new problem for me: I have some queries that open very slowly in design view. My benchmark query takes about 20 minutes to open in design view. That same query takes about 20 minutes to open in datasheet view. As an experiment, I deleted all rows in all tables; after that, the query took only seconds to open in both design view and datasheet view. From these facts, I conclude that Access is evaluating the query when I go to...
0
9456
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
10034
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, 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...
1
9843
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
9713
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
8713
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...
1
7248
isladogs
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...
0
5142
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...
0
5304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2666
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.