简单工厂模式
工厂方法模式
package com.dp.factory; interface IFactory { Calculator CreateCalculator(); } class AddFactory implements IFactory { @Override public Calculator CreateCalculator() { return new CalculatorAdd(); } } class SubFactory implements IFactory { @Override public Calculator CreateCalculator() { return new CalculatorMinus(); } } class MulFactory implements IFactory { @Override public Calculator CreateCalculator() { return new CalculatorMultiply(); } } class DivFactory implements IFactory { @Override public Calculator CreateCalculator() { return new CalculatorDivide(); } } abstract class Calculator { protected double numberA=0; protected double numberB=0; public double getNumberA() { return numberA; } public void setNumberA(double numberA) { this.numberA = numberA; } public double getNumberB() { return numberB; } public void setNumberB(double numberB) { this.numberB = numberB; } public abstract double getResult() throws Exception; } class CalculatorAdd extends Calculator { @Override public double getResult() { double result=numberA+numberB; return result; } } class CalculatorMinus extends Calculator { @Override public double getResult() { double result=numberA-numberB; return result; } } class CalculatorMultiply extends Calculator { @Override public double getResult() { double result=numberA*numberB; return result; } } class CalculatorDivide extends Calculator { @Override public double getResult() throws Exception { double result=numberA/numberB; if(numberB==0) throw new Exception("除数不能为0"); return result; } }
package com.dp.leifengFactory;
//简单工厂模式实现
public abstract class LeiFeng { public void sweep() { System.out.println("扫地"); } public void wash() { System.out.println("洗衣服"); } public void buyRice() { System.out.println("买米"); } } class Undergraduate extends LeiFeng { } class Volunteer extends LeiFeng { } class SimpleFactory { public static LeiFeng CreateLeiFeng(String type) { LeiFeng result=null; switch(type) { case "学雷锋的大学生": result=new Undergraduate(); break; case "志愿者": result=new Volunteer(); break; } return result; } }
package com.dp.leifengFactory; public class Client { public static void main(String[] args) { LeiFeng StudentA=SimpleFactory.CreateLeiFeng("学雷锋的大学生"); LeiFeng StudentB=SimpleFactory.CreateLeiFeng("学雷锋的大学生"); LeiFeng StudentC=SimpleFactory.CreateLeiFeng("学雷锋的大学生"); } }
使用工厂方法
package com.dp.leifengFactory; public interface IFactory { LeiFeng createLeifeng(); } class UnderGraduateFactory implements IFactory { @Override public LeiFeng createLeifeng() { // TODO Auto-generated method stub return new Undergraduate(); } } class VolunteerFactory implements IFactory { @Override public LeiFeng createLeifeng() { // TODO Auto-generated method stub return new Volunteer(); } } abstract class LeiFeng { public void sweep() { System.out.println("扫地"); } public void wash() { System.out.println("洗衣服"); } public void buyRice() { System.out.println("买米"); } } class Undergraduate extends LeiFeng { } class Volunteer extends LeiFeng { }
package com.dp.leifengFactory; public class Client { public static void main(String[] args) { IFactory factory=new UnderGraduateFactory(); LeiFeng lf=factory.createLeifeng(); lf.buyRice(); } }
时间: 2024-11-05 15:58:45