接口是实现多重继承的途径,而生成遵循某个接口的对象的典型方法就是工厂方法设计模式。
题目:创建一个Cycle接口,及其Unicycle、Bicycle、Tricycle实现。对每种类型的Cycle都创建相应的工厂,然后编写代码使用这些工厂。工厂产生的对象能够被管理,可以输出当前产生的对象总数。
emmmm,本人看了一些别人的代码,理解之后加入了一些自己的理解,初步诞生代码见下:
package homework1;
interface Cycle{
int wheels();
void cyclingTime();
void printPrices();
}
interface CycleFactory{
Cycle getCycle();
}
class Unicycle implements Cycle{
private int wheels = 1;
private int price = 180;
@Override
public int wheels() {
return wheels;
}
@Override
public void cyclingTime() {
// TODO Auto-generated method stub
System.out.println("The longest cycling time is "+wheels*2+" years.");
}
@Override
public void printPrices() {
System.out.println("The price is "+price+" yuan.");
}
}
class Bicycle implements Cycle{
private int wheels = 2;
private int price = 280;
@Override
public int wheels() {
return wheels;
}
@Override
public void cyclingTime() {
// TODO Auto-generated method stub
System.out.println("The longest cycling time is "+wheels*2+" years.");
}
@Override
public void printPrices() {
System.out.println("The price is "+price+" yuan.");
}
}
class Tricycle implements Cycle{
private int wheels = 3;
private int price = 380;
@Override
public int wheels() {
return wheels;
}
@Override
public void cyclingTime() {
// TODO Auto-generated method stub
System.out.println("The longest cycling time is "+wheels*2+" years.");
}
@Override
public void printPrices() {
System.out.println("The price is "+price+" yuan.");
}
}
class UnicycleFactory implements CycleFactory{
private int number = 0;
public Cycle getCycle() {
Unicycle uni = new Unicycle();
number++;
System.out.println("当前产生对象数:"+number);
return uni;
}
}
class BicycleFactory implements CycleFactory{
private int number = 0;
public Cycle getCycle() {
Bicycle bi = new Bicycle();
number++;
System.out.println("当前产生对象数:"+number);
return bi;
}
}
class TricycleFactory implements CycleFactory{
private int number =0;
public Cycle getCycle() {
Tricycle tri = new Tricycle();
number++;
System.out.println("当前产生对象数:"+number);
return tri;
}
}
public class Main {
public static void CycleSomethings(CycleFactory factory){
Cycle cycle=factory.getCycle();
System.out.println(cycle.wheels());
cycle.cyclingTime();
cycle.printPrices();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
UnicycleFactory Uf = new UnicycleFactory();
CycleSomethings(Uf);
System.out.print("\n");
BicycleFactory Bf = new BicycleFactory();
CycleSomethings(Bf);
System.out.print("\n");
TricycleFactory Tf = new TricycleFactory();
CycleSomethings(Tf);
System.out.print("\n");
//-------------------------------------------------
CycleSomethings(Uf);
System.out.print("\n");
CycleSomethings(Bf);
System.out.print("\n");
CycleSomethings(Tf);
System.out.print("\n");
}
}
运行结果:
当前产生对象数:1
1
The longest cycling time is 2 years.
The price is 180 yuan.
当前产生对象数:1
2
The longest cycling time is 4 years.
The price is 280 yuan.
当前产生对象数:1
3
The longest cycling time is 6 years.
The price is 380 yuan.
当前产生对象数:2
1
The longest cycling time is 2 years.
The price is 180 yuan.
当前产生对象数:2
2
The longest cycling time is 4 years.
The price is 280 yuan.
当前产生对象数:2
3
The longest cycling time is 6 years.
The price is 380 yuan.
哈哈哈,不得不承认,第一次写的代码像老婆子的裹脚布——又臭又长。。。。。
中间有很多代码复制的地方,这很不好,我晓得。。。。。。
但是,至少现在我还并没有找到更好的办法来解决当前的代码复制的问题!
先到这里,后续肯定还会有优化,我再想想!!!