标签:
|
1
2
3
4
5
|
package com.example.demo6;//定义小汽车接口public interface ICar { //由于工厂模式仅关系对象的创建,为说明方便,无需定义方法} |
|
1
2
3
4
5
|
package com.example.demo6;//高档小汽车public class TopCar implements ICar {} |
|
1
2
3
4
5
|
package com.example.demo6;//中档小汽车public class MidCar implements ICar {} |
|
1
2
3
4
5
|
package com.example.demo6;//低档小汽车public class LowCar implements ICar {} |
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
|
package com.example.demo6;//简单工厂public class CarSimpleFactory { public static final String TOPTYPE="toptype"; public static final String MIDTYPE="midtype"; public static final String LOWTYPE="lowtype"; public static ICar create(String mark){ ICar obj=null; if(mark.equals(TOPTYPE)){ //如果是高档类型 obj=new TopCar(); //则创建高档车对象 }else if(mark.equals(MIDTYPE)){ obj=new MidCar(); }else if(mark.equals(LOWTYPE)){ obj=new LowCar(); } return obj; //返回选择的对象 }} |
|
1
2
3
4
5
6
7
8
|
package com.example.demo6;//测试程序public class CarTest { public static void main(String[] args) { //从工厂中创建对象 ICar obj=CarSimpleFactory.create("toptype"); }} |
|
1
2
3
4
5
|
package com.example.demo8;//定义小汽车接口public interface ICar {} |
|
1
2
3
4
5
|
package com.example.demo8;//高档小汽车public class TopCar implements ICar {} |
|
1
2
3
4
5
|
package com.example.demo8;//中档小汽车public class MidCar implements ICar {} |
|
1
2
3
4
5
|
package com.example.demo8;//低档小汽车public class LowCar implements ICar {} |
|
1
2
3
4
5
|
package com.example.demo8;//定义抽象工厂public abstract class AbstractFactory { public abstract ICar create();} |
|
01
02
03
04
05
06
07
08
09
10
|
package com.example.demo8;//定义高档小汽车工厂public class TopFactory extends AbstractFactory { @Override public ICar create() { return new TopCar();//高档工厂生成高档小气车对象 }} |
|
01
02
03
04
05
06
07
08
09
10
|
package com.example.demo8;//定义中档小汽车工厂public class MidFactory extends AbstractFactory { @Override public ICar create() { return new LowCar();//低档工厂生成低档小汽车对象 }} |
|
01
02
03
04
05
06
07
08
09
10
|
package com.example.demo8;//定义低档小汽车工厂public class LowFactory extends AbstractFactory { @Override public ICar create() { return new MidCar();//低档工厂生成中档小汽车 }} |
|
1
2
3
4
5
6
7
8
|
package com.example.demo8;//测试类public class CarTest { public static void main(String[] args) { AbstractFactory obj=new TopFactory();//多态创建高档工厂 ICar car=obj.create(); //获得高档工厂中的小汽车对象 }} |
|
1
2
3
|
package com.example.demo9;//定义小汽车接口public interface ICar {} |
|
1
2
3
|
package com.example.demo9;//定义高档小汽车类public class TopCar implements ICar {} |
|
1
2
3
|
package com.example.demo9;//定义中档小汽车类public class MidCar implements ICar {} |
|
1
2
3
|
package com.example.demo9;//定义低档小汽车类public class LowCar implements ICar {} |
|
1
2
3
|
package com.example.demo9;//定义公共汽车接口public interface IBus {} |
|
1
2
3
|
package com.example.demo9;//定义高档公共汽车类public class UpBus implements IBus {} |
|
1
2
3
|
package com.example.demo9;//定义中档公共汽车类public class MidBus implements IBus {} |
|
1
2
3
|
package com.example.demo9;//定义低档公共汽车类public class DnBus implements IBus {} |
|
1
2
3
4
5
6
|
package com.example.demo9;//定义抽象工厂public abstract class AbstractFactory { public abstract ICar createCar();//产生小汽车对象 public abstract IBus createBus();//产生公共汽车对象} |
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
|
package com.example.demo9;//定义高档工厂public class TopFactory extends AbstractFactory { @Override public ICar createCar() { return new TopCar();//高档工厂生成高档小汽车对象 } @Override public IBus createBus() { return new UpBus();//高档工厂生成高档公共汽车对象 }} |
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
|
package com.example.demo9;//定义中档工厂public class MidFactory extends AbstractFactory { @Override public ICar createCar() { return new MidCar();//中档工厂生成中档小汽车 } @Override public IBus createBus() { return new MidBus();//中档工厂生成中档公共汽车 }} |
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
|
package com.example.demo9;//定义低档工厂public class LowFactory extends AbstractFactory { @Override public ICar createCar() { return new LowCar();//低档工厂生成低档小汽车对象 } @Override public IBus createBus() { return new DnBus();//低档工厂生成低档公共汽车对象 }} |
标签:
原文地址:http://www.cnblogs.com/android-blogs/p/5631966.html