工厂模式(类的实例化在工厂类的方法中)

class Cpu{
private String description;
double price;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
class Memory{
private String description;
double price;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}

class Disk{
private String description;
double price;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}

class ComputerFactory{
public Cpu getCpu() {
System.out.println("cpu生产好了");
return new Cpu();
}
public Memory getMemory() {
System.out.println("内存生产好了");
return new Memory();
}
public Disk getDisk() {
System.out.println("硬盘生产好了");
return new Disk();
}
}
public class FactoryTest {

public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("下面我们生产一台电脑");
ComputerFactory computerFactory = new ComputerFactory();
computerFactory.getCpu();
computerFactory.getMemory();
computerFactory.getDisk();
}
}

posted @ 2020-01-17 15:50  集大软辅孙忠  阅读(295)  评论(0编辑  收藏  举报