面向对象高级(四)----工厂模式,代理模式,适配器模式

  1. 接口的实际应用------制定标准
代码如下
package lianxijihe;
interface USB{
	void start();
	void stop();
}
class Computer{
	public static void plugin(USB usb){
		usb.start();
		System.out.println("设备正在工作!!!!!");
		usb.stop();
	}
}
class Flash implements USB{
	public void start(){
		System.out.println("U盘正在读入");
	}
	public void stop(){
		System.out.println("U盘停止工作");
	}
}
class Print implements USB{
	public void start(){
		System.out.println("打印机正在工作");
		
	}
	public void stop(){
		System.out.println("打印机停止工作");
	}
}

public class lianxi019 {
	public static void main(String args[]){
		Computer.plugin(new Print());
		Computer.plugin(new Flash());
	}
}
  • 工厂设计模式---java中最经常使用的一种模式
    package lianxijihe;
    interface Fruit{
    	void eat();
    }
    class Apple implements Fruit{
    	public void eat(){
    		System.out.println("吃苹果");
    	}
    }
    class Banana implements Fruit{
    	public void eat(){
    		System.out.println("吃香蕉");
    	}
    }
    class Factory {
    	 static Fruit f = null;
    	public static Fruit getInstance(String ClassName){
    		if("apple".equals(ClassName)){
    				f = new Apple();
    		}
    		if("banana".equals(ClassName)){
    			f = new Banana();
    		}
    		return f;
    	}
    }
    public class lianxi020 {
    	public static void main(String args[]){
    		Fruit f = Factory.getInstance("apple");
    		if(f!=null){
    			f.eat();
    		}
    	}
    }
    

    要针对接口编程,不要针对实现编程。
  • 代理设计模式
    package lianxijihe;
    interface NetWork{
    	void Open();
    }
    class Real implements NetWork{
    	public void Open(){
    		System.out.println("我要上网");
    	}
    }
    class Proxy implements NetWork{
    	NetWork net = null;
    	public Proxy(NetWork net){
    		this.net = net;
    	}
    	public void check(){
    		System.out.println("检查上网是否安全");
    	}
    	public void Open(){
    		check();
    		this.net.Open();
    	}
    
    
    }
    public class lianxi021 {
    	public static void main(String args[]){
    		NetWork net = new Proxy(new Real());
    		net.Open();
    	}
    }
    

  • 设配器模式设计

  • package lianxijihe;
    interface Window{
    	void open();
    	void close();
    	void activated();
    	void iconified();
    	void deiconified();
    }
    abstract class WindowAdapter implements Window{
    	public void open(){};
    	public void close(){};
    	public void activated(){};
    	public void iconified(){};
    	public void deiconified(){};
    }
    class WindowImpl extends WindowAdapter{
    	public void open(){
    		System.out.println("窗口打开");
    	}
    	public void close(){
    		System.out.println("窗口关闭");
    	}
    }
    public class lianxi022 {
    	public static void main(String args[]){
    	Window w = new WindowImpl();
    	w.open();
    	w.close();
    	}
    }
    


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值