A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 霍振鹏 中级黑马   /  2014-4-17 17:03  /  1164 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

谁能告我一下,抽象工厂模式中的“抽象”相对工厂模式来说,它的“抽象”表现在哪儿?多谢!

3 个回复

倒序浏览
如果是一个手机工厂,他既可以生产苹果手机,又可以生产三星手机,还可以生产国产手机,那么他的生产手机的方法一定为抽象的
回复 使用道具 举报
package com.gengu.抽象工厂;  
/**
* 抽象产品
* */  
public abstract class Product {  
    public abstract void dosomething();  
}  
具体产品类

  
/**
* 产品
* */  
class ProductA1 extends Product{  
  
    @Override  
    public void dosomething() {  
        System.out.println("这是产品");  
    }  
}
回复 使用道具 举报
本帖最后由 Tking 于 2014-4-17 17:16 编辑

抽象工厂模式就是是简单工厂模式的扩展,它们主要的区别在于需要创建对象的复杂程度上。
简单工厂,只给商品创建接口,
而抽象工厂,是给工厂也创建接口
你需要什么样的工厂他就给你什么样的工厂,并提供该的特有产品
在抽象工厂模式中,抽象产品可能是一个或多个,从而构成一个或多个产品族。 在只有一个产品族的情况下,抽象工厂模式实际上退化到工厂方法模式。
  1. 1.// 产品 Plant接口
  2. 2.public interface Plant {
  3. 3.}
  4. 4.
  5. 5.// 具体产品PlantA,PlantB
  6. 6.public class PlantA implements Plant {
  7. 7.
  8. 8. public PlantA() {
  9. 9. System.out.println(" create PlantA ! ");
  10. 10. }
  11. 11.
  12. 12. public void doSomething() {
  13. 13. System.out.println(" PlantA do something ");
  14. 14. }
  15. 15.}
  16. 16.
  17. 17.public class PlantB implements Plant {
  18. 18. public PlantB() {
  19. 19. System.out.println(" create PlantB ! ");
  20. 20. }
  21. 21.
  22. 22. public void doSomething() {
  23. 23. System.out.println(" PlantB do something ");
  24. 24. }
  25. 25.}
  26. 26.
  27. 27.// 产品 Fruit接口
  28. 28.public interface Fruit {
  29. 29.}
  30. 30.
  31. 31.// 具体产品FruitA,FruitB
  32. 32.public class FruitA implements Fruit {
  33. 33. public FruitA() {
  34. 34. System.out.println(" create FruitA ! ");
  35. 35. }
  36. 36.
  37. 37. public void doSomething() {
  38. 38. System.out.println(" FruitA do something ");
  39. 39. }
  40. 40.}
  41. 41.
  42. 42.public class FruitB implements Fruit {
  43. 43. public FruitB() {
  44. 44. System.out.println(" create FruitB ! ");
  45. 45. }
  46. 46.
  47. 47. public void doSomething() {
  48. 48. System.out.println(" FruitB do something ");
  49. 49. }
  50. 50.}
  51. 51.
  52. 52.// 抽象工厂方法
  53. 53.public interface AbstractFactory {
  54. 54. public Plant createPlant();
  55. 55.
  56. 56. public Fruit createFruit();
  57. 57.}
  58. 58.
  59. 59.// 具体工厂方法
  60. 60.public class FactoryA implements AbstractFactory {
  61. 61. public Plant createPlant() {
  62. 62. return new PlantA();
  63. 63. }
  64. 64.
  65. 65. public Fruit createFruit() {
  66. 66. return new FruitA();
  67. 67. }
  68. 68.}
  69. 69.
  70. 70.public class FactoryB implements AbstractFactory {
  71. 71. public Plant createPlant() {
  72. 72. return new PlantB();
  73. 73. }
  74. 74.
  75. 75. public Fruit createFruit() {
  76. 76. return new FruitB();
  77. 77. }
  78. 78.}
复制代码


评分

参与人数 1技术分 +1 收起 理由
zzkang0206 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马