zoukankan      html  css  js  c++  java
  • 设计模式适配器模式

    类图

     

    源码

    package adapter;
    
    public interface Cat {
        public void requestCat();
        public void miao();
    }
    
    Dog.java
    package adapter;
    
    public interface Dog {
        public void requestDog();
        public void wang();
    }
    
    ClassAdapterDog.java
    package adapter;
    
    public class ClassAdapterDog extends Adaptee implements Dog{
        public void requestDog()    
        {        
            specificCat();    
        }
    
        @Override
        public void wang() {
            // TODO 自动生成的方法存根
            System.out.println("狗汪汪叫!");
        }
    }
    ClassAdapterCat.java
    package adapter;
    
    public class ClassAdapterCat extends Adaptee implements Cat{
        public void requestCat()    
        {        
            specificDog();    
        }
    
        @Override
        public void miao() {
            // TODO 自动生成的方法存根
            System.out.println("猫抓老鼠!");
        }
    }
    
    Adaptee.java
    package adapter;
    
    public class Adaptee {
        public void specificCat()    
        {               
            System.out.println("猫学:");
        }
        public void specificDog()    
        {               
            System.out.println("狗学:");
        }  
    }
    
    ClassAdapterTest.java
    package adapter;
    
    public class ClassAdapterTest {
        public static void main(String[] args)    
        {        
            System.out.println("测试:");        
            Dog target = new ClassAdapterDog();        
            target.requestDog();
            target.wang();
            Cat target1 = new ClassAdapterCat();
            target1.requestCat();
            target1.miao();
        }
    }

    测试截图

     

  • 相关阅读:
    PHP
    思科模拟器
    路由器
    服务器
    Windows Server 2008 笔记【瞎写】
    Day1 T3 数列
    java中自定义excel模板并且填充内容
    springMVC接收值list时,超过256出现IndexOutOfBoundsException
    java将日期转换成周几
    两个tomcat配置各自的SSL证书(前后端分离)
  • 原文地址:https://www.cnblogs.com/lx06/p/15688390.html
Copyright © 2011-2022 走看看