Computer Model:

package com.ccl.factory;

import java.util.LinkedList;
import java.util.List;

public class Computer {

	private List<CPU> cpus;

	private List<Memory> memorys;

	private int cpuNumber;

	private int memoryNumber;

	public Computer(int cpuNumber, int memoryNumber) {

		cpus = new LinkedList<CPU>();

		memorys = new LinkedList<Memory>();

		this.cpuNumber = cpuNumber;

		this.memoryNumber = memoryNumber;

	}

	public int getCpuNumber() {
		return cpuNumber;
	}

	public List<CPU> getCpus() {
		return cpus;
	}

	public int getMemoryNumber() {
		return memoryNumber;
	}

	public List<Memory> getMemorys() {
		return memorys;
	}

	public void setCpus(List<CPU> cpus) {
		this.cpus = cpus;
	}

	public void setMemorys(List<Memory> memorys) {
		this.memorys = memorys;
	}

	public String toString() {

		return "CPU:" + this.cpus + "\nMemory:" + this.memorys;
	}

}

CPU:

package com.ccl.factory;

public class CPU {

	public CPU() {
		System.out.println("new a CPU.");
	}

	public String toString() {

		return "CPU";
	}
}


 

Memory:

package com.ccl.factory;

public class Memory {

	public Memory() {
		System.out.println("new a memory.");
	}

	public String toString() {

		return "Memory";
	}
}


 

以上都是扯蛋:

入正题:

package com.ccl.factory;

public interface ComputerFactory {

	public void buildCPU();

	public void buildMemory();

	public Computer getComputer();

}

package com.ccl.factory;

import java.util.LinkedList;
import java.util.List;

public class SimpleComputerFactoryImpl implements ComputerFactory {

	private Computer computer;

	public SimpleComputerFactoryImpl() {

		computer = new Computer(1, 2);
	}

	@Override
	public void buildCPU() {

		List<CPU> list = new LinkedList<CPU>();
		for (int i = 0; i < this.computer.getCpuNumber(); i++) {
			list.add(new CPU());
		}
		this.computer.setCpus(list);
	}

	@Override
	public void buildMemory() {
		List<Memory> list = new LinkedList<Memory>();
		for (int i = 0; i < this.computer.getMemoryNumber(); i++) {
			list.add(new Memory());
		}
		this.computer.setMemorys(list);
	}

	@Override
	public Computer getComputer() {
		return this.computer;
	}

}


 

package com.ccl.factory;

import java.util.LinkedList;
import java.util.List;

public class SimpleComputerFactoryImpl implements ComputerFactory {

	private Computer computer;

	public SimpleComputerFactoryImpl() {

		computer = new Computer(1, 2);
	}

	@Override
	public void buildCPU() {

		List<CPU> list = new LinkedList<CPU>();
		for (int i = 0; i < this.computer.getCpuNumber(); i++) {
			list.add(new CPU());
		}
		this.computer.setCpus(list);
	}

	@Override
	public void buildMemory() {
		List<Memory> list = new LinkedList<Memory>();
		for (int i = 0; i < this.computer.getMemoryNumber(); i++) {
			list.add(new Memory());
		}
		this.computer.setMemorys(list);
	}

	@Override
	public Computer getComputer() {
		return this.computer;
	}

}


 

package com.ccl.factory;

import java.util.LinkedList;
import java.util.List;

public class ComplexComputerFactoryImpl implements ComputerFactory {

	private Computer computer;

	public ComplexComputerFactoryImpl() {

		computer = new Computer(3, 6);
	}

	@Override
	public void buildCPU() {

		List<CPU> list = new LinkedList<CPU>();
		for (int i = 0; i < this.computer.getCpuNumber(); i++) {
			list.add(new CPU());
		}
		this.computer.setCpus(list);
	}

	@Override
	public void buildMemory() {
		List<Memory> list = new LinkedList<Memory>();
		for (int i = 0; i < this.computer.getMemoryNumber(); i++) {
			list.add(new Memory());
		}
		this.computer.setMemorys(list);
	}

	@Override
	public Computer getComputer() {
		return this.computer;
	}

}
package com.ccl.factory;

public class ComputerFactoryCompany {

	public ComputerFactoryCompany() {
	}

	public Computer build(ComputerFactory c) {

		c.buildCPU();
		c.buildMemory();

		return c.getComputer();

	}

}


Demo:

package com.ccl.factory;

public class ComputerDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		ComputerFactoryCompany company = new ComputerFactoryCompany();

		Computer computer = company.build(new SimpleComputerFactoryImpl());

		System.out.println(computer);

		Computer newcomputer = company.build(new ComplexComputerFactoryImpl());

		System.out.println(newcomputer);

	}

}




 

作者:chengchanglun 发表于2012-4-10 11:51:01 原文链接
阅读:4780 评论:0 查看评论