Frage

I still having some trouble with the conception of an idea I had.

The problem is this wanted to create a class A which can vary its properties and methods apartid system configuration, which could change dynamically.

Simply I could have a class "A1" with the properties X, Y, and only with the add method and another class "A2" with X, Y, Z and the methods add and subtract. and probably will have them within the system.

but the idea is that the class "A" or better the object of class A can dynamically change to one of two recebebdo their methods.

I thought about using the strategy pattern because I can change dynamically, but I stuck the functions that can not be dynamically added and I read a little about the Abstract Factory and Factory pattern, it "nearly" meets my need. but I think it is because my example is not very clear.

What I came to imagine is that the class would be their own Abstract factory, but that still leaves me with doubts and their new methods.

It is not for a specific language, but my mind and ease of use of the system without the need to manage and work with multiple classes in the code, it would be an interface to the actual classes.

The big question is: Is it possible? How can I do?

Exemple:

 class System
 {
setConfig(Config.VALUE);
 }

 class A 
 {
if(system.config == Config.A1)
{
    this.handleClass = new A1();
}
else
{
    this.handleClass = new A2();
}
 }

 class A1
 {
add(value:float);
 }

 class A1
 {
subtract(value:float);
 }

 main
 {
System system.setConfig(Config.A);
A foo = new A();

A.add() // because the add method belongs to class A1;

System system.setConfig(Config.B);

A.subtract() // because the add method belongs to class A2;

 }
War es hilfreich?

Lösung

Language isn't obvious. In most languages (C++, C#, Java) you are to return specific type; even if this is "general" type like void* or object, you usually have to know which methods of it you can and can't call at compile-time. Workarounds are possible, but in your case, they probably aren't needed.

What you do need is Factory or Abstract Factory pattern, I suppose. You have to figure out what is general interface for all possible cases (because if function can return either int or array, you can't add returned values, neither you can take their elements) and implement this interface in different classes. Then, whenever you need to create instance of interface, you request Factory to do that, and Factory chooses which class it should be.

interface ISampleValues{
  public abstract int NumberOfValues();
  public abstract int SpecificValue(int index);
}

class SingleValue : ISampleValues {
  private int m_value;
  public SingleValue(int val) {m_value = val;}
  public override int NumberOfValues() {return 1;}
  public override int SpecificValue(int index) {return m_value;}
}

class ArrayOfValues : ISampleValues {
  private int[] m_values;
  public ArrayOfValues(int[] arr) {m_values = arr.Clone();}
  public override int NumberOfValues() {return m_values.Length;}
  public override int SpecificValue(int index) {return m_values[index];}
}

class ValuesFabric {
  public ISampleValues GetValues() {
    if(System.Config.Multiple)
      return new ArrayOfValues(System.Config.ValuesArray);
    else 
      return new SingleValue(System.Config.ValuesArray[0]);
  }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top