大话设计模式_工厂方法
[java]
package com.wzs.design;
/**
* 大话设计模式--page73 工厂方法
*
* @author Administrator
*
*/
public class FactoryMethod {
public static void main(String[] args) {
IFactory iFactory = new UndergraduateFactory();// 大学生雷锋工厂
iFactory.createLeiFeng().buyRice();
iFactory.createLeiFeng().wash();
iFactory.createLeiFeng().sweep();
System.out.println();
iFactory = new VolunteerFactory();// 志愿者雷锋工厂
iFactory.createLeiFeng().buyRice();
iFactory.createLeiFeng().wash();
iFactory.createLeiFeng().sweep();
}
}
/*
* 创建雷锋的工厂
*/
inte易做图ce IFactory {
LeiFeng createLeiFeng();
}
/*
* 大学生雷锋工厂
*/
class UndergraduateFactory implements IFactory {
@Override
public LeiFeng createLeiFeng() {
return new Undergraduate();
}
}
/*
* 志愿者雷锋工厂
*/
class VolunteerFactory implements IFactory {
@Override
public LeiFeng createLeiFeng() {
return new Volunteer();
}
}
/*
* 雷锋
*/
abstract class LeiFeng {
public abstract void sweep();
public abstract void wash();
public abstract void buyRice();
}
/*
* 大学生雷锋
*/
class Undergraduate extends LeiFeng {
@Override
public void buyRice() {
System.out.println("大学生帮老人买米.");
}
@Override
public void sweep() {
System.out.println("大学生帮老人洗衣服.");
}
@Override
public void wash() {
System.out.println("大学生帮老人扫地.");
}
}
/*
* 志愿者雷锋
*/
class Volunteer extends LeiFeng {
@Override
public void buyRice() {
System.out.println("志愿者帮老人买米.");
}
@Override
public void sweep() {
System.out.println("志愿者帮老人洗衣服.");
}
@Override
public void wash() {
System.out.println("志愿者帮老人扫地.");
}
}
补充:软件开发 , Java ,