Shape Factory

Factory is a design pattern in common usage. Implement a ShapeFactory that can generate correct shape.

You can assume that we have only tree different shapes: TriangleSquare and Rectangle.

Example
ShapeFactory sf = new ShapeFactory();
Shape shape = sf.getShape("Square");
shape.draw();
>>  ----
>> |    |
>> |    |
>>  ----

shape = sf.getShape("Triangle");
shape.draw();
>>   /\
>>  /  \
>> /____\

shape = sf.getShape("Rectangle");
shape.draw();
>>  ----
>> |    |
>>  ----

 1 /**
 2  * Your object will be instantiated and called as such:
 3  * ShapeFactory sf = new ShapeFactory();
 4  * Shape shape = sf.getShape(shapeType);
 5  * shape.draw();
 6  */
 7 interface Shape {
 8     void draw();
 9 }
10 
11 class Rectangle implements Shape {
12     public void draw() {
13         System.out.println(" ----"); 
14         System.out.println("|    |");
15         System.out.println(" ----");    
16     }
17 }
18 
19 class Square implements Shape {
20     public void draw() {
21         System.out.println(" ----"); 
22         System.out.println("|    |");
23         System.out.println("|    |");
24         System.out.println(" ----");
25     }
26 }
27 
28 class Triangle implements Shape {
29     public void draw() {
30         System.out.println("  /\\"); 
31         System.out.println(" /  \\");
32         System.out.println("/____\\");
33     }
34 }
35 
36 public class ShapeFactory {
37     /**
38      * @param shapeType a string
39      * @return Get object of type Shape
40      */
41     public Shape getShape(String shapeType) {
42         if (shapeType.toLowerCase().equals("square")) {
43             return new Square();
44         } else if (shapeType.toLowerCase().equals("triangle")) {
45             return new Triangle();
46         } else if (shapeType.toLowerCase().equals("rectangle")) {
47             return new Rectangle();
48         } else {
49             return null;
50         }
51     }
52 }

 

转载于:https://www.cnblogs.com/beiyeqingteng/p/5662231.html

### 如何在 Android 中读取 shape 文件 在 Android 开发中,`shape` 文件通常用于定义可绘制对象(Drawable),这些文件存储在 `res/drawable` 目录下。为了实现对已有 `shape.xml` 文件的动态解析和修改功能,可以借助 SAX 解析器来完成这一过程。 以下是基于提供的引用内容以及专业知识构建的一个完整的解决方案: #### 使用 SAX 解析器读取 shape 文件 可以通过以下方式利用 SAX 解析器读取 XML 数据并将其转换为对应的 UI 属性设置逻辑[^1]。 ```java private void initSax() { // 获取当前文件的内容 String text = FileDocumentManager.getInstance().getDocument(file).getText(); // 创建处理程序实例 ShapeSaxHandler handler = new ShapeSaxHandler(); try { // 将文本传递给处理器以生成视图列表 handler.createViewList(text); } catch (ParserConfigurationException | SAXException | IOException e) { e.printStackTrace(); } } ``` 此代码片段展示了如何通过获取指定文件中的 XML 文本数据,并调用 `ShapeSaxHandler` 的方法对其进行解析。该类负责提取形状的具体参数以便后续应用到界面组件上。 #### 定义 ShapeSaxHandler 类 下面是一个可能实现的 `ShapeSaxHandler` 部分结构示例,它继承自默认的 DefaultHandler 并重写了必要的回调函数: ```java public class ShapeSaxHandler extends DefaultHandler { private List<View> viewList; public ShapeSaxHandler() { this.viewList = new ArrayList<>(); } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { super.startElement(uri, localName, qName, attributes); switch(qName){ case "solid": handleSolid(attributes); break; case "stroke": handleStroke(attributes); break; default: System.out.println("Unrecognized element: "+qName); } } private void handleSolid(Attributes attrs){ String colorValue = attrs.getValue("color"); // 进一步处理颜色值... } private void handleStroke(Attributes attrs){ int width = Integer.parseInt(attrs.getValue("width")); String dashGap = attrs.getValue("dashGap"); // 处理边框宽度和其他样式... } public void createViewList(String xmlContent) throws ParserConfigurationException, SAXException, IOException{ SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); InputSource inputSource = new InputSource(new StringReader(xmlContent)); saxParser.parse(inputSource,this); } } ``` 以上代码实现了针对 `<solid>` 和 `<stroke>` 元素的基础解析逻辑。实际项目中还需要扩展其他标签的支持如 gradient、corners 等等[^2]^。 #### 动态加载 Drawable 资源 除了手动解析外,还可以直接使用 Android 提供的功能快速加载 drawable resource : ```java GradientDrawable gd = (GradientDrawable) ContextCompat.getDrawable(context,R.drawable.my_shape); int solidColor = gd.getColor().getDefaultColor(); float[] radii = gd.getCornerRadii(); // 可继续访问更多属性... ``` 这种方法适用于只需要简单查询某些特定属性的情况;但如果需要全面控制整个图形配置,则推荐前面提到的手动解析方案[^3]. ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值