A pluggable adapter is an adapter that adapts dynamically to one of several classes. Of course, the adapter can adapt only to classes that it can recognize, and usually the adapter decides which class it is adapting to based on differing constructors or setParameter methods.
Java has yet another way for adapters to recognize which of several classes it must adapt to: reflection. You can use reflection to discover the names of public methods and their parameters for any class. For example, for any arbitrary object you can use the getClass method to obtain its class and the getMethods method to obtain an array of the method names.
java 代码
-
-
-
- class Pathfinder
- {
- public void explore()
- {
- System.out.println("explore");
- }
- }
-
-
-
-
- class Digger
- {
- public void dig()
- {
- System.out.println("Dig");
- }
- }
-
-
-
-
- class Collector
- {
- public void collect()
- {
- System.out.println("collect");
- }
- }
-
-
-
-
- class PluggablePathfinderAdapter
- {
- private Pathfinder pathfinder;
- private HashMap map=new HashMap();
-
- PluggablePathfinderAdapter(Pathfinder pathfinder)
- {
- this.pathfinder=pathfinder;
- }
-
- public void adapt(String classname,String methodname)
- {
- try
- {
-
- Class _class=Class.forName(classname);
- Method method=_class.getMethod(methodname,null);
- map.put(classname,method);
- }catch(ClassNotFoundException cnfe)
- {
- cnfe.printStackTrace();
- }catch(NoSuchMethodException nsme)
- {
- nsme.printStackTrace();
- }
- }
-
- public void explore(String classname)
- {
- try
- {
- pathfinder.explore();
- map.get(classname).invoke(Class.forName(classname).newInstance(),null);
- }catch(Exception e)
- {
- e.printStackTrace();
- }
-
- }
- }
java 代码
- public class PluggableAdapterDemo
- {
-
- public PluggableAdapterDemo()
- {
-
- }
-
- public static void main(String[] args)
- {
- Pathfinder pathfinder=new Pathfinder();
- PluggablePathfinderAdapter adapter=new PluggablePathfinderAdapter(pathfinder);
- adapter.adapt("Digger","dig");
- adapter.adapt("Collector","collect");
- adapter.explore("Digger");
- }
-
- }
分享到:
相关推荐
主要是介绍各种格式流行的软件设计模式,对于程序员的进一步提升起推进作用,有时间可以随便翻翻~~ 23种设计模式汇集 如果你还不了解设计模式是什么的话? 那就先看设计模式引言 ! 学习 GoF 设计模式的重要性 ...
Pluggable Factory part1
Asynchronous Pluggable Protocol使用例子
ABB Pluggable OVR-Multi-Pole样本pdf,ABB Pluggable OVR-Multi-Pole样本
Oracle 12c 新特性介绍,pluggable database的一些说明和使用
Asynchronous Pluggable Protocol异步可插的协议(9KB)
Pluggable Factory part 2
解决Linux MySQL缺失依赖包 perl-Module-Pluggable-3.90-141.el6_7.1.x86_64.rpm
ORA-65122_ Pluggable database GUID conflicts with the GUID of an existing_ITPUB博客.mhtml
win-acme是一款免费SSL证书自动获取和部署工具,由于官方下载慢,特提供Csdn版本供使用. 具体的使用教程可看我收集的好文地址https://blog.csdn.net/r657225738/article/details/103778925
This specification defines a 28 Gb/s QSFP+ pluggable transceiver solution popularly known as QSFP28. It gathers the appropriate/unique Base Electrical, Optical, Common Management, Module/Plug ...
QSFP 模块 MSA 标准, 可插拔QSFP收发模块标准和Specification文件。
win-acme是一款免费SSL证书自动获取和部署工具,非常难下载,托朋友下载的,资源宝贵,具体的使用教程可看我收集的好文地址https://blog.csdn.net/r657225738/article/details/103778925
Structured, pluggable logging for Go.
This document defines the low speed electrical and management interface specifications for enhanced Small Form Factor Pluggable(SFP+) modules and hosts. The SFP+ module is a hot pluggable small ...
Cisco Small Form-Factor Pluggable Modules for Gigabit Ethernet Applications The industry-standard Cisco® Small Form-Factor Pluggable (SFP) Gigabit Interface Converter is a hot-swappable input/output ...
win-acme是一款免费SSL证书自动获取和部署工具,由于官方下载慢,特提供Csdn版本供使用. 具体的使用教程可看我收集的好文地址https://blog.csdn.net/r657225738/article/details/103778925
借助React Pluggable,我们可以将我们的应用视为一组功能,而不是一组组件。 它提供了解决此问题的混合方法。 我们已经在大型复杂的应用程序(例如使用了React Pluggable,以随着时间的推移添加独立的和从属的功能...
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
离线安装包,亲测可用