| TerraLib 4.1

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files
- Examples

- File List
- File Members

E:/Projetos_Primeiro_Semestre_2012/TerraView/terralib/src/terralib/kernel/TeAbstractFactory.h
Go to the documentation of this file.

    00001 /************************************************************************************
    00002 TerraLib - a library for developing GIS applications.
    00003 Copyright © 2001-2007 INPE and Tecgraf/PUC-Rio.
    00004
    00005 This code is part of the TerraLib library.
    00006 This library is free software; you can redistribute it and/or
    00007 modify it under the terms of the GNU Lesser General Public
    00008 License as published by the Free Software Foundation; either
    00009 version 2.1 of the License, or (at your option) any later version.
    00010
    00011 You should have received a copy of the GNU Lesser General Public
    00012 License along with this library.
    00013
    00014 The authors reassure the license terms regarding the warranties.
    00015 They specifically disclaim any warranties, including, but not limited to,
    00016 the implied warranties of merchantability and fitness for a particular purpose.
    00017 The library provided hereunder is on an "as is" basis, and the authors have no
    00018 obligation to provide maintenance, support, updates, enhancements, or modifications.
    00019 In no event shall INPE and Tecgraf / PUC-Rio be held liable to any party for direct,
    00020 indirect, special, incidental, or consequential damages arising out of the use
    00021 of this library and its documentation.
    00022 *************************************************************************************/
    00026 #ifndef  __TERRALIB_INTERNAL_NEW_FACTORY_H
    00027 #define  __TERRALIB_INTERNAL_NEW_FACTORY_H
    00028
    00029 #include "TeDefines.h"
    00030
    00031 #include <map>
    00032 #include <string>
    00033
    00034 //using std::map;
    00035 //using std::string;
    00036
    00038 /*
    00039     This file implements a templated version of the Abstract Factory Pattern
    00040            See: "Design Patterns - Elements of Reusable Object-Oriented Software", by Gamma et. al
    00041            page 87.
    00042   \author Gilberto Camara, Lúbia Vinhas
    00043 */
    00044 template <typename PRODUCT,                                                             // The product to be made
    00045                         typename PARAMS,                                                           // The parameter necessary to make the product
    00046                         typename PRODUCTIDTYPE=std::string>       // The type used to identify the product
    00047 class /*TL_DLL*/ TeAbstractFactory
    00048 {
    00049 public:
    00050
    00052            typedef std::map<PRODUCTIDTYPE, TeAbstractFactory<PRODUCT,PARAMS,PRODUCTIDTYPE>* > TeFactoryMap;
    00053
    00055            static TeFactoryMap& instance()
    00056            {
    00057                       static TeFactoryMap Fmap_;
    00058                       return Fmap_;
    00059            }
    00060
    00061 protected:
    00062
    00064     virtual PRODUCT* build(PARAMS* /*arg*/)
    00065            {
    00066                       return PRODUCT::DefaultObject();
    00067            }
    00068
    00070    virtual PRODUCT* build()
    00071            {
    00072                       return PRODUCT::DefaultObject();
    00073            }
    00074
    00075 public:
    00077            /*
    00078                       \param prodId the identification of the product that the factory produces
    00079            */
    00080    TeAbstractFactory(PRODUCTIDTYPE prodId):
    00081                       productIdentification_(prodId)
    00082            {
    00083                       TeAbstractFactory<PRODUCT,PARAMS,PRODUCTIDTYPE>::instance()[prodId] = this;
    00084            }
    00085
    00086
    00088    virtual ~TeAbstractFactory(){ }
    00089
    00091            /*
    00092                       \param arg a pointer to an structure that contains the arguments
    00093                       necessary to build the product. The type of product has to be extracted
    00094                       from the arguments
    00095            */
    00096            static PRODUCT* make(PARAMS* arg)
    00097            {
    00098                       // If there are no arguments or factory identification return the default object
    00099                       if (!arg)
    00100                                  return PRODUCT::DefaultObject();
    00101
    00102                       PRODUCTIDTYPE productId = arg->getProductId();
    00103
    00104                       // try to find the name on the factory dictionary
    00105                       typename TeFactoryMap::iterator i = TeAbstractFactory<PRODUCT,PARAMS,PRODUCTIDTYPE>::instance().find(productId);
    00106
    00107                       // Not found ?  return the Default Object
    00108                       if (i == TeAbstractFactory<PRODUCT,PARAMS,PRODUCTIDTYPE>::instance().end())
    00109                                  return PRODUCT::DefaultObject();
    00110
    00111                       // Create an object, based on the input parameters
    00112                                  return (*i).second->build(arg);
    00113            }
    00114
    00116            static PRODUCT* make(PRODUCTIDTYPE prodId)
    00117            {
    00118                       // try to find the name on the factory dictionary
    00119                       typename TeFactoryMap::iterator i = TeAbstractFactory<PRODUCT,PARAMS,PRODUCTIDTYPE>::instance().find(prodId);
    00120
    00121                       // Not found ?  return the Default Object
    00122                       if (i == TeAbstractFactory<PRODUCT,PARAMS,PRODUCTIDTYPE>::instance().end())
    00123                                  return PRODUCT::DefaultObject();
    00124
    00125                       // Create an object, based on the input parameters
    00126                       return (*i).second->build();
    00127            }
    00128
    00129 private:
    00130            PRODUCTIDTYPE  productIdentification_;
    00131 };
    00132
    00134 //template <typename PRODUCT, typename PARAMS, typename PRODUCTIDTYPE>
    00135 //inline TeAbstractFactory<PRODUCT,PARAMS,PRODUCTIDTYPE>::TeAbstractFactory(PRODUCTIDTYPE prodId):
    00136 //         productIdentification_(prodId)
    00137 //{
    00138 //         TeAbstractFactory<PRODUCT,PARAMS,PRODUCTIDTYPE>::instance()[prodId] = this;
    00139 //}
    00140
    00141 //template <typename PRODUCT, typename PARAMS, typename PRODUCTIDTYPE>
    00142 //inline TeAbstractFactory<PRODUCT,PARAMS,PRODUCTIDTYPE>::~TeAbstractFactory()
    00143 //{
    00144
    00145 //}
    00146
    00148 //template <typename PRODUCT, typename PARAMS, typename PRODUCTIDTYPE>
    00149 //inline PRODUCT*
    00150 //TeAbstractFactory<PRODUCT,PARAMS,PRODUCTIDTYPE>::make(PRODUCTIDTYPE producId)
    00151 //{
    00152 //         // try to find the name on the factory dictionary
    00153 //         typename TeFactoryMap::iterator i = TeAbstractFactory<PRODUCT,PARAMS,PRODUCTIDTYPE>::instance().find(producId);
    00154
    00155 //         // Not found ?  return the Default Object
    00156 //         if (i == TeAbstractFactory<PRODUCT,PARAMS,PRODUCTIDTYPE>::instance().end())
    00157 //                    return PRODUCT::DefaultObject();
    00158
    00159 //         // Create an object, based on the input parameters
    00160 //         return (*i).second->build();
    00161 //}
    00162
    00164 //template <typename PRODUCT, typename PARAMS, typename PRODUCTIDTYPE>
    00165 //inline PRODUCT*
    00166 //TeAbstractFactory<PRODUCT,PARAMS,PRODUCTIDTYPE>::make(PARAMS* arg)
    00167 //{
    00168 //         // If there are no arguments or factory identification return the default object
    00169 //         if (!arg)
    00170 //                    return PRODUCT::DefaultObject();
    00171
    00172 //         PRODUCTIDTYPE productId = arg->getProductId();
    00173
    00174 //         // try to find the name on the factory dictionary
    00175 //         typename TeFactoryMap::iterator i = TeAbstractFactory<PRODUCT,PARAMS,PRODUCTIDTYPE>::instance().find(productId);
    00176
    00177 //         // Not found ?  return the Default Object
    00178 //         if (i == TeAbstractFactory<PRODUCT,PARAMS,PRODUCTIDTYPE>::instance().end())
    00179 //                    return PRODUCT::DefaultObject();
    00180
    00181 //         // Create an object, based on the input parameters
    00182 //                    return (*i).second->build(arg);
    00183 //}
    00184 #endif
    00185

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines