Posted on 2011-04-22 16:35
哈希 阅读(116)
评论(0) 编辑 收藏 所属分类:
设计模式
好了,开始这是我自己用形状,大小来描述桥梁模式的方式:
可以分为四种:大圆,小圆,大矩形,小矩形
看代码:
public interface Shape {
public abstract void DrawShape();
}
public class Circle implements Shape {
public Circle() {
// TODO Auto-generated constructor stub
}
public void DrawShape() {
// TODO Auto-generated method stub
System.out.println("我是圆");
}
}
public class Triangle implements Shape {
public Triangle() {
// TODO Auto-generated constructor stub
}
public void DrawShape() {
// TODO Auto-generated method stub
System.out.println("我是矩形!");
}
}
public abstract class Type {
public abstract void draw(String str);
protected Shape GetShape(String type){
if(type.equals("circle")){
return new Circle();
}else if(type.equals("triangle")){
return new Triangle();
}else{
return Circle();
}
}
private Shape Circle() {
// TODO Auto-generated method stub
return new Circle();
}
}
public class Big extends Type {
private Shape shape;
public Big(String tr){
shape=GetShape(tr);
}
public void draw(String str) {
// TODO Auto-generated method stub
System.out.println(str);
System.out.println("大");
shape.DrawShape();
}
}
public class Small extends Type {
private Shape shape;
public Small(String str) {
shape=GetShape(str);
}
public void draw(String tr){
System.out.println(tr);
System.out.println("小");
shape.DrawShape();
}
}
可以测试一下啊:
public class Test {
public static void main(String[] args) {
System.out.println("=============小类型================");
//small类型时候
Type type=new Small("triangle");
type.draw("triangle");
Type type2=new Small("circle");
type2.draw("circle");
System.out.println("=============大类型================");
//Big类型时候
Type type3=new Big("circle");
type3.draw("circle");
Type type4=new Big("triangle");
type4.draw("triangle");
}
}
设计模式相当强悍啊!能帮助大家学习一点本人心得也算很愉快了!我会继续吧所学贡献出来的!