Class pattern: Java Design Pattern 15th (Bridge pattern) class pattern

Source: Internet
Author: User

A small problem occurred in the recent application development process. By the way, record the cause and method-class mode.

I. What is the Bridge Connection Mode?

The bridge mode is also called the bridge connection mode. It is one of the construction design modes. The bridge mode is based on the most subtotal setting criterion of classes. It allows different classes to assume different responsibilities through application encapsulation, aggregation, inheritance, and other behaviors. The main feature of this architecture is to separate abstract actions from implementation, so that the independence of each part can be adhered to and the ability to respond to them can be expanded.

Ii. Structure of the Bridge Connection Mode

Iii. Roles and responsibilities of the Bridge Connection Mode

Client
Bridge Application

Invalid action
Abstract class interface (interface or abstract class)
Reference of implementor

Refined Response Action
Define action subclass

Implementor
Behavior implementation class interface (the lower action interface defines a higher layer operation based on the implementor port)

Concreteimplementor
Implementor subclass

CodeImplementation:

Implementor class:

 
Package com. qianyan. Bridge; public interface engine {/*** install mobilization engine */Public void installengine ();}

Concreteimplementor class:

 
Package com. qianyan. Bridge; public class engine2000 implements engine {@ overridepublic void installengine () {system. Out. println ("2000cc mobilization engine installed ");}}
Package com. qianyan. Bridge; public class engine2200 implements engine {@ overridepublic void installengine () {system. Out. println ("Install 2200cc mobilization engine ");}}

Invalid action class:

 
Package COM. qianyan. bridge; public abstract class car {private engine; Public Car (engine) {This. engine = engine;} public engine getengine () {return engine;} public void setengine (engine) {This. engine = engine;} public abstract void installengine ();}

Refined Response Action class:

 
Package COM. qianyan. bridge; public class bus extends car {Public Bus (engine) {super (ENGINE) ;}@ overridepublic void installengine () {system. out. print ("Bus:"); this. getengine (). installengine ();}}
Package COM. qianyan. bridge; public class jeep extends car {public Jeep (engine) {super (ENGINE) ;}@ overridepublic void installengine () {system. out. print ("jeep:"); this. getengine (). installengine ();}}

Client class:

 
Package COM. qianyan. bridge; public class mainclass {public static void main (string [] ARGs) {Engine engine2000 = new engine2000 (); Engine engine2200 = new engine2200 (); car bus = new bus (engine2000); bus. installengine (); car jeep = new Jeep (engine2200); jeep. installengine ();}}

Result:

 
Bus: 2000cc mobilization engine installed jeep: 2200cc mobilization engine installed

the article ends with the following joke quotes from Program : q:
A person driving a hot air balloon found him lost. He lowered the flight height and recognized a person on the ground. He continued to drop his height and shouted to the man, "Excuse me, can you tell me where I am ?"
the person below said, "yes. You are in a hot air balloon, hovering in the air of 30 feet ".
the person on the hot air balloon said, "You must do technical work in the IT department ".
"That's right," said the person on the ground. "How do you know ?"
"Haha", the person on the hot air balloon said, "everything you tell me is technically correct, but it is useless ".
the person on the ground said, "You must be from the management layer ".
"Yes," said the person on the hot air balloon. "But how do you know ?"
"Haha", the man on the ground said, "You don't know where you are, and you don't know where you are going. You always want me to help you. You were in the original place when you met us, but now I am wrong ".

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.