jav设计模式之代理模式

package com.huateng;
/**
 * 接口
 * @author Administrator
 *
 */
public interface Subject {
 public void request();
}
#############

package com.huateng;
/**
 * 被代理类
 * @author Administrator
 *
 */
public class RealSubject implements Subject{
 @Override
 public void request(){
  System.out.println("业务处理");
 }
}
#################

package com.huateng;

public class ProxySubject implements Subject{
 //代理的接口
 private Subject subject = null;
 //默认被代理者
 public ProxySubject(){
  subject = new RealSubject();
 }
 //通过构造函数传递被代理者
 public ProxySubject(Subject obj){
  subject =  (Subject)obj;
 }
 //实现接口中定义的方法
 @Override
 public void request() {
  //前
  before();
  this.subject.request();
  //后
  after();
 }
 public void before(){
  System.out.println("前");
 }
 public void after(){
  System.out.println("后");
 }
}

######################

package com.huateng;

public class 代理模式 {
 //RealSubject和ProxySubject实现同一个接口
 public static void main(String[] args) {
       Subject sub = new ProxySubject(new RealSubject());
       sub.request();
 }
}

 

### Wrapper Design Pattern in Java The Wrapper (or Adapter) design pattern is used to create compatibility between incompatible interfaces by wrapping one interface around another. This approach enables objects with incompatible interfaces to collaborate effectively. #### Implementation Example Below demonstrates how the wrapper pattern can be implemented within Java: ```java // Adaptee class which has a different interface from what the client expects. public class Banner { private String string; public Banner(String string){ this.string = string; } public void showWithParen() { System.out.println("(" + string + ")"); } public void showWithAster() { System.out.println("*" + string + "*"); } } // Target Interface that clients use. interface Print { void printWeak(); void printStrong(); } // The Adapter or Wrapper Class implementing the target interface while adapting the adaptee. class PrintBanner extends Banner implements Print { public PrintBanner(String str) { super(str); } @Override public void printWeak() { showWithParen(); } @Override public void printStrong(){ showWithAster(); } } ``` This code snippet shows how `PrintBanner` acts as an adapter for `Banner`, allowing it to conform to the `Print` interface expected by some clients[^5]. #### Use Cases One common scenario where the wrapper pattern shines involves integrating new components into legacy systems without altering existing structures significantly. For instance, suppose there exists older code expecting certain method signatures but newer libraries provide similar functionalities through differing APIs. By employing wrappers, seamless integration becomes feasible without extensive refactoring efforts. Another practical application appears when dealing with third-party services whose API contracts do not align perfectly with internal requirements. Instead of modifying core business logic layers directly, creating adapters ensures loose coupling alongside easier maintenance cycles down the line. --related questions-- 1. How does applying the wrapper pattern influence system architecture? 2. What benefits arise from utilizing established design patterns like the wrapper during development processes? 3. Can you elaborate on scenarios wherein avoiding direct modifications leads to better long-term project sustainability via usage of such patterns?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值