Thorough understanding of the factory model---analysis mediaplayerfactory

Source: Internet
Author: User

1 What is a Factory mode?

The following excerpt from Wikipedia

The factory method pattern (English: FactoryMethod pattern) is an object-oriented design pattern that implements the concept of "factory". Just as with other creation patterns, it is also a matter of working with objects created without specifying object-specific types. The essence of the factory method pattern is "to define an interface to create an object, but let the class implementing the interface decide which class to instantiate." The factory method defers instantiation of a class to a subclass. ”

Creating an object often requires complex procedures, so it is not suitable for inclusion in a composite object. Creating an object can result in a large amount of duplicated code that may require information that is not accessible to a composite object, may not provide enough level of abstraction, and may not be part of the composite object concept. The factory method pattern solves these problems by defining a separate method for creating objects. Subclasses implement this method to create objects of a specific type.

Some of the procedures in object creation include deciding which objects to create, managing the life cycle of objects, and managing the creation and destruction of specific objects.

2 from theory to reality

The Mediaplayerfactory in Android is the classic simple factory model, which is defined as:

Class Mediaplayerfactory {public:class ifactory {...   };   Static status_t Registerfactory (ifactory* factory, Player_type type);   static void Unregisterfactory (Player_type type); Private:   typedef keyedvector<player_type, ifactory*> Tfactorymap;   Static Tfactorymap Sfactorymap;};

Mediaplayerfactory has a map called Sfactorymap to store different kinds of factory, including Stagefrightplayerfactory,nuplayerfactory, Sonivoxplayerfactory,testplayerfactory, if there is a special need, you can also define a new factory to add in.

Each factory is implemented in Ifactory interfaces, the most important of which is scorefactory and Createplayer

   Class Ifactory {public     :       virtual ~ifactory () {}        virtual float scorefactory (const sp<imediaplayer> &/*client*/,                                   const char*/*url*/,                                   float/*curscore*/) {return 0.0;}        Virtual float scorefactory (const sp<imediaplayer>&/*client*/,                                   int/*fd*/,                                   int64_t/*offset*/,                                   int64_t/*length*/,                                   float/*curscore*/) {return 0.0;}        Virtual float scorefactory (const sp<imediaplayer>&/*client*/,                                   constsp<istreamsource> &/* source*/,                                   float/*curscore*/) {return 0.0;}        Virtual Sp<mediaplayerbase> createplayer () = 0;};


Applications typically call Mediaplayerfactory's Getplayertype function

Player_typemediaplayerfactory::getplayertype (const sp<imediaplayer>& Client,                                             const char* URL) {   Get_player_type_impl (client, URL);}


In Get_player_type_impl, the Sfactorymap is actually graded according to the URL of each factory, and then the factory with the highest score is obtained.

 #define GET_PLAYER_TYPE_IMPL (a ...)                         Mutex::autolock Lock_ (&slock);                  player_type ret = Stagefright_player;                                 float Bestscore = 0.0;                  for (size_t i = 0; i < sfactorymap.size (); ++i) {ifactory* v = sfactorymap.valueat (i);                                       float Thisscore;                                      CHECK (v! = NULL);             Thisscore = V->scorefactory (A, bestscore);                            if (Thisscore > Bestscore) {ret = Sfactorymap.keyat (i);                             Bestscore = Thisscore; }} if (0.0 = = Bestsco                      RE) {ret = Getdefaultplayertype (); } return ret; 

After getting the corresponding factory, call this it's Createplayer method to get the corresponding player

Class Stagefrightplayerfactory: Public   mediaplayerfactory::ifactory {public:virtual floatscorefactory (const SP <IMediaPlayer>&/*client*/,    }   virtual sp<mediaplayerbase> createplayer () {       ALOGV (" Create Stagefrightplayer ");       return new Stagefrightplayer ();    }};

While the player is initializing, it creates another really working player that is awesomeplayer.

Stagefrightplayer::stagefrightplayer ()    : MPlayer (new Awesomeplayer) {    alogv ("Stagefrightplayer");    Mplayer->setlistener (this);}

3 Summary

1. When the caller needs a product, pass a parameter directly to the factory and let the factory produce different products.

2. These products achieve the same interface.

3. Callers do not need to know the details, only need to ask (pass parameters) to the factory.

Thorough understanding of the factory model---analysis mediaplayerfactory

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.