标签:ati 编程 实现类 绘制矩形 catch 颜色 三方 red 软件
? 第二种设计方案是根据实际需要对形状和颜色进行组合。
对于有两个变化维度(即两个变化的原因)的系统,采用方案二来进行设计系统中类的个数更少,且系统扩展更为方便。设计方案二即是桥接模式的应用。桥接模式将继承关系转换为关联关系,从而降低了类与类之间的耦合,减少了代码编写量。
1 public interface Implementor 2 { 3 public void operationImpl(); 4 }
典型的抽象类代码:
1 public abstract class Abstraction 2 { 3 protected Implementor impl; 4 5 public void setImpl(Implementor impl) 6 { 7 this.impl=impl; 8 } 9 10 public abstract void operation(); 11 }
典型的扩充抽象类代码:
1 public class RefinedAbstraction extends Abstraction 2 { 3 public void operation() 4 { 5 //代码 6 impl.operationImpl(); 7 //代码 8 } 9 }
实例代码(JAVA):
1 //抽象类 2 public abstract class Pen 3 { 4 protected Color color; 5 public void setColor(Color color) 6 { 7 this.color=color; 8 } 9 public abstract void draw(String name); 10 } 11 12 //扩充抽象类 13 public class SmallPen extends Pen 14 { 15 public void draw(String name) 16 { 17 String penType="小号毛笔绘制"; 18 this.color.bepaint(penType,name); 19 } 20 } 21 22 //扩充抽象类 23 public class MiddlePen extends Pen 24 { 25 public void draw(String name) 26 { 27 String penType="中号毛笔绘制"; 28 this.color.bepaint(penType,name); 29 } 30 } 31 32 //扩充抽象类 33 public class BigPen extends Pen 34 { 35 public void draw(String name) 36 { 37 String penType="大号毛笔绘制"; 38 this.color.bepaint(penType,name); 39 } 40 } 41 42 //实现类接口 43 public interface Color 44 { 45 void bepaint(String penType,String name); 46 } 47 48 //扩充实现类 49 public class Red implements Color 50 { 51 public void bepaint(String penType,String name) 52 { 53 System.out.println(penType + "红色的"+ name + "."); 54 } 55 } 56 57 //扩充实现类 58 public class Green implements Color 59 { 60 public void bepaint(String penType,String name) 61 { 62 System.out.println(penType + "绿色的"+ name + "."); 63 } 64 } 65 66 //扩充实现类 67 public class Blue implements Color 68 { 69 public void bepaint(String penType,String name) 70 { 71 System.out.println(penType + "蓝色的"+ name + "."); 72 } 73 } 74 75 //扩充实现类 76 public class White implements Color 77 { 78 public void bepaint(String penType,String name) 79 { 80 System.out.println(penType + "白色的"+ name + "."); 81 } 82 } 83 84 //扩充实现类 85 public class Black implements Color 86 { 87 public void bepaint(String penType,String name) 88 { 89 System.out.println(penType + "黑色的"+ name + "."); 90 } 91 } 92 93 //配置文件configPen.xml 94 <?xml version="1.0"?> 95 <config> 96 <className>Blue</className> 97 <className>SmallPen</className> 98 </config> 99 100 //使用java反射创建具体的颜色和画笔 101 import javax.xml.parsers.*; 102 import org.w3c.dom.*; 103 import org.xml.sax.SAXException; 104 import java.io.*; 105 public class XMLUtilPen 106 { 107 //该方法用于从XML配置文件中提取具体类类名,并返回一个实例对象 108 public static Object getBean(String args) 109 { 110 try 111 { 112 //创建文档对象 113 DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance(); 114 DocumentBuilder builder = dFactory.newDocumentBuilder(); 115 Document doc; 116 doc = builder.parse(new File("configPen.xml")); 117 NodeList nl=null; 118 Node classNode=null; 119 String cName=null; 120 nl = doc.getElementsByTagName("className"); 121 122 if(args.equals("color")) 123 { 124 //获取包含类名的文本节点 125 classNode=nl.item(0).getFirstChild(); 126 127 } 128 else if(args.equals("pen")) 129 { 130 //获取包含类名的文本节点 131 classNode=nl.item(1).getFirstChild(); 132 } 133 134 cName=classNode.getNodeValue(); 135 //通过类名生成实例对象并将其返回 136 Class c=Class.forName(cName); 137 Object obj=c.newInstance(); 138 return obj; 139 } 140 catch(Exception e) 141 { 142 e.printStackTrace(); 143 return null; 144 } 145 } 146 } 147 148 //客户端 149 public class Client 150 { 151 public static void main(String a[]) 152 { 153 Color color; 154 Pen pen; 155 156 color=(Color)XMLUtilPen.getBean("color"); 157 pen=(Pen)XMLUtilPen.getBean("pen"); 158 159 pen.setColor(color); 160 pen.draw("鲜花"); 161 } 162 }
标签:ati 编程 实现类 绘制矩形 catch 颜色 三方 red 软件
原文地址:https://www.cnblogs.com/WindSun/p/10260547.html