`
独上高楼
  • 浏览: 58063 次
  • 性别: Icon_minigender_1
  • 来自: 郑州
社区版块
存档分类

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

阅读更多
很久前就像看看设计模式了,在此记录下自己的学习过程。
设计模式分为三种:静态工厂模式,工厂方法模式,抽象工厂模式。其中抽象工厂模式是三个里面最抽象的,也是最具有一般性的。
在看教程的同时,花了一上午时间写了个例子。
例子配置如图:


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

具体工厂类:
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;
/**
* 生产跑车型车具体工厂类,有几种产品族,就有个具体工厂类 * @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;
/**
* 生产商务型车具体工厂类,有几种产品族,就有几个具体工厂类 * @author anfei
*
*/
public class FactoryBusiness implements Factory {
public BmwAuto factoryBmw() {
return new BusinessBwmAuto();
}
public BenzAuto factoryBenz() {
return new BusinessBenzAuto();
}
}


抽象产品类:
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();
}


具体产品类:

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("宝马跑车");
}
}


测试类:
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();
}
}


输出结果:

奔驰跑车
奔驰商务车
-----------------------
宝马跑车
宝马商务车
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics