标签:
public class AtFirst {
public static void main(String[] args) {
EmployeeABuy. Abuy();
//报销
EmployeeBBuy. Bbuy();
//报销
}
}
class EmployeeABuy{
public static void Abuy(){
System. out.println( "A买了一张");
}
}
class EmployeeBBuy{
public static void Bbuy(){
System. out.println( "B买了一张");
}
}
//行政部-适配器
public class Adapter implements BuyTicket {
RDBuy rdBuy=new RDBuy();
@Override
public void Buy(String name) {
rdBuy.Buy( name);
}
}
interface BuyTicket{
public void Buy(String name);
}
class RDBuy implements BuyTicket{
@Override
public void Buy(String name) {
System. out.println( "研发部帮 " +name +"买了一张火车票" );
}
}
public static void main(String[] args) {
String emploeeName= "研发部-老王" ;
BuyTicket buyer= new Adapter();
buyer.Buy( emploeeName);
}
public class Adapter implements BuyTicket {
public static void main(String[] args) {
String emploeeName= "研发部-老王" ;
BuyTicket buyer= new Adapter();
buyer.Buy( emploeeName);
}
private AdministrationBuy administraionBuy= new AdministrationBuy(); //
RDBuy rdBuy=new RDBuy();
@Override
public void Buy(String name) {
if ( name.contains( "研发")) {
rdBuy.Buy( name);
}
if ( name.contains( "行政")) {
administraionBuy.Buy( name);
}
}
}
interface BuyTicket{
public void Buy(String name);
}
//行政部
class AdministrationBuy implements BuyTicket{
@Override
public void Buy(String name) {
System. out.println( "行政部帮 " +name +"买了一张火车票" );
}
}
//研发部
class RDBuy implements BuyTicket{
@Override
public void Buy(String name) {
System. out.println( "研发部帮 " +name +"买了一张火车票" );
}
}
interface BuyTicket{
public void Buy(String name);
}
class CompanyBuy implements BuyTicket{
@Override
public void Buy(String name) {
System. out.println( name+ "买了一张火车票" );
}
}
//行政部来审核
class AdministraionPoxy implements BuyTicket{
BuyTicket buyTicket;
public AdministraionPoxy(BuyTicket buyTicket){
this. buyTicket= buyTicket;
}
@Override
public void Buy(String name) {
System. out.println( "审核...");
buyTicket.Buy( name);
System. out.println( "记录...");
}
}
public class Proxy {
public static void main(String[] args) {
BuyTicket employee= new CompanyBuy();
BuyTicket company= new AdministraionPoxy( employee);
company.Buy( "EmployeeABuy");
company.Buy( "EmployeeABuy");
}
}
interface BuyTicket{
public void Buy(String name);
}
class CompanyBuy implements BuyTicket{
@Override
public void Buy(String name) {
System. out.println( "经过一系列手续···" +name +"买票了" );
}
}
class AdministraionHandler implements InvocationHandler {
private Object object;
public AdministraionHandler(Object object){
this. object= object;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
Object result= null;
System. out.println( "内部审核...");
result= method.invoke( object, args);
System. out.println( "内部记录...");
return result;
}
}
public static void main(String[] args) {
BuyTicket buyer= new CompanyBuy();
InvocationHandler handler= new AdministraionHandler( buyer);
BuyTicket proxy=(BuyTicket)Proxy. newProxyInstance(buyer.getClass().getClassLoader(),
buyer.getClass().getInterfaces(), handler);
proxy.Buy( "老王");
}
标签:
原文地址:http://www.cnblogs.com/wanglao/p/5307809.html