Bridegroom、aunty、bride的静态代理
时间: 2023-12-27 11:03:20 浏览: 119
高考英语单词3500分类记忆
静态代理是指在编译期间就已经确定好代理关系的代理方式。在这种代理方式中,代理对象与被代理对象需要实现同一个接口,代理对象负责将请求转发给被代理对象处理。
下面是Bridegroom、aunty、bride的静态代理的示例代码:
首先定义一个接口:
```java
public interface Marry {
void marry();
}
```
Bridegroom实现Marry接口:
```java
public class Bridegroom implements Marry {
@Override
public void marry() {
System.out.println("Bridegroom is ready to get married");
}
}
```
Aunty实现Marry接口并作为Bridegroom的代理类:
```java
public class Aunty implements Marry {
private Marry bridegroom;
public Aunty(Marry bridegroom) {
this.bridegroom = bridegroom;
}
@Override
public void marry() {
System.out.println("Aunty is helping the bridegroom to get ready");
bridegroom.marry();
}
}
```
Bride实现Marry接口并作为Bridegroom的代理类:
```java
public class Bride implements Marry {
private Marry bridegroom;
public Bride(Marry bridegroom) {
this.bridegroom = bridegroom;
}
@Override
public void marry() {
System.out.println("Bride is getting ready for the wedding");
bridegroom.marry();
}
}
```
客户端代码:
```java
public class Client {
public static void main(String[] args) {
Marry bridegroom = new Bridegroom();
Marry aunty = new Aunty(bridegroom);
Marry bride = new Bride(aunty);
bride.marry();
}
}
```
运行结果:
```
Bride is getting ready for the wedding
Aunty is helping the bridegroom to get ready
Bridegroom is ready to get married
```
阅读全文