1 /** 2 * 行为 3 * @author lucksheep 4 * 5 */ 6 public class LeiFeng { 7 8 public void Sweep(){ 9 System.out.print("\t扫地\t"); 10 } 11 12 public void Laundry(){ 13 System.out.print("\t洗衣\t"); 14 } 15 16 public void Cook(){ 17 System.out.print("\t做饭\t"); 18 } 19 }
LeiFeng.java
1 /** 2 * 工厂接口 3 * @author lucksheep 4 * 5 */ 6 public interface IFactory { 7 8 LeiFeng createLeiFeng(); 9 }
IFactory.java
1 /** 2 * 学习雷锋的人 3 * @author lucksheep 4 * 5 */ 6 public class UndergraduateFactory implements IFactory { 7 8 @Override 9 public LeiFeng createLeiFeng() { 10 return new Undergraduate(); 11 } 12 13 }
UndergraduateFactory.java
1 /** 2 * 志愿者 3 * @author lucksheep 4 * 5 */ 6 public class Undergraduate extends LeiFeng { 7 8 }
Undergraduate.java
1 public class VolunteerFactory implements IFactory { 2 3 @Override 4 public LeiFeng createLeiFeng() { 5 return new Volunteer(); 6 } 7 8 }
VolunteerFactory.java
1 /** 2 * 志愿者 3 * @author Administrator 4 * 5 */ 6 public class Volunteer extends LeiFeng { 7 8 }
Volunteer
1 /** 2 * 3 * 场景:学雷锋做好事 4 * 角色:大学生、志愿者等 5 * 行为:扫地、洗衣、做饭等 6 * @author lucksheep 7 * 8 */ 9 public class Test { 10 11 public static void main(String[] args) { 12 13 IFactory ifactory=new VolunteerFactory(); 14 LeiFeng lf=ifactory.createLeiFeng(); 15 16 lf.Sweep(); 17 lf.Cook(); 18 lf.Laundry(); 19 20 } 21 22 }
Test.java
结果:
额个人理解啥的就不说了,这些都是看文档后按照上面打的,边学边用,在实际开发中还是很好用的............................
有兴趣的联系我邮箱[email protected]
时间: 2024-11-07 13:20:30