Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1700227
  • 博文数量: 210
  • 博客积分: 10013
  • 博客等级: 上将
  • 技术积分: 2322
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-25 15:56
文章分类

全部博文(210)

文章存档

2011年(34)

2010年(121)

2009年(37)

2008年(18)

我的朋友

分类: Java

2011-03-16 16:40:13

package com.utstar.pattern.abstractfactory;

public interface Cpu {
    public abstract void calculate();
}


package com.utstar.pattern.abstractfactory;

public class IntelCpu implements Cpu{
    public int pins;
    
    public IntelCpu(int p){
        this.pins = p;
    }
    public void calculate(){
        System.out.println("Intel Cpu.");
    }
}


package com.utstar.pattern.abstractfactory;

public class AmdCpu implements Cpu{
    public int pins;
    
    public AmdCpu(int p){
        this.pins = p;
    }
    public void calculate(){
        System.out.println("AmdCpu.");
    }
}


package com.utstar.pattern.abstractfactory;

public interface MainBoard {
    public abstract void install();
}


package com.utstar.pattern.abstractfactory;

public class GigaMainBoard implements MainBoard{
    public int pins;
    
    public GigaMainBoard(int p){
        this.pins = p;
    }
    public void install(){
        System.out.println("技嘉主板");
    }
}


package com.utstar.pattern.abstractfactory;

public class MsiMainBoard implements MainBoard{
    public int pins;
    
    public MsiMainBoard(int p){
        this.pins = p;
    }
    
    public void install(){
        System.out.println("微星主板");
    }
}


package com.utstar.pattern.abstractfactory;

public interface Factory {
    public abstract Cpu createCpu();
    public abstract MainBoard createMainBoard();
}


package com.utstar.pattern.abstractfactory;

public class IntelFactory implements Factory{
    public Cpu createCpu(){
        return new AmdCpu(936);
    }
    public MainBoard createMainBoard(){
        return new GigaMainBoard(936);
    }
}


package com.utstar.pattern.abstractfactory;

public class AmdFactory implements Factory{
    public Cpu createCpu(){
        return new AmdCpu(1189);
    }
    public MainBoard createMainBoard(){
        return new GigaMainBoard(1189);
    }
}


package com.utstar.pattern.abstractfactory;

public class TestAbstractFactory{
    public static void main(String args[]){
        Cpu cpu1 = new IntelFactory().createCpu();
        MainBoard board1 = new IntelFactory().createMainBoard();
        cpu1.calculate();
        board1.install();
    }
}


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