1,对平房进行田园风光式的打造。
2,对楼房进行欧式风格的打造。
可以用继承的方式来完成。
House
|--PingFang
|--TianYuanPingFang
|--LouFang
|--OuShiLouFang
该体系就有点臃肿,因为当平房想要改成欧式时,还要建立子类。
*/
abstract class House
{
abstract void show();
}
class PingFang extends House
{
public void show()
{
System.out.println("平房");
}
}
class TianYuanPingFang extends PingFang
{
public void showTianYuan()
{
super.show();
System.out.println("田园风格");
}
}
class LouFang extends House
{
public void show()
{
System.out.println("楼房");
}
}
class OuShiLouFang extends LouFang
{
public void showOuShi()
{
super.show();
System.out.println("欧式风格");
}
}
class HouseDemo
{
public static void main(String[] args)
{
PingFang p = new PingFang();
///p.show();
TianYuanPingFang t = new TianYuanPingFang();
t.showTianYuan();
LouFang l = new LouFang();
//l.show();
OuShiLouFang o = new OuShiLouFang();
o.showOuShi();
}
}
使用装饰设计模式进行装饰:
[java] view plaincopy
/*
原体系:
House
|--PingFang
|--LouFang
无论将平房打造成田园还是欧式,都是对平房一种装修。
而该装修也可以用楼房。
将现将该装修定义好,需要把什么房子进行改装修,传入即可。
House
|--PingFang
|--LouFang
|--TianYuan
|--OuShi
*/
abstract class House
{
abstract void show();
}
class PingFang extends House
{
public void show()
{
System.out.println("平房");
}
}
class LouFang extends House
{
public void show()
{
System.out.println("楼房");
}
}
class TianYuan extends House
{
private House h;
TianYuan(House h)
{
this.h = h;
}
public void show()
{
System.out.println("田园风格");
}
public void showTianYuan()
{
h.show();
this.show();
}
}
class OuShi extends House
{
private House h;
OuShi(House h)
{
this.h = h;
}
public void show()
{
System.out.println("欧式风格");
}
public void showOuShi()
{
h.show();
this.show();
}
}
class HouseDemo2
{
public static void main(String[] args)
{
PingFang p = new PingFang();
LouFang l = new LouFang();
OuShi o = new OuShi(new TianYuan(p));
o.showOuShi();
//TianYuan t = new TianYuan(l);
//t.showTianYuan();