java物品类_java – 抽象类的集合(或类似的东西……)

I’m looking for a solution that allows a single Cars class to contain

every Car object. I don’t want different collections (like Corvettes,

Clunkers). I’m also looking for a solution that allows the creation of

Car objects based on the attributes of an individual car kind… as

previously mentioned, creating a new Car of kind Corvette would result

in a speed of 0.9. There should be no other way to specify a car’s

speed.

哦,男孩哦,男孩有很多方法可以解决这个问题,我们可以整天继续!我会做一次大脑转储,希望你处理它不会太多.

解决方案1:使用策略.

策略基本上是将重度可替代逻辑与另一个类分开的一种方法.在这种情况下,每辆汽车都需要以不同的方式创建.一个策略是完美的.

对不起,如果我偶然混入一些C#…自从我编写java以来??已经很久了.

public interface CarCreationStrategy{

void BuildCar(Car theCar);

}

public class CorvetteStrategy implements CarCreationStrategy{

public void BuildCar(Car theCar){

theCar.Type = "Corvette";

theCar.Speed = 0.9;

theCar.Comments = "Speedster!";

}

}

public class ToyotaStrategy implements CarCreationStrategy{

public void BuildCar(Car theCar){

theCar.Type = "Toyota";

theCar.Speed = "0.5";

theCar.Comments = "Never dies, even if you drop it from the top of a building";

}

}

现在,您可以使用汽车构造函数传递策略.

public class Car{

// Variables ...

public Car(CarCreationStrategy strategy, int year){

strategy.BuildCar(this); // Implements your properties here.

this.year = year;

}

}

所以,你现在得到的是如此棒!

List cars = new List();

cars.Add(new Car(new CorvetteStrategy(),1999));

cars.Add(new Car(new ToyotaStrategy(),2011);

这将完全符合您的要求.

但是,您可以在战略和汽车之间找到一种耦合.

解决方案2:使用工厂.

工厂也是一个很好的解决方案,可能更容易.你做的是有一个CarFactory,有多种工厂方法来创建每种类型的汽车.

public class CarFactory{

public static Car BuildCorvette(int year){

Car car = new Car(year);

car.Type = "Corvette;

car.Speed = 0.9";

return car;

}

public static Car BuildToyota(int year){

Car car = new Car(year);

car.Type = "Toyota;

car.Speed = 0.5";

return car;

}

}

用法:

List cars = new List();

cars.Add(CarFactory.BuildCorvette(1999));

cars.Add(CarFactory.BuildToyota(2011));

所以关于这一点的好处是你不必担心现在实例化汽车.它全部由CarFactory处理,将您的“实例化逻辑”与您的代码分离.但是,您仍然需要知道要构建哪辆车并相应地调用该方法,这仍然是一个小耦合.

解决方案3:战略工厂!

所以,如果我们想要摆脱最后一点的耦合,让我们将两者结合起来!

public class CarFactory{

public static Car BuildCar(CarCreationStrategy strategy, int year){

Car car = new Car(year);

strategy.BuildCar(car);

return car;

}

}

List cars = new List();

cars.Add(CarFactory.BuildCar(new CorvetteStrategy(),1999));

cars.Add(CarFactory.BuildCar(new ToyotaStrategy(),2011);

现在你有一个建造汽车的战略,一个为你建造它们的工厂,以及一辆没有你原来的额外联轴器的汽车.很棒,不是吗?

如果你使用过Swing,你会注意到这就是他们处理一些事情的方式,比如Layouts(GridBagLayout,GridLayout都是策略).还有一个BorderFactory.

起色

抽象策略

public interface CarCreationStrategy{

void BuildCar(Car theCar);

}

public class AbstractStrategy:CarCreationStrategy{

public string Type;

public double Speed;

public string Comments;

public void BuildCar(Car theCar){

theCar.Type = this.Type;

theCar.Speed = this.Speed;

theCar.Comments = this.Comments;

}

}

public class CorvetteStrategy extends AbstractStrategy{

public CorvetteStrategy(){

this.Type = "Corvette";

this.Speed = 0.9;

this.Comments = "Speedster!";

}

}

public class ToyotaStrategy extends AbstractStrategy{

public ToyotaStrategy{

this.Type = "Toyota";

this.Speed = "0.5";

this.Comments = "Never dies, even if you drop it from the top of a building";

}

}

使用此功能,您可以灵活地动态创建AbstractStrategies(例如,从数据存储中提取汽车属性).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值