code manipulation : dynamic proxy(proxy pattern)

This commit is contained in:
haerong22
2021-02-21 22:12:00 +09:00
parent 705a4dcbd3
commit edc5bf337b
6 changed files with 103 additions and 0 deletions

View File

@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>dynamic-proxy</artifactId>
<version>1.0-SNAPSHOT</version>
<name>dynamic-proxy</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,24 @@
package org.example.proxy;
public class Book {
private Integer id;
private String title;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}

View File

@@ -0,0 +1,6 @@
package org.example.proxy;
public interface BookService {
void rent(Book book);
}

View File

@@ -0,0 +1,17 @@
package org.example.proxy;
public class BookServiceProxy implements BookService {
BookService bookService;
public BookServiceProxy(BookService bookService) {
this.bookService = bookService;
}
@Override
public void rent(Book book) {
System.out.println("로깅!!");
bookService.rent(book);
System.out.println("다른기능!!");
}
}

View File

@@ -0,0 +1,9 @@
package org.example.proxy;
public class DefaultBookService implements BookService{
@Override
public void rent(Book book) {
System.out.println("rent: " + book.getTitle());
}
}

View File

@@ -0,0 +1,17 @@
package org.example.proxy;
import org.junit.Test;
public class BookServiceTest {
BookService bookService = new BookServiceProxy(new DefaultBookService());
@Test
public void proxy() {
Book book = new Book();
book.setTitle("spring");
bookService.rent(book);
}
}