In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
GOF defines the bridge pattern in the "design pattern" as: separating the abstract part from its implementation part so that they can all change independently. The abstract and implementation parts here are not the relationship between the parent class and the subclass, the interface and the implementation class, but the composite relationship. That is, the implementation part is called by the abstract part to complete (implement) the function of the abstract part.
The intention of the bridge model is to "decouple Abstraction and Implementation so that they can change independently."
Abstraction:
The common conceptual connection that exists in multiple entities is abstraction.
Realization:
The concrete realization of abstraction is realization.
Decoupling:
Coupling is some kind of strong relationship between the behavior of two entities. Decoupling is to get rid of this strong correlation.
Strong associations are those that are determined at compile time and cannot be changed dynamically at run time.
Weak associations are those that can be determined dynamically and can be changed dynamically at run time.
In Java, inheritance relationships are strong associations, and combinatorial (aggregate) relationships are soft associations.
1. Composition:
1) Abstraction role: it defines the interface of the abstract class and maintains a reference to the implementation role.
2) RefinedAbstraction\ SpecializedAbstraction roles: implement and extend the interfaces defined by abstract roles.
3) Implementor role: the interface of the implementation class is given, and the interface here can be inconsistent with the interface in the abstract role. The implementing role should only give the underlying operation, while the abstract role should only give a higher level of operation based on the underlying operation.
4) concrete implementation (ConcreteImplementor) role: the concrete implementation of the role definition interface is given.
2. UML class diagram:
Third, the code description of class diagram (JAVA):
The following code comes from "Thinking in Patterns with Java":
/ / the abstract role of the abstract part
Class Abstraction {
/ / maintains a reference to the Implementor role
Private Implementation implementation
Public Abstraction (Implementation imp) {
Implementation = imp
}
/ / the following defines the interfaces that the abstract part should have
Public void service1 () {
/ / the existing interfaces in the implementation part are used
/ / combine to realize the function
Implementation.facility1 ()
Implementation.facility2 ()
}
Public void service2 () {
Implementation.facility2 ()
Implementation.facility3 ()
}
Public void service3 () {
Implementation.facility1 ()
Implementation.facility2 ()
Implementation.facility4 ()
}
/ / For use by subclasses:
Protected Implementation getImplementation () {
Return implementation
}
}
/ / precise abstract role of the abstract part
Class ClientService1 extends Abstraction {
Public ClientService1 (Implementation imp) {
Super (imp)
}
/ / use the methods provided by abstract roles to combine to complete a function
/ / this is why it is called precise abstract role (fix abstract role)
Public void serviceA () {
Service1 ()
Service2 ()
}
Public void serviceB () {
Service3 ()
}
}
/ / another precise abstract role, which has been omitted as above
Class ClientService2 extends Abstraction {
/ / here is to achieve certain functions directly through the implementation part of the method.
Public void serviceE () {
GetImplementation () .facility3 ()
}
}
/ / the implementation role of the implementation part
Interface Implementation {
/ / this interface only defines a certain interface
Void facility1 ()
Void facility2 ()
Void facility3 ()
Void facility4 ()
}
/ / the specific implementation role is to implement the interface provided by the implementation role
/ / and complete certain functions
/ / it is omitted here
Class Implementation1 implements Implementation {
.
}
Class Implementation2 implements Implementation {
.
}
.
There is also a feature in the above program: not only the interfaces provided by the implementation part and the abstract part can be completely different, but also the interfaces inside the implementation part and the abstract part can be completely different. But the implementation part needs to provide similar functionality.
4. Detailed code implementation:
Here, I use a specific example:
Now I want to draw, I want to draw Rectangle and Triangle shapes (Shape), but I also want to draw solid lines (SolidLine) and dashed lines (DotLine) (Line).
In this way, we can regard shapes as abstractions and lines as realizations; rectangles and triangles are corrective abstractions, and solid lines and dotted lines are concrete realizations.
JAVA:
Shape.java:
/ / abstract roles
Package abstraction
Import implementation.Line
Public class Shape {
Protected Line line
Protected int x1
Protected int y1
Protected int x2
Protected int y2
Public Shape () {}
Public Shape (Line line,int x1, int y1, int x2, int y2) {
This.line = line
This.x1 = x1
This.y1 = Y1
This.x2 = x2
This.y2 = Y2
}
Public void show () {
}
Public int calculateArea () {
Return 0
}
Public int calculatePerimeter () {
Return 0
}
}
Rectangle.java:
/ / fixed abstract roles
Package abstraction
Import implementation.Line
Public class Rectangle extends Shape {
Public Rectangle (Line line,int x1, int y1, int x2, int y2) {
Super (line, x1, y1, x2, y2)
}
Public void show () {
System.out.println ("This is an Rectangle!")
Line.draw (x1, y1, x2, y2)
}
Public int calculateArea () {
Return Math.abs ((x2-x1) * (y2-y1))
}
Public int calculatePerimeter () {
Return 2* (Math.abs (x2-x1) + Math.abs (y2-y1))
}
}
Triangle.java:
/ / fixed abstract roles
Package abstraction
Import implementation.Line
Public class Triangle extends Shape {
Public Triangle (Line line,int x1, int y1, int x2, int y2) {
Super (line, x1, y1, x2, y2)
}
Public void show () {
System.out.println ("This is an Triangle!")
Line.draw (x1, y1, x2, y2)
}
Public int calculateArea () {
Return Math.abs ((x2-x1) * (y2-y1)) / 2
}
Public int calculatePerimeter () {
Int a = Math.abs (x2-x1)
Int b = Math.abs (y2-y1)
Int c = (int) Math.sqrt (ahuma + bounb)
Return a + b + c
}
}
Line.java:
/ / realize the role
Package implementation
Public class Line {
Protected int x1
Protected int y1
Protected int x2
Protected int y2
Public void draw (int x1, int y1, int x2, int y2) {
}
}
DotLine.java:
/ / specific implementation of the role
Package implementation
Public class DotLine extends Line {
Protected int x1
Protected int y1
Protected int x2
Protected int y2
Public void draw (int x1, int y1, int x2, int y2) {
System.out.println ("drawSolidLine:" + x1+ "," + y1+ "," + x2+ "," + y2)
}
}
SolidLine.java:
/ / specific implementation of the role
Package implementation
Public class SolidLine extends Line {
Protected int x1
Protected int y1
Protected int x2
Protected int y2
Public void draw (int x1, int y1, int x2, int y2) {
System.out.println ("drawDotLine:" + x1+ "," + y1+ "," + x2+ "," + y2)
}
}
MainDraw.java:
/ / implementation
Import abstraction.*
Import implementation.*
Public class mainDraw {
/ * *
* @ param args
, /
Public static void main (String [] args) {
/ / TODO Auto-generated method stub
Int x1 # 0, y1 # 0, x2 # 1, y2 # 1
Line solidLine = new SolidLine ()
Line dotLine = new DotLine ()
Shape solidLineRectangle = new Rectangle (solidLine,x1, y1, x2, y2)
Shape dotLineRectangle = new Rectangle (dotLine,x1, y1, x2, y2)
Shape solidLineTriangle = new Triangle (solidLine,x1, y1, x2, y2)
Shape dotLineTriangle = new Triangle (dotLine,x1, y1, x2, y2)
System.out.println ("# #")
System.out.println ("solidLineRectangle:")
SolidLineRectangle.show ()
System.out.println ("area:" + solidLineRectangle.calculateArea ())
System.out.println ("perimeter:" + solidLineRectangle.calculatePerimeter ())
System.out.println ("# #")
System.out.println ("dotLineRectangle:")
DotLineRectangle.show ()
System.out.println ("area:" + dotLineRectangle.calculateArea ())
System.out.println ("perimeter:" + dotLineRectangle.calculatePerimeter ())
System.out.println ("# #")
System.out.println ("solidLineTriangle:")
SolidLineTriangle.show ()
System.out.println ("area:" + solidLineTriangle.calculateArea ())
System.out.println ("perimeter:" + solidLineTriangle.calculatePerimeter ())
System.out.println ("# #")
System.out.println ("dotLineTriangle:")
DotLineTriangle.show ()
System.out.println ("area:" + dotLineTriangle.calculateArea ())
System.out.println ("perimeter:" + dotLineTriangle.calculatePerimeter ())
}
}
Output result:
#
SolidLineRectangle:
This is an Rectangle!
DrawDotLine:0,0,1,1
Area:1
Perimeter:4
#
DotLineRectangle:
This is an Rectangle!
DrawSolidLine:0,0,1,1
Area:1
Perimeter:4
#
SolidLineTriangle:
This is an Triangle!
DrawDotLine:0,0,1,1
Area:0
Perimeter:3
#
DotLineTriangle:
This is an Triangle!
DrawSolidLine:0,0,1,1
Area:0
Perimeter:3
Caterpillar:
Shape.h (defined in the include package):
/ / abstract roles
# ifndef SHAPE_H_
# define SHAPE_H_
# include "lines.h"
# include
Using namespace std
Class Shape {
Public:
Shape () {}
Shape (Line & line,int x1, int y1, int x2, int y2) {
This- > line = & line
This- > x1 = x1
This- > y1 = y1
This- > x2 = x2
This- > y2 = y2
}
Virtual void show () = 0
Virtual int calculateArea () = 0
Virtual int calculatePerimeter () = 0
Virtual ~ Shape () {}
Protected:
Line* line
Int x1, y1, x2, y2
}
Class Rectangle: public Shape {
Public:
Rectangle (Line & line,int x1, int y1, int x2, int y2)
Int calculateArea ()
Int calculatePerimeter ()
Void show ()
Virtual ~ Rectangle () {}
Protected:
Int x1, y1, x2, y2
}
Class Triangle: public Shape {
Public:
Triangle (Line & line,int x1, int y1, int x2, int y2)
Void show ()
Int calculateArea ()
Int calculatePerimeter ()
Private:
Int x1, y1, x2, y2
}
# endif
Line.h (defined in the include package):
/ / realize the role
# ifndef LINES_H_
# define LINES_H_
Class Line {
Public:
Virtual void draw (int x1, int y1, int x2, int y2) = 0
Virtual ~ Line () {}
}
Class SolidLine:public Line {
Public:
Void draw (int x1, int y1, int x2, int y2)
Virtual ~ SolidLine () {}
}
Class DotLine:public Line {
Public:
Void draw (int x1, int y1, int x2, int y2)
Virtual ~ DotLine () {}
}
# endif / * LINES_H_ * /
Rectangle.cpp:
/ / fixed the implementation role
# include ".. / include/shape.h"
# include
Rectangle::Rectangle (Line & line,int x1, int y1, int x2, int y2) {
This- > line = & line
This- > x1 = x1
This- > y1 = y1
This- > x2 = x2
This- > y2 = y2
}
Int Rectangle::calculateArea () {
Return abs ((x2-x1) * (y2-y1))
}
Int Rectangle::calculatePerimeter () {
Return 2* (abs (x2-x1) + abs (y2-y1))
}
Void Rectangle::show () {
Cout line = & line
This- > x1 = x1
This- > y1 = y1
This- > x2 = x2
This- > y2 = y2
}
Int Triangle::calculateArea () {
Return abs ((x2-x1) * (y2-y1)) / 2
}
Int Triangle::calculatePerimeter () {
Int a = abs (x2-x1)
Int b = abs (y2-y1)
Int c = (int) sqrt (ahuma + bounb)
Return a + b + c
}
Void Triangle::show () {
Cout
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.