package org.ustc.factory; public class LeiFeng { public void buyRice(){ System.out.println("卖大米"); } public void wash(){ System.out.println("洗衣"); } public void Sweep(){ System.out.println("扫地"); } }
package org.ustc.factory; public class Volunteer extends LeiFeng { }
package org.ustc.factory; public class Undergraduate extends LeiFeng { }
package org.ustc.factory; public interface Factory { public LeiFeng createInstance(); }
package org.ustc.factory; public class CraeteVolunteer implements Factory { @Override public LeiFeng createInstance() { // TODO Auto-generated method stub return new Volunteer(); } }
package org.ustc.factory; public class CreateUnderGradutate implements Factory { @Override public LeiFeng createInstance() { // TODO Auto-generated method stub return new Undergraduate(); } }
package org.ustc.factory; public class Main { public static void main(String[] args) { Factory fact = new CreateUnderGradutate(); LeiFeng student = fact.createInstance(); student.buyRice(); } }
大话设计模式 工厂方法
最新推荐文章于 2024-04-16 20:29:55 发布