抽象工厂模式的个人理解列子

很久前就像看看设计模式了,在此记录下自己的学习过程。
设计模式分为三种:静态工厂模式,工厂方法模式,抽象工厂模式。其中抽象工厂模式是三个里面最抽象的,也是最具有一般性的。
在看教程的同时,花了一上午时间写了个例子。
例子配置如图:
[img]http://dl.iteye.com/upload/picture/pic/88011/928e8541-37a7-3bf0-a5e3-9db62c66b8e2.bmp[/img]

[color=red]我认为比较重要的事:有几个产品,抽象工厂中就有几个方法;有几个产品族,就应该有几个具体的工厂类。[/color](有什么不对的地方希望给予指出)
代码如下:
[color=blue]抽象工厂:[/color]
package com.topnet.af.exercise.auto;
import com.topnet.af.exercise.auto.benz.BenzAuto;
import com.topnet.af.exercise.auto.bmw.BmwAuto;
/**
* 生产产品的接口(抽象类),[color=red]有几种产品,就有几个方法[/color]
* @author anfei
*
*/
public interface Factory {
//生产宝马方法
public BmwAuto factoryBmw();
//生产奔驰方法
public BenzAuto factoryBenz();
}

[color=blue]具体工厂类:[/color]
package com.topnet.af.exercise.auto;
import com.topnet.af.exercise.auto.benz.BenzAuto;
import com.topnet.af.exercise.auto.benz.SportBenzAuto;
import com.topnet.af.exercise.auto.bmw.BmwAuto;
import com.topnet.af.exercise.auto.bmw.SportBwmAuto;
/**
* 生产跑车型车具体工厂类,[color=red]有几种产品族,就有个具体工厂类[/color] * @author anfei
*
*/
public class FactorySport implements Factory {
public BmwAuto factoryBmw() {
return new SportBwmAuto();
}
public BenzAuto factoryBenz() {
return new SportBenzAuto();
}
}

package com.topnet.af.exercise.auto;
import com.topnet.af.exercise.auto.benz.BenzAuto;
import com.topnet.af.exercise.auto.benz.BusinessBenzAuto;
import com.topnet.af.exercise.auto.bmw.BusinessBwmAuto;
import com.topnet.af.exercise.auto.bmw.BmwAuto;
/**
* 生产商务型车具体工厂类,[color=red]有几种产品族,就有几个具体工厂类[/color] * @author anfei
*
*/
public class FactoryBusiness implements Factory {
public BmwAuto factoryBmw() {
return new BusinessBwmAuto();
}
public BenzAuto factoryBenz() {
return new BusinessBenzAuto();
}
}


[color=blue]抽象产品类:[/color]
package com.topnet.af.exercise.auto.bmw;
public interface BmwAuto {//奔驰
public void showFunction();
}


package com.topnet.af.exercise.auto.benz;
public interface BenzAuto {//宝马
public void showFunction();
}

[color=blue]
具体产品类:[/color]
package com.topnet.af.exercise.auto.benz;
public class BusinessBenzAuto implements BenzAuto {
public void showFunction() {
System.out.println("奔驰商务车");
}
}


package com.topnet.af.exercise.auto.benz;
public class SportBenzAuto implements BenzAuto {
public void showFunction() {
System.out.println("奔驰跑车");
}
}


package com.topnet.af.exercise.auto.bmw;
public class BusinessBwmAuto implements BmwAuto {
public void showFunction() {
System.out.println("宝马商务车");
}
}


package com.topnet.af.exercise.auto.bmw;
public class SportBwmAuto implements BmwAuto {
public void showFunction() {
System.out.println("宝马跑车");
}
}


[color=blue]测试类:[/color]
package com.topnet.af.exercise.auto;
import com.topnet.af.exercise.auto.benz.BenzAuto;
import com.topnet.af.exercise.auto.benz.SportBenzAuto;
import com.topnet.af.exercise.auto.bmw.BmwAuto;
/**
* 调用类
* @author anfei
*
*/
public class Magnate {
public static void main(String[] args) {
//跑车型工厂
Factory fs = new FactorySport();

//商务型工厂
Factory fb = new FactoryBusiness();

//奔驰
BenzAuto benz;
benz = fs.factoryBenz();
benz.showFunction();
benz = fb.factoryBenz();
benz.showFunction();

System.out.println("-----------------------");

//宝马
BmwAuto bmw;
bmw = fs.factoryBmw();
bmw.showFunction();
bmw = fb.factoryBmw();
bmw.showFunction();
}
}

[color=blue]
输出结果:[/color]
奔驰跑车
奔驰商务车
-----------------------
宝马跑车
宝马商务车
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值