JAVA基础(设计模式)-结构性模式

目录

0.结构性模式

 1.适配器模式

1.1.概念​

 1.2.代码

2.桥接模式

2.1.概念

 2.2.代码

3.代理模式

    3.1.静态代理模式:

  3.2.动态代理模式

     概念​

 invocationHandler接口​

proxy类

 代码


0.结构性模式

 1.适配器模式

1.1.概念

 

 

 1.2.代码

   

//适配者类:网线
public class Adaptee {

    public  void  net(){
        System.out.println("连接网线可以上网了!");
    }

}

 

//适配器接口(抽象适配器)
public interface Adapter {
    public  void getNet();
}
//类适配器:继承适配者类,调用net方法
public class Net extends Adaptee implements Adapter{
    @Override
    public void getNet() {
        super.net();
    }
}
//对象适配器:组合适配者类,调用net方法
public class Net2 implements Adapter{
    private  Adaptee adaptee;

    public Net2(Adaptee adaptee) {
        this.adaptee = adaptee;
    }

    @Override
    public void getNet(){
        adaptee.net();
    }
}
//客户类:电脑
public class Computer {
    public static void main(String[] args) {
        Computer computer=new Computer();
        Adaptee adaptee=new Adaptee();
        Adapter adapter1=new Net();
        Adapter adapter2=new Net2(adaptee);

        computer.linkNet(adapter1);
        computer.linkNet(adapter2);
    }

    public  void  linkNet(Adapter adapter){
        adapter.getNet();
    }
}

2.桥接模式

2.1.概念

 

 

 

 

 

 2.2.代码

//颜色类
public interface Color {
    void  color();
}
//具体颜色类
public class Red implements Color {
    @Override
    public void color() {
        System.out.print("红色");
    }
}
//具体颜色类
public class Yellow implements Color {
    @Override
    public void color() {
        System.out.print("黄色");
    }
}
//大小类
public abstract class Size {
    protected   Color color;

    public Size(Color color) {
        this.color = color;
    }

    public void size(){
        color.color();
    }

}

//具体大小类
class  Big extends  Size{


    public Big(Color color) {
        super(color);
    }

    @Override
    public void size() {
        color.color();
        System.out.print("大号");
    }
}

//具体大小类
class  Small extends  Size{


    public Small(Color color) {
        super(color);
    }

    @Override
    public void size() {
        color.color();
        System.out.print("小号");
    }
}
//测试类
//java访问属性:public,所有类都可以访问,protect:当前包下的所有类和所有子类都可以访问,defult:m默认当前包下的所有类可以访问,private:只有当前类可以访问;
public class main {
    public static void main(String[] args) {
        Size size1=new Big(new Yellow());
        Size size2=new Small(new Red());

        size1.size();
        size2.size();
    }
}

3.代理模式

    3.1.静态代理模式:

 

 

 

 

 

 

 

 代码:

//抽象角色类:结婚
public interface Marry {
    void  marry();
}
//真实角色类
public class True implements Marry{
    @Override
    public void marry() {
        System.out.println("要结婚了,好开心!");
    }
}
//代理角色:沟通真实角色和客户
public class Proxy implements Marry{

    private  True aTrue;

    public Proxy(True aTrue) {
        this.aTrue = aTrue;
    }

    @Override
    public void marry() {
        before();
        aTrue.marry();
        after();
    }

    public void  before(){
        System.out.println("筹备结婚典礼!");
    }
    public  void after(){
        System.out.println("收彩礼钱!");
    }


}
//测试类
public class main {
    public static void main(String[] args) {
        Marry marry=new Proxy(new True());
        marry.marry();
    }
}

3.2.动态代理模式

     概念

 invocationHandler接口

 

 

proxy类

 

 

 

 

 代码

//抽象角色类
public interface Service {
    void add();
    void update();
    void  delete();
    void select();
}
//真实角色类
public class ServiceImpl implements  Service{
    @Override
    public void add() {
        System.out.println("正在执行增加操作!");
    }

    @Override
    public void update() {
        System.out.println("正在执行更新操作!");
    }

    @Override
    public void delete() {
        System.out.println("正在执行删除操作!");
    }

    @Override
    public void select() {
        System.out.println("正在执行查询操作!");
    }
}

 

//代理类
public class Proxy implements InvocationHandler {

    private  Object target;

    public Object getTarget() {
        return target;
    }

    public void setTarget(Object target) {
        this.target = target;
    }

    //创建代理类:3个参数:代理类的类加载器,真实角色的内加载器,类加载器
   /* Foo f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(),
            new Class<?>[] { Foo.class },
            handler);*/

    public  Object getProxy(){
       return java.lang.reflect.Proxy.newProxyInstance(this.getClass().getClassLoader(),target.getClass().getInterfaces(),this);
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        msg("22");
        Object invoke = method.invoke(target, args);
        return invoke;
    }
    public  void  msg(String msg){
        System.out.println("开始");
    }


}
//测试类
public class main {
    public static void main(String[] args) {
        Service service=new ServiceImpl();
        Proxy proxy=new Proxy();
        proxy.setTarget(service);

        Service service2= (Service) proxy.getProxy();
        service2.add();
    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值