转载请注明出处:http://blog.csdn.net/l1028386804/article/details/45457969
I. Overview
Separates the abstract part from its implementation, so that they can all change independently. Second, applicability
1. You do not want to have a fixed binding relationship between the abstraction and its implementation part. For example, this may be because the implementation part of the program runtime should be selectable or switched.
2. The abstraction of a class and its implementation should be augmented by the method of generating subclasses. In this case, bridge mode allows you to combine different abstract interfaces and implementation parts and expand them separately.
3. Modifications to an abstract implementation component should not affect the customer, that is, the customer's code does not have to be recompiled.
4. As shown in the first class diagram of the intent section, there are many classes to build. Such a class hierarchy means that you have to break an object into two parts. 5. You want to share the implementation across multiple objects (possibly using reference counting), but ask the customer not to know this. Third, participants
1.Abstraction
Defines an interface for an abstract class. Maintains a pointer to an object of type Implementor.
2.RefinedAbstraction
Expands the interfaces defined by abstraction.
3.Implementor
Defines an interface for the implementation class that does not necessarily have to be exactly the same as the abstraction interface. In fact, the two interfaces can be completely different. Generally speaking, the Implementor interface provides only basic operations, while abstraction defines a higher level of operation based on these basic operations.
4.ConcreteImplementor
Implement the Implementor interface and define its specific implementation. four, class diagram
v. Examples
Abstraction
Package Com.lyz.design.bridge;
/**
* Define Abstraction Person class
* @author liuyazhuang
* * * */Public
abstract class Person {
private Clothing clothing;
Private String type;
Public clothing getclothing () {return
clothing;
}
public void setclothing (clothing clothing) {
this.clothing = clothing;
}
public void SetType (String type) {
this.type = type;
}
Public String GetType () {return
this.type;
}
public abstract void dress ();
}
refinedabstraction
Package Com.lyz.design.bridge;
/**
* Refinedabstraction class man
* @author Liuyazhuang * * * * * * */public
class man
extends
Public Mans () {
setType ("man");
}
public void dress () {
Clothing clothing = getclothing ();
Clothing.persondresscloth (this);
}
Package Com.lyz.design.bridge;
/**
* Define Refinedabstraction class Lady
* @author Liuyazhuang
* * * * * * * * */public
class Lady extends person { Public
Lady () {
setType ("woman");
}
public void dress () {
Clothing clothing = getclothing ();
Clothing.persondresscloth (this);
}
implementor
Package Com.lyz.design.bridge;
/**
* defines implementor class clothing * @author Liuyazhuang * * * * */public
abstract class Clothing { Public
abstract void Persondresscloth (person person);
}
Concreteimplementor
Package Com.lyz.design.bridge;
/**
* defines concreteimplementor class jacket
* @author Liuyazhuang * */Public
class Jacket extends Clothing {public
void Persondresscloth (person person) {
System.out.println (Person.gettype () + "vest");
}
}
Package Com.lyz.design.bridge;
/**
* defines Concreteimplementor class Trouser
* @author Liuyazhuang * */Public
class Trouser extends Clothing {public
void Persondresscloth (person person) {
System.out.println (Person.gettype () + pants);
}
}
Test
Package Com.lyz.design.bridge;
/**
* Test class
* @author liuyazhuang
* * *
/public class Test {public
static void Main (string[] args) { Person
mans = New Man ();
Lady of the person = The New Lady ();
Clothing jacket = new Jacket ();
Clothing trouser = new trouser ();
Jacket.persondresscloth (mans);
Trouser.persondresscloth (mans);
Jacket.persondresscloth (lady);
Trouser.persondresscloth (Lady);
}
Result
Men wear vest
men wear trousers
women wear
Pants