C + + design mode---strategy mode

Source: Internet
Author: User

First, preface

Learn the first design pattern! Do not know the right to understand, I expect you to communicate more ~

Strategy mode: The strategy mode, defines the algorithm family, separately encapsulates, this pattern can make the algorithm's change independent to use the algorithm the customer. The strategy mode encapsulates the logical algorithm into a class, and the concrete algorithm is implemented in the composite object by the combination method, and the implementation of the abstract interface is delegated to the composite object by the way of the delegate. The model structure diagram is as follows:

 Ii. Examples of strategy strategies

Recently, the algorithm of the fusion of remote sensing images, PCA, Brovey and Sfim algorithms, can be used in this study strategy strategy.

About the three fusion algorithms belong to the substitution class algorithm, the idea is to replace the other one with one. Then obtain high-resolution HF information and ground-resolved spectral information.

Based on the idea of strategy strategy, we first define a Ccontextfusion class, which has a dofusionaction () method, which is mainly used to implement the algorithm logic abstraction interface, the header file is as follows:

#pragma once#include "StrategyFusion.h" Class Cstrategyfusion;class Ccontextfusion{public:ccontextfusion ( Cstrategyfusion *STG); ~ccontextfusion (void); bool Dofusionaction ();p rivate:cstrategyfusion *m_stg;};

CPP files are as follows:

#include "ContextFusion.h" ccontextfusion::ccontextfusion (cstrategyfusion *stg) {M_STG = STG;} Ccontextfusion::~ccontextfusion (void) {if (!M_STG) delete M_stg;} BOOL Ccontextfusion::D ofusionaction () {return m_stg->runfusion ();}

  

After completing this class, we first define a fusion algorithm super class Cstrategyfusion, considering that the above three algorithms are to achieve the image resampling and gain coefficient calculation. To this end, we put the implementation of these two methods in the superclass, for other methods related to the fusion algorithm, put into the specific algorithm class. The class structure relationships are as follows:

The header files are as follows:

#pragma once#include <iostream> #include <string> #include <omp.h> #include <gdal_alg_priv.h> Class cstrategyfusion{public:cstrategyfusion (void); virtual ~cstrategyfusion (void); virtual bool Runfusion () = 0; Protected:int Resamplemsstopan (); void Getgain (); std::string m_panfilename;std::string m_mssfilename;std::string m_ Resamplefilename;std::string M_fusionfilename; Gdaldatatype m_dt;int m_resamplemodel;int M_gainx;  X-direction gain cell number int m_gainy;  Number of Y-direction gain cells int m_fusionwidth;  Image width int m_fusionheight after fusion; After fusion image high double m_fusiongeotransform[6];//private:};

  

#pragma once#include "Strategyfusion.h" class CSTRATEGYFUSIONBYPCA:p ublic cstrategyfusion{public: CSTRATEGYFUSIONBYPCA (void); ~cstrategyfusionbypca (void), bool Runfusion ();p rivate:double *calmssmean ();d ouble * Calcovmaxtrix (double *bandmean), bool EEJCB (double a[],int n,double v[],double eps,int JT), void Sorteigenvector (int ibandcount,double * eigenvector,double * COVAFTEREEJCB); void Pcatransform (double *eigenvector);d ouble* cdf (int *h,int length), void Matchhistogram (), void Inversematrix (double *matrix, int n), void INVERTPCA (double * eigenvector); std:: String m_pcafilename;std::string m_pannewfilename;};

  

#pragma once#include "Strategyfusion.h" class Cstrategyfusionbybrovey:p ublic cstrategyfusion{public: Cstrategyfusionbybrovey (void); ~cstrategyfusionbybrovey (void); bool Runfusion ();p rivate:bool cnbybrovery ();};

  

#pragma once#include "Strategyfusion.h" class Cstrategyfusionbysfim:p ublic cstrategyfusion{public: Cstrategyfusionbysfim (void); ~cstrategyfusionbysfim (void); bool Runfusion ();p rivate:bool filtermeanbypan (); bool Sfim ();};

  

Summary: Through the strategy strategy, we can freely and conveniently supplement the new substitution class-based fusion algorithm, and even all other fusion algorithms, in other words, can freely customize their own fusion algorithm class, this interface-based implementation does not cause the risk of unpredictable due to inheritance.

C + + design mode---strategy mode

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.