Does the class only print for identifying the type of good idea?

The question is longer than usual, but I tried to clarify. Please carry me and read the whole question, this can be an interesting problem.

I have method A, which currently accepts an object with two List<X> properties, but represents two different objects and therefore the property is aptly named R and S. Class structure

 class A { List<X> R; List<X> S; } 

and now I have a method that accepts type A input and works with both (assembly and signatures below)

 public void updateMe(A objA) { } 

now I have a case where I need to reuse this method, but now the fact is that I do not need to distinguish between entities and, therefore, have one list, List<X> T.

Now my question is how to reorganize the method for working with one list, but at the same time provide the ability to distinguish between two lists in the previous case.

My method will update these collections, thus either add to or remove from the list.

My solution now is to create new classes to represent two different entities that are derived from X, and then pass this list of the base class to my method and let the method update this base class, and then, reading it, I can determine which object is type.

So my new classes will be

 public class X1:X { } public class X2:X { } class A { List<X> R; } 

Is this the best solution or are there any other approaches that I could use here. I just don't like the idea of ​​extending a class just to identify the type without adding any properties.

+4
source share
2 answers

I would say either: 1. Use ENUM :) 2. Make the Y & Z classes to extend abstract X with the abstract getList () method. But this is certainly unnecessary for a simple case. So, back to 1 - just use the enumeration :)

0
source

It seems that the goal is to achieve a form of polymorphism in terms of class content. In this sense, you should isolate the class for internal content, so my answer focuses on the goal that leads to your question, not the question itself.

In general, for a priori your question, I would say that the difference between two collections, each of which is a different object, is not a question of a typical type, if we assume that this is correct, but the responsibility of the caller.
My answer follows this path and, nevertheless, implements the variable internal representation and update logic in a strongly typed way, with static polymorphism (excluding due to the time explaining this choice to you).

Typically, to decide whether to use subclass X , you should ask yourself: the updateMe logic, which controls the updating of List<X> R and List<X> S , in accordance with the details of objects X

I suggest from your question that these are not always two lists, and my solution also generalizes the type of collections.
Hope to give you a starting point. If I guessed your request, I will illustrate more.

 template <class staticPolimorphicAInnerT> class A { public: void updatMe (const A& ref) { // <do something pre if necessary> updateInnerMe (A.innerObj); // <do some other post if necessary> } void updateInnerMe (const staticPolimorphicAInnerT& innerRef) { innerObj.updateMe (innerRef); // or better: // inner_update_method (innerRef) } private: // is used to request the static polymorphism typedef void (staticPolimorphicAInnerT::* inner_update_method) (const staticPolimorphicAInnerT&) staticPolimorphicAInnerT innerObj; //<something more i hope> } template <class T> class AInnerFirst { public: void updatMe (const AInnerFirst& ref) { // <do something with R> // <do something with S> } private: List<T> R; List<T> S; } template <class T> class AInnerSecond { public: void updatMe (const AInnerSecond& ref) { // <do something with W> } private: Vector<T> W; } 
0
source

All Articles