mercredi 27 juillet 2016

is this a example where abstract factory pattern is used?

Now I am developing a class for recognize a object in a photo, and this class is composed of several components (classes). For example,

class PhotoRecognizer
{
 public:
    int perform_recogniton()
    {
        pPreProcessing->do_preprocessing();
        pFeatureExtractor->do_feature_extraction();
        pClassifier->do_classification()
     }

    boost::shared_ptr<PreProcessing> pPreProcessing;
    boost::shared_ptr<FeatureExtractor> pFeatureExtractor;
    boost::shared_ptr<Classifier> pClassifier;

}

In this example, when we use this class to perform recognition, we invoke other classes PreProcessing, FeatureExtractor and Classifier. As you can image, there are many different methods to implement each class. For example, for the Classifier class, we can use SVMClassfier or NeuralNetworkClassifer, which is a derived class of the basic Classifier class.

class SVMClassifier: public Classifier
{
 public:
    void do_classification();

};

Therefore, by using different elements within PhotoRecognizer class, we can create different kinds of PhotoRecongnizer. Now, I am building a benchmark to know how to combine these elements together to create an optimal PhotoRecognizer. One solution I can think of is to use abstract factory:

class MethodFactory
{
 public:
      MethodFactory(){};
        boost::shared_ptr<PreProcessing> pPreProcessing;
        boost::shared_ptr<FeatureExtractor> pFeatureExtractor;
        boost::shared_ptr<Classifier> pClassifier;

};
class Method1:public MethodFactory
{
  public:
     Method1():MethodFactory()
     { 
          pPreProcessing.reset(new GaussianFiltering);
          pFeatureExtractor.reset(new FFTStatictis);
          pClassifier.reset(new SVMClassifier);

      }

};

class Method2:public MethodFactory
{
  public:
     Method1():MethodFactory()
     { 
          pPreProcessing.reset(new MedianFiltering);
          pFeatureExtractor.reset(new WaveletStatictis);
          pClassifier.reset(new NearestNeighborClassifier);

      }

};



 class PhotoRecognizer
    {
     public:
        PhotoRecognizer(MethodFactory *p):pFactory(p)
        {
         }
        int perform_recogniton()
        {
            pFactory->pPreProcessing->do_preprocessing();
            pFactory->pFeatureExtractor->do_feature_extraction();
            pFactory->pClassifier->do_classification()
         }

       MethodFactory *pFactory;


    }

So when I use Method1 to perform photo recognition, I can simply do the following:

Method1 med;
PhotoRecognizer recogMethod1(&med);
med.perform_recognition()

Further more, I can even make the class PhotoRecognizer more compact:

enum RecMethod
{
  Method1, Method2

};

class PhotoRecognizer
{
public:
    PhotoRecognizer(RecMethod)
    {
       switch(RecMethod)
       {
          case Method1:
             pFactory.reset(new Method1());
             break;
           ...
         }
     }

    boost::shared_ptr<MethodFactory> pFactory;

};

So here is my question: is abstract factory design pattern well justified in the situation described above? are there alternative solutions? Thanks.

Aucun commentaire:

Enregistrer un commentaire