Difference between Bridge pattern and Adapter pattern

后端 未结 9 1049
清歌不尽
清歌不尽 2020-11-29 19:16

What is the difference between the Bridge and Adapter patterns?

9条回答
  •  醉酒成梦
    2020-11-29 19:42

    There are plenty of answers To distinguish between Adapter and Bridge. But as ppl are looking for code examples, I would give one example for Adapter Design Pattern crafted into a timeline story :

        //---------------------------------------External Vendor/Provider--------------------------------
       
                //Adaptee | RussianTankInterface is adaptee | adaptee lives in is own lala land and do not care about any other class or interface
                RussianTankInterface smerch9K58 = new RussianTank("The Russian Artillery bought by India in October 2015");
                    smerch9K58.aboutMyself();
                    smerch9K58.stuff();
                    smerch9K58.rotate();
                    smerch9K58.launch();
            
       
        //---------------------------------2016 : India manufactures Bharat52 ------------------------------ 
        
                //Client_1 :: IndianTank 
                EnemyAttacker bharat52Attacker = new IndianTank("Tank built in India delivered to Army in Jul 2016"); 
       
                // behaves normally -------------------------(1)
                    bharat52Attacker.aboutMe();
                    bharat52Attacker.load();
                    bharat52Attacker.revolve();
                    bharat52Attacker.fireArtillery();
                    
        //---------------------------------2019 : India mnufactures Pinaka, and thought about fusion with Russian technology - so adaption required ------------------------------ 
                      
                //Client_2 :: IndianTank 
                EnemyAttacker pinakaAttacker = new IndianTank("Tank built in India in 1998 got upgraded_v1 in 9 Sep 2019"); 
                
                
                
    #####----Bilateral-Coalition happens----## 
    #####       India  : I want a fusion artillery technology with 
    #####                       1) Indian materials and brain-power but
    #####                       2) Russian machine-parts-movement technology 
    #####       Russia : Give me your Interface - at max we can help by providing an Adapter 
        
        //---------------------------------------External Vendor/Provider-----------------------------------
        
                //Adapter :: RussianTechnologyAdapter | Russia gets EnemyAttacker interface only from India & creates RussianTechnologyAdapter 
                RussianTechnologyAdapter russianTechnologyAdapter = new RussianTechnologyAdapter(smerch9K58);
               
               
                //Target | EnemyAttacker was initially ClientInterface but later becomes the Target as story evolves | <- client owns this Interface
                EnemyAttacker dhanushAttacker = russianTechnologyAdapter;
                
    #####----Russia keeps her Word----## 
    #####       Russia to India : Here you go! Take Dhanush, a wrapper over our encapsulated adapter, and plug-in anything conforming to your EnemyAttacker.
    #####       India : Thanks a lot!
    
        //--------------------------------- 2020 : India returns back happily with dhanushAttacker--------------------------------------- 
                
                //Client_2 - adapted behavior -------------------------(2)
                    dhanushAttacker.setNavigationCapability(pinakaAttacker.getCuttingEdgeNavigableTargets());
                    dhanushAttacker.aboutMe(); //calls RussianInstance -> aboutMyself()
                    dhanushAttacker.load();    //calls RussianInstance -> stuff()
                    dhanushAttacker.revolve(); //calls RussianInstance -> rotate()
                    dhanushAttacker.fireArtillery();  //calls RussianInstance -> launch()
                           
              
    

    Carefully notice :

    • Same API in (1) & (2) behaves differently
    • India can push its brain into wireFrame of Russia e.g dhanushAttacker.setNavigationCapability(pinakaAttacker.get(..))

    Noteworthy points

    Client side owns:

    -  Invoker /Use(uses Adapter later point)/Client
    -  ClientInterface (a.k.a Target )
    

    Shared later:

    - ClientInterface  ( becomes Target after sharing)
    

    Receiver side owns :

    - Adapter (later shared directly or as a wrapper )  
    - Adaptee
    
         
    

    Hoping someone gives an inline for Bridge too :)

提交回复
热议问题