win7iis配置网站,深圳市住房和建设局红色警示,泰安哪里做网站,网站中的文字滑动怎么做的目录 侨接设计模式最佳实践 侨接设计模式最佳实践
桥接模式是一种结构型设计模式#xff0c;它将抽象部分与它的实现部分分离#xff0c;使它们可以独立地变化。桥接模式是一种结构型设计模式#xff0c;它将抽象部分与它的实现部分分离#xff0c;使它们可以独立地变化。… 目录 侨接设计模式最佳实践 侨接设计模式最佳实践
桥接模式是一种结构型设计模式它将抽象部分与它的实现部分分离使它们可以独立地变化。桥接模式是一种结构型设计模式它将抽象部分与它的实现部分分离使它们可以独立地变化。在Spring框架中桥接模式通常用于处理复杂的类层次结构其中不同的实现可以动态地组合在一起。 #mermaid-svg-hcHfazpKfXZ9VGFK {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-hcHfazpKfXZ9VGFK .error-icon{fill:#552222;}#mermaid-svg-hcHfazpKfXZ9VGFK .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-hcHfazpKfXZ9VGFK .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-hcHfazpKfXZ9VGFK .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-hcHfazpKfXZ9VGFK .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-hcHfazpKfXZ9VGFK .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-hcHfazpKfXZ9VGFK .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-hcHfazpKfXZ9VGFK .marker{fill:#333333;stroke:#333333;}#mermaid-svg-hcHfazpKfXZ9VGFK .marker.cross{stroke:#333333;}#mermaid-svg-hcHfazpKfXZ9VGFK svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-hcHfazpKfXZ9VGFK g.classGroup text{fill:#9370DB;fill:#131300;stroke:none;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:10px;}#mermaid-svg-hcHfazpKfXZ9VGFK g.classGroup text .title{font-weight:bolder;}#mermaid-svg-hcHfazpKfXZ9VGFK .nodeLabel,#mermaid-svg-hcHfazpKfXZ9VGFK .edgeLabel{color:#131300;}#mermaid-svg-hcHfazpKfXZ9VGFK .edgeLabel .label rect{fill:#ECECFF;}#mermaid-svg-hcHfazpKfXZ9VGFK .label text{fill:#131300;}#mermaid-svg-hcHfazpKfXZ9VGFK .edgeLabel .label span{background:#ECECFF;}#mermaid-svg-hcHfazpKfXZ9VGFK .classTitle{font-weight:bolder;}#mermaid-svg-hcHfazpKfXZ9VGFK .node rect,#mermaid-svg-hcHfazpKfXZ9VGFK .node circle,#mermaid-svg-hcHfazpKfXZ9VGFK .node ellipse,#mermaid-svg-hcHfazpKfXZ9VGFK .node polygon,#mermaid-svg-hcHfazpKfXZ9VGFK .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-hcHfazpKfXZ9VGFK .divider{stroke:#9370DB;stroke:1;}#mermaid-svg-hcHfazpKfXZ9VGFK g.clickable{cursor:pointer;}#mermaid-svg-hcHfazpKfXZ9VGFK g.classGroup rect{fill:#ECECFF;stroke:#9370DB;}#mermaid-svg-hcHfazpKfXZ9VGFK g.classGroup line{stroke:#9370DB;stroke-width:1;}#mermaid-svg-hcHfazpKfXZ9VGFK .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#mermaid-svg-hcHfazpKfXZ9VGFK .classLabel .label{fill:#9370DB;font-size:10px;}#mermaid-svg-hcHfazpKfXZ9VGFK .relation{stroke:#333333;stroke-width:1;fill:none;}#mermaid-svg-hcHfazpKfXZ9VGFK .dashed-line{stroke-dasharray:3;}#mermaid-svg-hcHfazpKfXZ9VGFK #compositionStart,#mermaid-svg-hcHfazpKfXZ9VGFK .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-hcHfazpKfXZ9VGFK #compositionEnd,#mermaid-svg-hcHfazpKfXZ9VGFK .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-hcHfazpKfXZ9VGFK #dependencyStart,#mermaid-svg-hcHfazpKfXZ9VGFK .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-hcHfazpKfXZ9VGFK #dependencyStart,#mermaid-svg-hcHfazpKfXZ9VGFK .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-hcHfazpKfXZ9VGFK #extensionStart,#mermaid-svg-hcHfazpKfXZ9VGFK .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-hcHfazpKfXZ9VGFK #extensionEnd,#mermaid-svg-hcHfazpKfXZ9VGFK .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-hcHfazpKfXZ9VGFK #aggregationStart,#mermaid-svg-hcHfazpKfXZ9VGFK .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-hcHfazpKfXZ9VGFK #aggregationEnd,#mermaid-svg-hcHfazpKfXZ9VGFK .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-hcHfazpKfXZ9VGFK .edgeTerminals{font-size:11px;}#mermaid-svg-hcHfazpKfXZ9VGFK :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} implements extends «interface» Abstraction operate() RefinedAbstraction operate() «interface» Implementor implement() ConcreteImplementorA implement() ConcreteImplementorB implement() 应用demo 1. 定义实现化角色Implementor
桥接模式是一种结构型设计模式它将抽象部分与它的实现部分分离使它们可以独立地变化。桥接模式是一种结构型设计模式它将抽象部分与它的实现部分分离使它们可以独立地变化。在Spring框架中桥接模式通常用于处理复杂的类层次结构其中不同的实现可以动态地组合在一起。
这是实现化角色的接口定义了一些基本操作。
public interface Implementor {void operationImpl();
}2. 定义具体实现化角色ConcreteImplementor
这是实现化角色的具体实现。
public class ConcreteImplementorA implements Implementor {Overridepublic void operationImpl() {System.out.println(ConcreteImplementorA operation implemented.);}
}public class ConcreteImplementorB implements Implementor {Overridepublic void operationImpl() {System.out.println(ConcreteImplementorB operation implemented.);}
}定义抽象化角色Abstraction
这是抽象化角色的抽象类它持有一个对实现化角色的引用。
public abstract class Abstraction {protected Implementor implementor;public Abstraction(Implementor implementor) {this.implementor implementor;}public abstract void operation();
}4. 定义具体抽象化角色RefinedAbstraction
这是抽象化角色的具体实现它调用实现化角色中的操作。
public class RefinedAbstraction extends Abstraction {public RefinedAbstraction(Implementor implementor) {super(implementor);}Overridepublic void operation() {System.out.println(Operation of RefinedAbstraction.);implementor.operationImpl();}
}5. 客户端代码Client
这是使用桥接模式的客户端代码。
public class Client {public static void main(String[] args) {Implementor implementorA new ConcreteImplementorA();Abstraction abstractionA new RefinedAbstraction(implementorA);abstractionA.operation(); // 输出: Operation of RefinedAbstraction. ConcreteImplementorA operation implemented.Implementor implementorB new ConcreteImplementorB();Abstraction abstractionB new RefinedAbstraction(implementorB);abstractionB.operation(); // 输出: Operation of RefinedAbstraction. ConcreteImplementorB operation implemented.}
}在这个示例中Implementor 接口定义了实现化角色的基本操作而 ConcreteImplementorA 和 ConcreteImplementorB 是它的具体实现。Abstraction 抽象类定义了抽象化角色它持有一个 Implementor 类型的引用并通过构造器注入。RefinedAbstraction 是 Abstraction 的具体子类它实现了抽象方法 operation并在该方法中调用了实现化角色的操作。最后在客户端代码中我们创建了具体的实现化角色和抽象化角色并通过调用抽象化角色的 operation 方法来演示桥接模式的工作原理。
以下是一个使用桥接模式和Spring框架的示例
首先我们定义一个抽象的消息服务接口和它的实现
public interface MessageService {void sendMessage(String msg);
}public class EmailService implements MessageService {Overridepublic void sendMessage(String msg) {System.out.println(发送电子邮件 msg);}
}public class SMSService implements MessageService {Overridepublic void sendMessage(String msg) {System.out.println(发送短信 msg);}
}然后我们定义一个抽象的消息客户端类它包含一个对MessageService的引用
public abstract class MessageClient {protected MessageService messageService;public MessageClient(MessageService messageService) {this.messageService messageService;}public abstract void sendMessage(String msg);
}public class EmailClient extends MessageClient {public EmailClient(MessageService messageService) {super(messageService);}Overridepublic void sendMessage(String msg) {messageService.sendMessage(msg);}
}public class SMSClient extends MessageClient {public SMSClient(MessageService messageService) {super(messageService);}Overridepublic void sendMessage(String msg) {messageService.sendMessage(msg);}
}现在我们可以在Spring的配置类中注册这些bean
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;Configuration
public class AppConfig {Beanpublic MessageService emailService() {return new EmailService();}Beanpublic MessageService smsService() {return new SMSService();}Beanpublic MessageClient emailClient(MessageService emailService) {return new EmailClient(emailService);}Beanpublic MessageClient smsClient(MessageService smsService) {return new SMSClient(smsService);}
}最后我们可以在Spring的上下文中使用这些bean
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;Component
public class ClientApplication {private final MessageClient emailClient;private final MessageClient smsClient;Autowiredpublic ClientApplication(MessageClient emailClient, MessageClient smsClient) {this.emailClient emailClient;this.smsClient smsClient;}public void sendMessages() {emailClient.sendMessage(这是一封电子邮件);smsClient.sendMessage(这是一条短信);}
}