OOP and C#: Too much objects make me unhappy, but what can I do??

  • Thread starter Thread starter hufel
  • Start date Start date
H

hufel

Hi,
I'm doing my first big project in C# and I'm stuck with a problem that
I believe has a simple and efficient solution for it (I just haven't
bumped into it yet...). The concept is the following:

//Users manage clients. When the system creates the client it must
fetch the information from the DB and allow some modifications:

Client client = new Client("test");
MasterAccount ma = new MasterAccount(1324651);
client.MasterAccount = ma;
PurchaseItems pi = client.MasterAccount.GetPurchasedItems();
FillListview(pi);

// when a user selects a line in the listview, he must have the
possibility to split each line into two or more lines:

pi[0].Subitem[0..n]

// Each Subitem contains in turn two variables that will in turn be
instanced into two new classes:

Subitem si = pi[0].Subitem[0];
si.TypeA = new Type();
si.TypeB = new Type();

//TypeA and TypeB are of the same type. But in turn contain more class
variables inside

si.TypeA.SubType0
si.TypeA.SubType1
si.TypeA.SubType2
si.TypeA.SubType3

//The SubTypes contain only bool variables and a method.

My problem is that this is a representation in a "object oriented way"
(I think) of what the user sees on screen, but in terms of
calculations and memory comsumption it is not efficient, although it
is flexible enough to make future modification easy.

The problem is that now I'm stuck with this concept, and although I'm
trying to model other solutions in UML, I am not getting anywhere in
order to improve this. A friend suggested storing the values in arrays
and looping through it in order to calculate the values but I dislike
the idea due to (in my opinion) complicated implementation and the
fact that the code becomes much more difficult to understand and
modify in the future.

Please correct me if I'm wrong, through ideas in, whatever...
I welcome all suggestions.

PS. I thought about using patterns, like the singleton pattern or the
flyweight pattern, however I haven't seen real live examples and so I
don't understand how I could use them in my application.

Thank in advance
Hugo
 
Could you explain briefly what you're trying to do, rather than explaining
how you're trying to do it...
From evaluating a problem, we can the provide recommendations, opinions and
ideas as to where you could go.

Thanks

Dan.
 
I'll do my best to explain:

The application main purpose is to retrieve information regarding
purchased items by a client. Each client has a masteraccount. He uses
this MasterAccount to purchase items. This is were I begin the hard
part. I must read the items, show them in a listview and allow the
user of my application to split each item into two or more subItems.
Each SubItem contains two more pieces of information that I simply
called 'TypeA' and 'TypeB'. When the user starts working the
information regarding 'TypeA', he is filling information regarding the
client divided into 4 'blocks' that I called SubTypes. Each SubType
contains several boolean variables. I need to read the information
contained in these boolean variables, multiply it by a factor directly
related to each variable and by the amount spent on the original item.
The result is calculated at the subItem level. These values are stored
inside a sql server 2000 db.

Pseudocode:

bvA = Client.MasterAccount.PurchasedItems[].Subitems[].TypeA.SubType0.BoolVarA;
foreach (purchasedItem)
foreach (SubItem)
TypeA.CalcValues {
Calc_SubType0 {
bvA(0/1) * Factor_bvA * Amount($)
bvB(0/1) * Factor_bvB * Amount($)
bvC(0/1) * Factor_bvC * Amount($)
}
Calc_SubType1 {
bvD(0/1) * Factor_bvD * Amount($)
bvE(0/1) * Factor_bvE * Amount($)
bvF(0/1) * Factor_bvF * Amount($)
bvG(0/1) * Factor_bvG * Amount($)
bvH(0/1) * Factor_bvH * Amount($)
}
Calc_SubType2 {
(...)
}
(...)
}

TypeB.CalcValues {
(...)

}


The manager of my project doesn't agree with this structure because it
will create a lot of objects and consume lots of memory each time a
user decides to open a client account, and the application is supposed
to allow the user to open as many clients has he wishes. I'm also not
happy with it but a better solution hasn't occurred to me yet. Any
suggestions?

