Quote Encyclopedia
Bridging mode separates the abstraction part from its implementation, so that they can all change independently. It is an object-structured pattern, also known as a handle (Handle and body) pattern or interface (Interface) pattern.
In software systems, some types, due to their own logic, have a change of two or more dimensions, so how to deal with this "multidimensional change". How to make use of object-oriented technology so that the type can easily change in many directions without introducing additional complexity. This is going to use bridge mode.
Applicability:
1 if a system needs to add more flexibility between the abstract and materialized roles of the component, avoid establishing static relationships between the two levels.
2 Any change in the design requirements to implement the role should not affect the client, or the implementation of changes to the role of the client is completely transparent.
3 an artifact has more than one abstract role and an implementation role, and the system needs dynamic coupling between them.
4 Although it is no problem to use inheritance in the system, the design requirements need to be managed independently because of the need for an independent change in the abstraction and materialization roles.
such as: the multi-level relationship between schools and students, a city can have many secondary schools, such as middle school, high school, each secondary school has male students, female students, if you want to describe a number of latitude students in class,
Namely: male students, female students in junior high school, the same male students, female students can also be in high school classes, as for their respective classes each student can play freely, so the above business can consider using
"Bridging mode" to implement.
The following relational structure:
Specific code:
1. Define Abstract
Public abstract class School {
Student Student;
Class public
void Lesson () {}
} public
abstract class Student {
//Learning public
Void study () {}
}
2, Concrete realization
public class HighSchool extends School {public
void Lesson () {
this.student.study ();
System.out.println ("High school Class!")
;
}
public class Middleschool extends School {public
void Lesson () {
this.student.study ();
System.out.println ("Middle school Class!");
}
public class Boystudent extends Student {public
Void study () {
System.out.print ("Male student \ T")
;
}
public class Girlstudent extends Student {public
Void study () {
System.out.print ("Female student \ T");
}
3, client clients
public class Client {
private static void study () {
School middle = new Middleschool ();
Middle.student = new Boystudent ();
Middle.lesson ();
Middle.student = new Girlstudent ();
Middle.lesson ();
School high = new HighSchool ();
High.student = new Boystudent ();
High.lesson ();
High.student = new Girlstudent ();
High.lesson ();
}
public static void Main (string[] args) {
study ();
}
}
Run
Male students in junior high school!
Female students in junior high school!
Male students high School Class!
Girls ' High school Class!
Bridging Mode (bridge) Description:
1. Bridge mode uses the "Combinatorial relationship between objects" to decouple the intrinsic binding relationship between abstraction and implementation, allowing abstractions and implementations to evolve along their respective dimensions.
2. The so-called abstract and implementation along the respective dimensions of the changes, that is, "subclass" they, get each subclass, you can arbitrarily them, so that different schools to achieve different students to do different things
3. Bridge mode is sometimes similar to multiple inheritance schemes, but multiple inheritance schemes often violate the single responsibility principle of a class (that is, a class has only one reason to change), and the reusability is relatively poor. Bridge mode is a better solution than multiple inheritance schemes.
4. The application of bridge mode is generally in "two very strong change dimensions", sometimes even if there are two different dimensions, but the change in a certain direction is not drastic--in other words, two changes will not lead to criss-cross, not necessarily use bridge mode.