面向对象把握一个重要的设计经验


谁拥有数据,谁就对外提供操作这些数据的方法。


例子:
1、人在黑板上画圆 (person blackboard circle)draw(x,y) 写在circle中
2、列车司机紧急刹车
3、售货员统计收获小票的金额
4、你把门关上了

5、球从绳子上一边移动到另一边。

class Ball
{
	private Rope rope;
	private Point currentPoint;
	public Ball(Rope rope,Point startPoint)
	{
		this.rope = rope;
		this.currentPoint = startPoint;
	}


	public void move()
	{
		this.currentPoint = rope.nextPoint(currentPoint);
		System.out.println("球移动到" + currentPoint);
	}
}




class Rope
{
	private Point start;
	private Point end;




	public Rope(Point start,Point end)
	{
		this.start = start;
		this.end = end;
	}




	public Point nextPoint(Point currentPoint)
	{
		//下一个point的计算


		
	}
}

6、两块石头磨成一把石刀,石刀可以砍树,砍成木材,木材做成椅子

Stone

StoneKnife

两块石头 变成 一把石刀(石头对象消失了)

StoneKnife = KnifeFactory.createKnife(Stone first,Stone second)

Matertial m = cut(Tree tree)

Tree

Matertial

Chair

木材变成椅子(木材消失了)

Chair c = ChairFactory.makeChair(Matertial m);









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值