Thank you
Hugo



Dan Bass said:
Could you explain briefly what you're trying to do, rather than explaining
how you're trying to do it...
From evaluating a problem, we can the provide recommendations, opinions and
ideas as to where you could go.

Thanks

Dan.

hufel said:
Hi,
I'm doing my first big project in C# and I'm stuck with a problem that
I believe has a simple and efficient solution for it (I just haven't
bumped into it yet...). The concept is the following:

//Users manage clients. When the system creates the client it must
fetch the information from the DB and allow some modifications:

Client client = new Client("test");
MasterAccount ma = new MasterAccount(1324651);
client.MasterAccount = ma;
PurchaseItems pi = client.MasterAccount.GetPurchasedItems();
FillListview(pi);

// when a user selects a line in the listview, he must have the
possibility to split each line into two or more lines:

pi[0].Subitem[0..n]

// Each Subitem contains in turn two variables that will in turn be
instanced into two new classes:

Subitem si = pi[0].Subitem[0];
si.TypeA = new Type();
si.TypeB = new Type();

//TypeA and TypeB are of the same type. But in turn contain more class
variables inside

si.TypeA.SubType0
si.TypeA.SubType1
si.TypeA.SubType2
si.TypeA.SubType3

//The SubTypes contain only bool variables and a method.

My problem is that this is a representation in a "object oriented way"
(I think) of what the user sees on screen, but in terms of
calculations and memory comsumption it is not efficient, although it
is flexible enough to make future modification easy.

The problem is that now I'm stuck with this concept, and although I'm
trying to model other solutions in UML, I am not getting anywhere in
order to improve this. A friend suggested storing the values in arrays
and looping through it in order to calculate the values but I dislike
the idea due to (in my opinion) complicated implementation and the
fact that the code becomes much more difficult to understand and
modify in the future.

Please correct me if I'm wrong, through ideas in, whatever...
I welcome all suggestions.

PS. I thought about using patterns, like the singleton pattern or the
flyweight pattern, however I haven't seen real live examples and so I
don't understand how I could use them in my application.

Thank in advance
Hugo
 
Hugo,

So what you are trying to do, is work out how much cost is involved in the
customer purchasing items.
The items consist of sub parts and components with individual prices, and
the customer can choose to have a selection of parts making up this item.
Is this correct?

Starting from the bottom in a data type heirarchy, the most basic element is
the boolean variable saying whether the customer wants an item, and
presumably the price?

The OO way would then be to build a type that contains one boolean and value
pair.

class SubTypeFactor
{
private bool _itemSelected = false;
private float _priceFactor = 0.0;

public bool ItemSelected
{
get
{
return _itemSelected;
}
set
{
_itemSelected = value;
}
}

public float PriceFactor
{
// get and set for _priceFactor here
}
}


From what I can gather, your "subtypes" would then contain ArrayList of
SubTypeFactor objects as well as a costing for that subtype. For any subtype
the value would be made to be something like this...
float totalAmount = 0.0;
foreach ( SubTypeFactor stf in subTypeObject.Factors )
{
totalAmount += stf.PriceFactor *
Convert.ToFloat(stf.ItemSelected)
}


Each SubType object would look like this:
class SubType
{
private float
private ArrayList _factor = new ArrayList;

public void AddFactor ( SubTypeFactor stf )
{
// adds a factor to the factor arrayList
}

void CalculatePricing ()
{
// contains the above code snippet in the previous
paragraph
}

}

From here, each item for sale would contain these sub types, etc etc...

The amount of memory used depends on the amount of data there for the
customer to choose from, not necessarily the structure in which it's stored
(although inefficient structures repeating data etc etc will slow things
down further).

If I've missed the mark let me know and we'll see what we can help you with.

Thanks.

hufel said:
I'll do my best to explain:

