资源预览内容
第1页 / 共6页
第2页 / 共6页
第3页 / 共6页
第4页 / 共6页
第5页 / 共6页
第6页 / 共6页
亲,该文档总共6页全部预览完了,如果喜欢就下载吧!
资源描述
OOA&D 第六周小课题目1、民用电源为交流220V,某电器需要直流5V电源,使用适配器模式实现电源转换。对象适配器模式和类适配器模式UML如下图。分别使用对象适配器模式和类适配器模式代码实现该结构。并构造客户端类。测试适配器功能。适配器 单抽象桥接 双抽象代码:/class V220Plugpackage week06_01;public class V220Plug public void GetPower() System.out.println(Show 220 valts ac。);/class V5Plugpackage week06_01;public class V5Plug public void Get5VPower() System.out.println(Show 5 valts dc。);/class ObjectAdapterpackage week06_01;public class ObjectAdapter extends V220PlugV5Plug v5 = new V5Plug();public void GetPower() v5.Get5VPower();/class My220Plugpackage week06_01;public interface My220VPlug public void GetPower();/class ClassAdapterpackage week06_01;public class ClassAdapter extends V5Plug implements My220VPlugpublic void GetPower() Get5VPower();/class Clientpackage week06_01;public class Client public static void main(String args) System.out.println(ObjectAdapter:); V220Plug electrical1 = new ObjectAdapter(); electrical1.GetPower(); System.out.println(ClassAdapter:); My220VPlug electrical2 = new ClassAdapter(); electrical2.GetPower(); 运行结果:2、在三种操作平台上Windows、Linux和Mactosh绘制四种图形Rectangle、Circle、Line和Square。即在不同平台上实现画图形Shape,而shape本身也变化。使用桥接模式可以隔离抽象类的变化和实现的变化。UML类图如下,用代码实现桥接模式的应用。并构造客户端类。实现在各种平台上画出各种图形。ShapeDraw():voidRectangleCircleLineSquarePlatformOnPlatform():voidWindowsLinuxMactosh代码:/class Platformpackage week06_02;public abstract class Platform public abstract void OnPlatform();/class Windowspackage week06_02;public class Windows extends Platformpublic void OnPlatform() System.out.println(Using the Windows System.);/class Linuxpackage week06_02;public class Linux extends Platformpublic void OnPlatform() System.out.println(Using the Linux System.);/class Mactoshpackage week06_02;public class Mactosh extends Platformpublic void OnPlatform() System.out.println(Using the Mactosh System.);/class Shapepackage week06_02;public abstract class Shape private Platform os; public Shape(Platform clientOS) os = clientOS; public void Draw() os.OnPlatform();/class Rectanglepackage week06_02;public class Rectangle extends Shapepublic Rectangle(Platform clientOS) super(clientOS);System.out.print(Write a rectangle.);/class Circlepackage week06_02;public class Circle extends Shapepublic Circle(Platform clientOS) super(clientOS);System.out.print(Write a Circle.);/class Linepackage week06_02;public class Line extends Shapepublic Line(Platform clientOS) super(clientOS);System.out.print(Write a line.);/class Squarepackage week06_02;public class Square extends Shapepublic Square(Platform clientOS) super(clientOS);System.out.print(Write a square.);/class Clientpackage week06_02;public class Client public static void main(String args) Rectangle r1=new Rectangle(new Windows(); r1.Draw(); Circle c1=new Circle(new Windows(); c1.Draw();Line l1=new Line(new Windows();l1.Draw();Square s1=new Square(new Windows();s1.Draw();System.out.println(=);Rectangle r2=new Rectangle(new Linux(); r2.Draw(); Circle c2=new Circle(new Linux(); c2.Draw();Line l2=new Line(new Linux();l2.Draw();Square s2=new Square(new Linux();s2.Draw();System.out.println(=);Rectangle r3=new Rectangle(new Mactosh(); r3.Draw(); Circle c3=new Circle(new Mactosh(); c3.Draw();Line l3=new Line(new Mactosh();l3.Draw();Square s3=new Square(new Mactosh();s3.Draw();System.out.println(=); 运行结果:要求:1. 使用 Java 、C# 或者 C+或其他OOPL如ruby、python等编程语言编写上边题中UML类图的代码,能运行通过实现程序的功能。
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号