Original address: http://leihuang.org/2014/12/08/bridge/
Structural mode how to design the static structure between objects, how to complete the inheritance, implementation and dependency between objects, which is related to the system design is robust (robust): such as easy to understand, easy to maintain, easy to modify, low coupling and other issues. As the name of the Structural model, the pattern under its classification gives a variety of object relationship structures that can be applied in different situations.
- Default Adapter Mode
- Adapter mode
- Bridge mode
- Composite mode
- Decorator mode
- Facade mode
- Flyweight mode
- Proxy mode
Problem
Let's say there are some movies for users to choose from, such as action movies, romance movies, sci-fi movies ... They are also divided into domestic films, American films, Japanese and Korean films. What would you do if you were to implement a client that allows users to choose which category of movies to watch?
Are all the categories present? Or do you differentiate between two categories of different natures, and then two combinations? Obviously the latter. Because it's more concise. But when we're programmed, it's easy to write the first implementation. Like the following class structure diagram.
From the above class diagram, we can see that if there are more suspense movies now, then you need to add a subclass under each country class, if more than one country will need to implement the Imovie interface more. This leads to the whole involved very bloated.
So we're going to use the second scenario, the bridge pattern. We divide different regions into abstract categories, while action films, Love films, sci-fi films are divided into categories of implementation. The class diagram structure is as follows.
The line that is the aggregation relationship in the diagram is the bridge.
Achieve Big class
Public interface Imovieimplementor {public void Show (String region);//Play Movie}
Action movie Category
public class Actionmovie implements Imovieimplementor { Private String property = "action movie"; @Override public Void Show (String region) { System.out.println (region+ "--->" +property); }
Love movie Category
public class Lovemovie implements Imovieimplementor { Private String property = "Love movie"; @Override public Void Show (String region) { System.out.println (region+ "--->" +property); }
Sci-fi movie category
public class Sciencemovie implements Imovieimplementor { Private String property = "Sci-fi film"; @Override public Void Show (String region) { System.out.println (region+ "--->" +property); }
Abstract Big class
Public abstract class Regionabstract { private imovieimplementor movie = null; Protected regionabstract (Imovieimplementor movie) { this.movie = movie; } protected abstract void request (); Protected Imovieimplementor Getimp () { return movie; }}
Domestic movie Category
public class Chinesemovie extends Regionabstract { protected Chinesemovie (Imovieimplementor movie) { super ( movie); } @Override protected void request () { super.getimp (). Show ("Domestic");} }
American Movie Category
public class Americanmovie extends Regionabstract { protected Americanmovie (Imovieimplementor movie) { super ( movie); } @Override protected void request () { super.getimp (). Show ("United States");}
Japanese-Korean movie category
public class Japanesemovie extends Regionabstract { protected Japanesemovie (Imovieimplementor movie) { super ( movie); } @Override protected void request () { super.getimp (). Show ("Japanese-Korean");} }
Client class
public class Client {public static void Main (string[] args) { Imovieimplementor movieproperty = new Actionmovie () ; Selected action film regionabstract movieregion = new Chinesemovie (movieproperty);//Selected when the domestic movieregion.request (); Print: Homemade---> Action movie }}
A simple generalization of the bridge model is to divide all the classifications into two broad categories (one for the abstract category, one for the implementation category), and then to build a bridge between the two large classes, usually by aggregating relationships.
This article is also good, can see. dot here.
2014-12-08 23:08:53
Brave,happy,thanksgiving!
Design mode: Bridge mode