Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1743529
  • 博文数量: 297
  • 博客积分: 285
  • 博客等级: 二等列兵
  • 技术积分: 3006
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-06 22:04
个人简介

Linuxer, ex IBMer. GNU https://hmchzb19.github.io/

文章分类

全部博文(297)

文章存档

2020年(11)

2019年(15)

2018年(43)

2017年(79)

2016年(79)

2015年(58)

2014年(1)

2013年(8)

2012年(3)

分类: Java

2017-02-06 14:44:17

这个desgin pattern 的意思是factory of factories, 不同于factory pattern, 根据用户的input 来new 不同的对象,这里是直接new 不同的factory。然后让不同的工厂来负责生产不同的对象。
我觉得已经有让人找不到干活的代码到底在哪里的意思了。
Usage in JDK: 
javax.xml.parsers.DocumentBuilderFactory #newInstance()
javax.xml.transform.TransformerFactory #newInstance()
javax.xml.xpath.XPathFactory #newInstance()


点击(此处)折叠或打开

  1. package abstractfactory;

  2. interface IMobile{
  3.     public void cost();
  4.     public void pictureCapacity();
  5.     public void batterPower();
  6. }

  7. class Lenovo implements IMobile{

  8.     @Override
  9.     public void cost(){
  10.         System.out.println("Lenovo Cost starts from 10000");
  11.     }

  12.     @Override
  13.     public void pictureCapacity(){
  14.         System.out.println("Lenovo camera capacity starts from 10MP");

  15.     }

  16.     @Override
  17.     public void batterPower(){
  18.         System.out.println("Lenovo battery power starts from 2500 MAH");
  19.     }

  20.     @Override
  21.     public String toString(){
  22.         return "Lenovo [toString()= " +super.toString() +" ]";
  23.     }
  24. }

  25. class Samsung implements IMobile{

  26.     @Override
  27.     public void cost(){
  28.         System.out.println("Samsung Cost starts from 6000");
  29.     }

  30.     @Override
  31.     public void pictureCapacity(){
  32.         System.out.println("Samsung camera capacity starts from 4MP");

  33.     }

  34.     @Override
  35.     public void batterPower(){
  36.         System.out.println("Samsung battery power starts from 1200 MAH");
  37.     }

  38. }

  39. interface IMobileFactory{
  40.     public IMobileFactory createMobile(String type);
  41. }

  42. class MobileFactory implements IMobileFactory{

  43.     @Override
  44.     public IMobileFactory createMobile(String type){
  45.         IMobileFactory obj = null;
  46.         if("lenf".equalsIgnoreCase(type)){
  47.             obj = new LenovoMobileFactory();
  48.         }else if("samf".equalsIgnoreCase(type)){
  49.             obj = new SamsungMobileFactory();
  50.         }
  51.         return obj;
  52.     }
  53. }

  54. class LenovoMobileFactory extends MobileFactory{
  55.     Lenovo createLenovoMobile(){
  56.         return new Lenovo();
  57.     }
  58. }

  59. class SamsungMobileFactory extends MobileFactory{
  60.     Samsung createMobileFactory(){
  61.         return new Samsung();
  62.     }
  63. }

  64. public class AbstractFactoryDemo {
  65.     public static void main(String[] args){
  66.         MobileFactory factory = new MobileFactory();
  67.         LenovoMobileFactory lmf = (LenovoMobileFactory)factory.createMobile("lenf");
  68.         Lenovo ln = (Lenovo)lmf.createLenovoMobile();
  69.         ln.batterPower();
  70.         ln.pictureCapacity();
  71.     }
  72. }


阅读(479) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~