class properties

  • Thread starter Thread starter TS
  • Start date Start date
T

TS

i have a question about this situation:

i have a class that i use that i need to know 1 piece of data that exists in
a different class. Im wondering is it wrong to add the property that i need
onto the first class instead of having to instantiate the whole other class
for a single property?

If it isn't bad, where would you draw the line on what duplicated properties
you have?

thanks for the insight
 
Could you be more specific? The situation you described could apply to
so many different things, it's hard to know what advice to give.
 
Hi TS,

Thanks for your post.

Yes, I think if there are not too many properties we want to get from
class2 and these properties are not very hard to implement in class1, we
can just create a duplicated property for class1 usage.

However, if in OOP, we want to expose a number of properties from class2 to
class1, I think composition or inheritance can be considered. To determine
whether or not to use composition/inheritance or just duplicate property
definition, we can judge from the class relation semantics. If in logic
class2 is a part feature of class1, composition should be a suitable
choice, and we can use expose the wanted class2 properties as properties of
class1. If in logic class1 is a specific type of class2, we can just
inherit from class2 to create class1, then in class1 we can get the
properties of class2 without any problem.

Hope this helps
===========================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Thanks, I didn't know what the word composition meant. Now that i do, i know
that this is the method employed in the project i am working on....which
brings me back to my original question:

if you use composition, and access a property from class2 that simply points
to class1, you have to load class1 to access the property you are actually
interested in. This is how you can access a property of class1 from class2.
The thing is is that you have to load the whole class to access that single
property.

I can see that is good OOP, just wondering what your thoughts were,

thanks Jeffrey!
 
Hi TS,

Thanks for your feedback.

I am not sure I express myself clear in the last reply. Yes, composition is
a common way to use in this scenario, and we usually use this way.
However, this is not the only way, if the number of properties we want
expose from class1 is very large, we can just re-implement these wanted
properties in class2 without class1 knowing about. I think this will reduce
a lot of space, performance etc.., if in OOP semantics, the class1 should
be part feature of class2, I think your way of using composition(that is
create a class1 instance in class2) is the best choice.

Note: there may be some properties that is hard to re-implement out of the
scope of class1, in this situation, I think we have to use composition.

Also, I suggest you read my last reply again to gain a better understanding
of my comment. If you still have any concern, please feel free to tell me,
thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi TS,

Does my reply make sense to you? If you still have any concern, please feel
free to tell me, thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top