码迷,mamicode.com
首页 > 其他好文 > 详细

大话设计模式第八章之简单工厂模式

时间:2017-03-07 00:22:56      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:ace   com   sys   exce   print   rate   case   res   throws   

  

 简单工厂模式

技术分享

 

工厂方法模式

    技术分享

 

 

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();
}
}

 

大话设计模式第八章之简单工厂模式

标签:ace   com   sys   exce   print   rate   case   res   throws   

原文地址:http://www.cnblogs.com/HJL085/p/6512553.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!