`

pattern study

阅读更多
动态代理模式
可以在运行时期动态的创建代理类。
需要:被代理类,被代理类接口,织入器和InvocationHandler
通过织入器使用接口反射成代理类。
我们调用动态代理
package proxy;

import java.lang.reflect.Proxy;

/**
 *
 * @author liuquan
 */
public class Test {
    
    public static void main(String args[]){
        HelloHandler handler = new HelloHandler(new HelloImpl());
        ClassLoader classLoader = Hello.class.getClassLoader();
        Class[] proxyInterface = new Class[]{Hello.class};
        Hello hello = (Hello) Proxy.newProxyInstance(classLoader, proxyInterface, handler);
        hello.sayHello();
    }
}

ClassLoader当前工程的,所以你用Hello.class.getClassLoader(),还是用Test.class.getClassLoader()都一样。

模拟代理类
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package proxy;

import java.lang.reflect.Method;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author liuquan
 */
public class ProxyTest implements Hello {

    private HelloHandler handler;

    public ProxyTest(HelloHandler handler) {
        this.handler = handler;
    }

    @Override
    public void sayHello() {
        try {
            Method method = (handler.hello).getClass().getMethod("sayHello", null);
            handler.invoke(this, method, null);
        } catch (Throwable ex) {
            Logger.getLogger(ProxyTest.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public static void main(String args[]){
        HelloHandler helloHandler = new HelloHandler(new HelloImpl());
        new ProxyTest(helloHandler).sayHello();
    }
}

Proxy就是织入器,通过newProxyInstance构建代理类。看传的三个参数ClassLoader,Interface,InvocationHandler。ClassLoader检测当前类加载器的缓存里面是否有代理类,没有则生成代理类并缓存到本地的JVM,所以要用到ClassLoader将字节码加载到内存中,Interface是代理类要实现的,而Handler在代理类里面要调用的。通过模拟的代理类,很清楚的知道动态代理的调用了。
以前说动态代理必须是对接口操作的,不明白为什么,现在看到代理类就明白了。


Adapter适配器模式,把一个类的接口,变成客户端所期待的另一种接口,使得因为不匹配而无法在一起工作的二个类能够在一起工作。简单点来说,源类中有一个方法,客户要加一个方法。于是adapter类,既有源类的方法,也有客户端要加的方法。
主要的类:目标接口一个,客户所期望的接口(源的方法和客户要加的方法)。
          源类一个
          adapter类一个:有2种实现,一种继承源类,实现目标接口。第二种实现目标接口,持有源类引用,重写方法。
package pattern.adaptor;

/**
 *
 * @author liuquan
 */
public interface Target {
    
    public void openDoor();
    
    public void openWindow();
    
}

package pattern.adaptor;

/**
 *
 * @author liuquan
 */
public class Adaptee {
    
    public void openDoor(){
        System.out.println("open the Door");
    }
    
}

package pattern.adaptor;

/**
 *
 * @author liuquan
 */
public class Adaptor extends Adaptee implements Target{

    @Override
    public void openWindow() {
        System.out.println("open the window");
    }
    
}


or

package pattern.adaptor;

/**
 *
 * @author liuquan
 */
public class Adaptor2 implements Target{
    
    private Adaptee adaptee;
    
    public Adaptor2(Adaptee adaptee){
        this.adaptee = adaptee;
    }

    @Override
    public void openDoor() {
       adaptee.openDoor();
    }

    @Override
    public void openWindow() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
}

适配器感觉就是把多个动能糅合在一个类里面,如变压器,既可以转压成110V,也可以转压成220V。
分享到:
评论

相关推荐

    Statistical Pattern Recognition, Second Edition

    Statistical pattern recognition is a very active area of study and research, which has seen many advances in recent years. New and emerging applications - such as data mining, web searching, ...

    Manning_Bitter_Java.zip_AntiPatterns_clear

    understandable language, and the philosophy of striving to understand antipatterns.Advanced programmers will find some new antipatterns for XML, and will also benefit from a new angle of pattern study.

    Statistical Pattern Recognition (2nd Ed.)

    Statistical pattern recognition is a very active area of study and research, which has seen many advances in recent years. New and emerging applications – such as data mining, web searching, ...

    Statistical Pattern Recognition

    Statistical pattern recognition is a very active area of study and research, which has seen many advances in recent years. New and emerging applications - such as data mining, web searching, ...

    study-oop-pattern

    学习模式 用您的公司帐户名创建一个文件夹并编写您自己的文件 子文件夹可以根据自己的喜好自由组织。 学习后创建分支,编写文件,发送公关并合并自己 e.g. ├── juwon.kim │ ├── ch01.md ...

    design-pattern-study:设计模式研究

    这个压缩包文件"design-pattern-study"显然是一个关于设计模式学习的资源,可能包含了各种设计模式的实现示例,以Java语言为主。 设计模式分为三大类:创建型、结构型和行为型。每一种类型都有其特定的用途和适用...

    design-pattern-study:设计模式学习代码

    "design-pattern-study"项目显然是一个专注于设计模式学习和实践的代码库。通过分析其中的代码,我们可以深入理解设计模式的原理和应用。在这个项目中,"design-pattern-study-main"可能包含了不同设计模式的实例...

    FIgmaPatternStudy:尝试在httmlcss的帮助下创建网站

    Figma通常用于设计界面和原型,而这里的“Pattern Study”指的是作者试图将Figma的设计转化为实际的网页。HTML(HyperText Markup Language)是网页的基础结构语言,而CSS(Cascading Style Sheets)则负责控制页面...

    Pattern Oriented Software Architecture Volume 3 - Patterns for Resource

    ### Pattern Oriented Software Architecture Volume 3 - Patterns for Resource Management #### 标题解析与核心概念: 本书名为《面向模式的软件架构:资源管理模式》,是该系列书籍的第三卷,由Michael Kircher ...

    A Study on Workers' Pattern of Space Use in Workplaces by Using Sensor Network

    本篇研究文献的标题为《利用传感器网络研究办公空间中工作人员空间使用模式》,作者为张昕楠。研究聚焦在知识生产型办公环境中工作人员的空间使用行为。该研究的主要目的是通过分析知识工作活动与工作场所之间的相互...

    Study of pattern and products of salting out in treating mustard tuber wastewater by mixed hybrid membrane bioreactor

    复合生物膜反应器(Membrane Bioreactor, MBR)是近年来污水处理领域中发展迅速的一种技术,其特点是将微生物降解有机物与膜分离技术相结合,具有出水水质好、占地面积小、污泥产率低等优点。本文研究了榨菜废水处理...

    论文研究-Performance Study on RF Pattern Matching Positioning with Additional Inter-RAT Measurements.pdf

    多接入技术参数辅助的射频模式匹配定位技术性能研究,李伊婕,崔杰,本文提出了一种新的定位方法--射频模式匹配(RFPM),也被称为指纹,是第三代合作伙伴计划(3GPP)RAN4会议提出的一种定位技术。...

    Pattern-Oriented Software Architecture,Volume 3

    The first volume of the POSA pattern series introduced a broad-spectrum of general-purpose patterns in software design and architecture. The second narrowed the focus to fundamental patterns for ...

    Proxy Pattern Informatization Research Based On SaaS

    Abstract—Based on demands in proxy pattern, this article proposes informatization solution of proxy pattern which includes enterprise management, proxy management, franchisee management and supply ...

    study

    3. **Pattern Matching for instanceof (Preview)**:增强了`instanceof`关键字的功能,允许模式匹配,使得类型检查更加简洁和安全。 4. **JEP 382: macOS on Apple Silicon**:支持Apple的M1芯片,为Mac用户提供了...

    mot-tf-codepattern-study:MoT 2018 TF模式设计研究

    - TF fundamental pattern design (Jaewook Kang) - tf.layer, tf.estimator, tf.train, tf.test, TFRcord revisit (JoonHo Lee) - TF 추상화와 간소화, 모델 엑스포트와 서빙 (Gyubok Lee) - Recurrent Neural ...

    pattern classification chapter3 answer

    Based on the provided information from "Pattern Classification Chapter 3 Answer," ... These concepts form the basis for more advanced topics in the field and provide a solid foundation for further study.

    Go Heart Study跟随心动

    <url-pattern>/*</url-pattern> <dispatcher>REQUEST <listener-class>net.bull.javamelody.SessionListener</listener-class> ``` - **测试监控页面:** - 配置完成后,可以通过访问特定URL来查看...

    java源码中的设计模式-design_pattern_study:该项目包含在GOF设计模式研究期间进行的练习的源代码。Udemy编写的Ja

    本项目"design_pattern_study"正是一个针对GOF(Gang of Four,即《设计模式:可复用面向对象软件的基础》一书的四位作者)所提出的23种经典设计模式的实践研究。 GOF设计模式分为三大类:创建型、结构型和行为型。...

    DesignPattern-539

    Design PatternThis project is my study and implementation of Design Pattern.For each design pattern, there will be slides and several examples.I use three resources:《大话设计模式》通篇都是以情景对话...

Global site tag (gtag.js) - Google Analytics