23 design patterns novella

Structural design patterns, 7 species

(1) bridge mode

Bridge Mode

The separated portion of abstraction and achieve better scalability, the bridge pattern dimension requirements correctly identified two independent system changes, and therefore its use has certain limitations

// abstract implementation class 
public  interface Memory { 

    void addMemory (); 
} 

// specific category 
public  class Memory6G the implements Memory { 

    @Override 
    public  void addMemory () { 
        the System. OUT .println ( " installed 6G memory " ); 
        
    } 

} 

// specific category 
public  class Memory8G the implements memory { 

    @Override 
    public  void addMemory () { 
        the System. OUT .println ( " installed 8G memory " );
    } 

} 

// abstract specific portion 
public  abstract  class Phone { 
    
    protected   Memory Memory; 

    public  void setMemory (Memory Memory) {
         the this .memory = Memory; 
    } 
    
    protected  abstract  void buyPhone (); 
} 

public  class Huiwei the extends Phone { 

    
    @Override 
    protected  void buyPhone () { 
        . the System OUT .println ( " later Huawei cell phone " ); 
        memory.addMemory (); 
        
    } 

} 

public  class Xiaomi extends  Phone {

    @Override
    protected void buyPhone() {
        System.out.println("购买小米手机");
        memory.addMemory();
    }

}

public class MainClass {

    public static void main(String[] args) {
        
        Phone  phone =   new Huiwei();
        phone.setMemory(new Memory6G());
        phone.buyPhone();
        System.out.println("===============");
        phone.setMemory(new Memory8G());
        phone.buyPhone();
        
    }
}

 

Guess you like

Origin www.cnblogs.com/moris5013/p/11577599.html