Talking about the model of JAVA design--Bridging mode (bridge) __java

Source: Internet
Author: User
转载请注明出处: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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.