The application main purpose is to retrieve information regarding
purchased items by a client. Each client has a masteraccount. He uses
this MasterAccount to purchase items. This is were I begin the hard
part. I must read the items, show them in a listview and allow the
user of my application to split each item into two or more subItems.
Each SubItem contains two more pieces of information that I simply
called 'TypeA' and 'TypeB'. When the user starts working the
information regarding 'TypeA', he is filling information regarding the
client divided into 4 'blocks' that I called SubTypes. Each SubType
contains several boolean variables. I need to read the information
contained in these boolean variables, multiply it by a factor directly
related to each variable and by the amount spent on the original item.
The result is calculated at the subItem level. These values are stored
inside a sql server 2000 db.

Pseudocode:

bvA =
Client.MasterAccount.PurchasedItems[].Subitems[].TypeA.SubType0.BoolVarA;
foreach (purchasedItem)
foreach (SubItem)
TypeA.CalcValues {
Calc_SubType0 {
bvA(0/1) * Factor_bvA * Amount($)
bvB(0/1) * Factor_bvB * Amount($)
bvC(0/1) * Factor_bvC * Amount($)
}
Calc_SubType1 {
bvD(0/1) * Factor_bvD * Amount($)
bvE(0/1) * Factor_bvE * Amount($)
bvF(0/1) * Factor_bvF * Amount($)
bvG(0/1) * Factor_bvG * Amount($)
bvH(0/1) * Factor_bvH * Amount($)
}
Calc_SubType2 {
(...)
}
(...)
}

TypeB.CalcValues {
(...)

}


The manager of my project doesn't agree with this structure because it
will create a lot of objects and consume lots of memory each time a
user decides to open a client account, and the application is supposed
to allow the user to open as many clients has he wishes. I'm also not
happy with it but a better solution hasn't occurred to me yet. Any
suggestions?

Thank you
Hugo



Dan Bass said:
Could you explain briefly what you're trying to do, rather than
explaining
how you're trying to do it...
From evaluating a problem, we can the provide recommendations, opinions
and
ideas as to where you could go.

Thanks

Dan.

hufel said:
Hi,
I'm doing my first big project in C# and I'm stuck with a problem that
I believe has a simple and efficient solution for it (I just haven't
bumped into it yet...). The concept is the following:

//Users manage clients. When the system creates the client it must
fetch the information from the DB and allow some modifications:

Client client = new Client("test");
MasterAccount ma = new MasterAccount(1324651);
client.MasterAccount = ma;
PurchaseItems pi = client.MasterAccount.GetPurchasedItems();
FillListview(pi);

// when a user selects a line in the listview, he must have the
possibility to split each line into two or more lines:

pi[0].Subitem[0..n]

// Each Subitem contains in turn two variables that will in turn be
instanced into two new classes:

Subitem si = pi[0].Subitem[0];
si.TypeA = new Type();
si.TypeB = new Type();

//TypeA and TypeB are of the same type. But in turn contain more class
variables inside

si.TypeA.SubType0
si.TypeA.SubType1
si.TypeA.SubType2
si.TypeA.SubType3

//The SubTypes contain only bool variables and a method.

My problem is that this is a representation in a "object oriented way"
(I think) of what the user sees on screen, but in terms of
calculations and memory comsumption it is not efficient, although it
is flexible enough to make future modification easy.

The problem is that now I'm stuck with this concept, and although I'm
trying to model other solutions in UML, I am not getting anywhere in
order to improve this. A friend suggested storing the values in arrays
and looping through it in order to calculate the values but I dislike
the idea due to (in my opinion) complicated implementation and the
fact that the code becomes much more difficult to understand and
modify in the future.

Please correct me if I'm wrong, through ideas in, whatever...
I welcome all suggestions.

PS. I thought about using patterns, like the singleton pattern or the
flyweight pattern, however I haven't seen real live examples and so I
don't understand how I could use them in my application.

Thank in advance
Hugo
 
Thanks Dan, I used a portion of your code and I'm building another
structure that seems much more efective to me.

Thanks a lot
Hugo
 
Back
Top