当前位置:网站首页>Structural mode - bridging mode

Structural mode - bridging mode

2022-07-27 04:41:00 vbirdbest

One : Definition

Separate abstraction from implementation , So that they can change independently . It is realized by replacing inheritance relation with combination relation , Thus, the coupling degree of abstraction and Realization of these two variable latitudes is reduced .

Two : Case study

Develop a cross platform (Windows、Mac、Linux) Video player , It can play common video format files (rmvb、avi、mp4) etc. . The player contains two latitudes ( Cross platform latitude 、 File format latitude ), Suitable for use in bridging mode .

/** *  Video file ( Realize the role ) */
public interface VideoFile {
    
    void decode(String fileName);
}

/** * AVI Video file ( Specific implementation roles ) */
public class AviVideoFile implements VideoFile {
    
    @Override
    public void decode(String fileName) {
    
        System.out.println("AVI Video file :" + fileName);
    }
}

/** * rmvb Video file ( Specific implementation roles ) */
public class RmvbVideoFile implements VideoFile {
    
    @Override
    public void decode(String fileName) {
    
        System.out.println("RMVB Video file :" + fileName);
    }
}

/** *  Abstract operating system classes ( Abstract character ) */
public abstract class OperatingSystem {
    

    //  Declare video file references ( The most important thing )
    protected VideoFile videoFile;

    public OperatingSystem(VideoFile videoFile) {
    
        this.videoFile = videoFile;
    }

    //  Abstract method 
    public abstract void play(String fileName);
}



/** * Windows operating system ( Extend Abstract roles ) */
public class Windows extends OperatingSystem {
    
    public Windows(VideoFile videoFile) {
    
        super(videoFile);
    }

    @Override
    public void play(String fileName) {
    
        videoFile.decode(fileName);
    }
}



/** * Mac operating system ( Extend Abstract roles ) */
public class Mac extends OperatingSystem {
    
    public Mac(VideoFile videoFile) {
    
        super(videoFile);
    }

    @Override
    public void play(String fileName) {
    
        videoFile.decode(fileName);
    }
}

public class Client {
    
    public static void main(String[] args) {
    
        OperatingSystem os = new Mac(new AviVideoFile());
        os.play(" When the peach is ripe .avi");
    }
}

3、 ... and : benefits

  • The bridge mode improves the scalability of the system , Expand one of the two changing latitudes , No need to modify the original system . If a new one is added wmv File format , We only need to define another implementation class interface , Nothing else needs to change .
  • Make details transparent to customers .

Four : Use scenarios

  • When a class has two independent latitudes , And these two latitudes need to be expanded .
  • When a system does not want to use inheritance or the number of system classes increases dramatically because of multi-level inheritance .
  • When a system needs to add more flexibility between the abstract and concrete roles of components . Avoid creating static inheritance relationships between two levels , Bridging patterns enable them to establish an association at the abstraction level .

5、 ... and : law

  • Draw an interface for a latitude .
  • Abstract another latitude into abstract classes , And reference the interface of the previous latitude , Assign a value to the reference through the constructor .

The so-called bridge in bridge mode , The two ends of the bridge are connected to each other , That is, let the two ends combine at will .
It is mainly used for arbitrary combination of two latitudes . In fact, an abstract class holds a reference to another interface .

原网站

版权声明
本文为[vbirdbest]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/208/202207270346298366.html