Entwurfsidee?



  • hi,

    ich bin nicht glücklich mit dem Code hier ich will alle pairs von den maps in CWidget holen. Dachte ich setz immer was ich brauch mit ein enum Typ und hol mir die Werte. Um das noch zu vereinfachen wollte ich daraus noch ne Schleife basteln ,die nicht funktioniert, naja ein eher kläglicher Versuch.
    Aber vielleicht hat der Entwurf schon ein falschen Ansatz hab auch schon mal versucht das ganze mit templates zu realisieren aber irgendwie wird es dann für mich zu unübersichtlich.
    Wär nett wenn jemand wüsste wie man den Code vereinfachen könnte.

    enum propertyType {Combo, Bool, File, Text, Digit};
    
    class CWidget
    {
    public:
    	CWidget();
    	void setTypeProperty(propertyType);
    	void firstProperty();
    	void nextProperty();
    	bool isLastProperty();
    
    	void setComboProperty(std::string, std::string);
    	void setBoolProperty(std::string, bool);
    	void setFileProperty(std::string, std::string);
    	void setTextProperty(std::string, std::string);
    	void setDigitProperty(std::string, int);
    
    	std::string getNameProperty();
    	std::string getComboProperty(std::string);
    	bool 	    getBoolProperty(std::string);
    	std::string getFileProperty(std::string);
    	std::string getTextProperty(std::string);
    	int 	    getDigitProperty(std::string);
    
    private:
    	propertyType m_type;
    	std::map<std::string, std::string> m_comboProperties;
    	std::map<std::string, bool> 	   m_boolProperties;
    	std::map<std::string, std::string> m_fileProperties;
    	std::map<std::string, std::string> m_textProperties;
    	std::map<std::string, int> 	   m_digitProperties;
    
    	std::map<std::string, std::string>::iterator m_it_comboProperties;
    	std::map<std::string, bool>::iterator 	     m_it_boolProperties;
    	std::map<std::string, std::string>::iterator m_it_fileProperties;
    	std::map<std::string, std::string>::iterator m_it_textProperties;
    	std::map<std::string, int>::iterator 	     m_it_digitProperties;
    };
    
    bashWidgets::propertyType type;
    
     for(type=Bool; type<Digit; type++)
      {
            bashWidget->firstProperty();
    		while (!bashWidget->isLastProperty())
    		{
    		  string name = bashWidget->getNameProperty();
    
    		      switch (type)
    			{
    			case Combo: //addComboItem(bashWidget->getNameProperty(), bashWidget->getComboProperty());
    				break;
    			case Bool: addCheckItem(name, bashWidget->getBoolProperty(name));
    				break;
    			case File: //
    				break;
    			case Text: addTextItem(name, bashWidget->getTextProperty(name));
    				break;
    			case Digit: addSpinItem(name, bashWidget->getDigitProperty(name));
    				break;
    			}
    		   bashWidget->nextProperty();
    		}
       bashWidget->setTypeProperty(type);
      }
    

    MfG
    xmarvel



  • Mach eine Klasse Property leite davon TextProperty, BoolProperty, ... ab. Dann kannst du das Ganze in ein Set stecken und nicht in X Maps.

    Außerdem kannst du dir noch Zusatzinfos halten wenn das mal notwendig sein sollte....



  • vielleicht hab ich dich nicht richtig verstanden hab mal versucht das umsetzen und die Sachen markiert wo ich nicht weiss wie man das machen soll.

    class CProperty
    {
     public:
    	virtual ~CProperty();
    	virtual void setProperty(std::string, ???)=0; // da kann ich ja nicht den genauen Typ angeben
    	virtual ??? getProperty(std::string)=0; //hier genau so
    };
    
    class CTextProperty : public CProperty
    {
     public:
     	void setProperty(std::string, std::string);
    	std::string getProperty(std::string);
    };
    
    class CDigitProperty : public CProperty
    {
     public:
    	void setProperty(std::string, int);
    	int getProperty(std::string);
    };
    // ... usw
    
    class CWidget
    {
     public:
    	void firstProperty();
    	void nextProperty();
    	bool isLastProperty();
    
    	void setProperty(std::string, CProperty);
    	CProperty getProperty();
     private:
    	std::vector<CProperty> m_storage;
    };
    

    MfG
    xmarvel



  • Ja in der Art. 🙂

    (Naja... da alle Properties einen Namen haben würde ich den über die Basisklasse mit Get und Set-Funktionen zugreifbar machen und den Wert durch separate Funktionen in den abgeleiteten Klassen. (Vielleicht in der oberklasse generell in einen String für ne Text-Ausgabe?)
    Außerdem würde ich den Typ des Property durch eine virtuelle Funktion,
    die von den abgeleiteten Klassen immer brav überschrieben wird abfragbar machen.

    Das hier wäre auch besser:

    CProperty * firstProperty();
       CProperty * nextProperty();
    

    Geben entweder das Property zurück oder falls das Ende erreicht ist NULL.)


Anmelden zum Antworten