JAVA设计模式《四》

王朝学院·作者佚名  2016-05-28
窄屏简体版  字體: 小  |  中  |  大  |  超大  

java设计模式《四》经过前几篇的介绍相信大家对JAVA的设计模式一定有所解了,本篇我们再一起学习一下适配器模式、代理模式和工厂模式。

适配器模式使用的场景非常多,例如现实生活中,我们的笔记本电脑的充电线大部分都是三向插头,而当我们遇见一个二向插口时,如何给我们的笔记本充电呢?这时我们就需要一个适配器,帮我们把二向插口转化为三向插口。接下来我们需要讨论的适配器模式,就是如同这里的二向转三向插口,下面我们就以这个现实问题,来用代码实现一下适配器模式。

1、创建三向电流接口:

/* * 定义一个三相充电器接口 */public interface ThreePathIm { //使用三相电流供电 public void powerWithThree();}

2、创建三向电流类:

public class ThreePath implements ThreePathIm { public void powerWithThree() { System.out.PRintln("使用三向电流供电\n"); }}

3、创建二向电流类:

/* * 二相电流类 */public class TwoPath { public void prowerWithTwo(){ System.out.println("使用二相电流供电"); } }

4、创建二向接口转三向接口类(接口适配器):

/* * 电源接口适配器 * 二向接口适配三口接口 */public class TwoPlugAdapt implements ThreePathIm { private TwoPath two ; public TwoPlugAdapt(TwoPath two){ this.two = two; } public void powerWithThree() { System.out.println("通过适配器转化"); two.prowerWithTwo(); }}

5、创建继承二向电流类并实现了三向电流接口的类(继承适配器):

/* * 继承适配器 */public class extendsAdapt extends TwoPath implements ThreePathIm { public void powerWithThree() { System.out.println("\n使用继承适配器转化"); this.prowerWithTwo(); }}

6、创建测试类:

public class noteBook { private ThreePathIm path ; private noteBook(ThreePathIm path){ this.path = path; } private void change(){ path.powerWithThree(); } public static void main(String [] args){ ThreePathIm tpi = new ThreePath(); tpi.powerWithThree(); TwoPath two = new TwoPath();//获得二相接口对象 ThreePathIm three = new TwoPlugAdapt(two);//把二相电流接口转为三向 noteBook notebook = new noteBook(three); notebook.change(); three = new extendsAdapt(); notebook = new noteBook(three); notebook.change(); }}

工程模式使用的场景也比较多,比如之前很火的一款名为脸萌的图片制作软件,我们可以根据我们的需要来选择头发的类型,这是如何实现的呢?下面我们来一起学习一下。1、创建头发类型接口:

public interface Hair { public void getHair();//获得发型方法 }

2、通过该方法实现两个头发类型:

a、左偏分:

public class leftHair implements Hair { //左偏分 public void getHair() { System.out.println("我的头发是左偏分"); }}

b、右偏分:

public class rightHair implements Hair { //右偏分 public void getHair() { System.out.println("我的头发是右偏分"); }}

3、创建头发工厂:

public class hairFactory { Hair hair; //通过关键词来获得相应的头发类型类 public Hair getHairKey(String key){ if("left".equals(key)){ hair = new leftHair(); }else if("right".equals(key)){ hair = new rightHair(); } return hair; } //通过类地址来获得相应的头发类型类 public Hair getHairClass(String cls){ try { hair = (Hair)Class.forName(cls).newInstance(); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalaccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return hair; }}

4、创建测试类:

public class hairTest { /** * 测试类 * @param args */ public static void main(String[] args) { Hair hair1 = new leftHair(); Hair hair2 = new rightHair(); hair1.getHair(); hair2.getHair(); //通过工厂对象进行创建类 hairFactory factory = new hairFactory(); Hair hair3 = factory.getHairKey("left"); hair3.getHair(); Hair hair4 = factory.getHairClass("cn.edu.hpu.hair.rightHair"); hair4.getHair(); }}

代理模式是对一个对象提供一种代理,用来控制对这个对象的控制。

下面我们通过实现一个汽车行驶时,记录行车时间和日志的功能,不多说,上代码:

1、封装一个汽车行驶的方法:

public interface MoveAble { public void move();}

2、创建一个汽车类:

public class Car implements MoveAble { public void move(){ try {// System.out.println("汽车开始行驶");// long start = System.currentTimeMillis(); System.out.println("汽车在行驶中"); Thread.sleep(new Random().nextInt(1000));//模拟汽车行驶 // long end = System.currentTimeMillis();// System.out.println("汽车停止行驶 汽车行驶了:"+(end-start)+"毫秒"); } catch (InterruptedException e) { e.printStackTrace(); } }}

3、实现一个汽车子类:

通过继承方法,创建不同子类来实现行驶时间和日志的记录。

public class Car2 extends Car { public void move() { System.out.println("汽车开始行驶"); long start = System.currentTimeMillis(); super.move();//执行父类的方法 long end = System.currentTimeMillis(); System.out.println("汽车停止行驶 汽车行驶了:"+(end-start)+"毫秒"); } }

4、创建接口代理:

a、时间代理对象:

public class CarTimeProxy implements MoveAble { public CarTimeProxy(MoveAble m){ this.m = m; } public MoveAble m; public void move() { System.out.println("汽车开始行驶"); long start = System.currentTimeMillis(); m.move(); long end = System.currentTimeMillis(); System.out.println("汽车停止行驶 汽车行驶了:"+(end-start)+"毫秒"); }}

b、日志代理对象:

public class CarLogProxy implements MoveAble { public CarLogProxy(MoveAble m) { super(); this.m = m; } public MoveAble m; public void move() { System.out.println("日志开始"); m.move(); System.out.println("日志结束"); }}

5、测试类:

public class carTest { /** * @param 测试 */ public static void main(String[] args) {// Car car = new Car();// car.move(); //继承模式的静态代理// Car car = new Car2();// car.move(); //接口模式的静态代理,叠加操作 Car car = new Car(); MoveAble m1 = new CarTimeProxy(car); MoveAble m2 = new CarLogProxy(m1); m2.move(); }}

6、通过JDK实现代理:

public class TimeHander implements InvocationHandler { public TimeHander(Object object) { super(); this.object = object; } Object object; /* * 参数: * proxy:被代理的对象 * method:被代理对象的方法 * args:方法的参数 */ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("汽车开始行驶"); long start = System.currentTimeMillis(); method.invoke(object, null); long end = System.currentTimeMillis(); System.out.println("汽车停止行驶 汽车行驶了:"+(end-start)+"毫秒"); return null; }}

7、JDK代理测试:

//jdk动态代理public class Test { public static void main(String [] args){ Car car = new Car(); InvocationHandler hander = new TimeHander(car); Class cls = car.getClass(); MoveAble m = (MoveAble) Proxy.newProxyInstance(cls.getClassLoader(), cls.getInterfaces(), hander); m.move(); }}

截止到本篇关于JAVA中的设计模式已经为大家分析完毕,当然这些都是很基本的东西,想真正的在开发中使用,还需要多多练习。如有疑问,可以留言讨论。

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航