Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1699808
  • 博文数量: 210
  • 博客积分: 10013
  • 博客等级: 上将
  • 技术积分: 2322
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-25 15:56
文章分类

全部博文(210)

文章存档

2011年(34)

2010年(121)

2009年(37)

2008年(18)

我的朋友

分类: Java

2011-02-25 16:13:25

package hello;

/**
 * 抽象工厂简单示例
 * @author HZ20232
 *
 */

public class FactoryMethodPattern{
    public static void main(String args[]){
        Factory longFactory = new LongWeaponFactory();
        Gun longGun = longFactory.createGun();
        Connon longConnon = longFactory.createConnon();
        longGun.Beng();
        longConnon.Beng();
        
        Factory shorFactory = new ShortWeaponFactory();
        Gun shorGun = shorFactory.createGun();
        Connon shortConnon = shorFactory.createConnon();
        shorGun.Beng();
        shortConnon.Beng();
    }
}
interface Gun{
    void Beng();
}
interface Connon{
    void Beng();
}
class LongGun implements Gun{
    public void Beng(){
        System.out.println("Big Gun Beng!");
    }
}
class ShortGun implements Gun{
    public void Beng(){
        System.out.println("Little Gun Beng!");
    }
}
class LongConnon implements Connon{
    public void Beng(){
        System.out.println("Big Connon Beng!");
    }
}
class ShortConnon implements Connon{
    public void Beng(){
        System.out.println("Little Connon Beng!");
    }
}
interface Factory{
    Gun createGun();
    Connon createConnon();
}
class LongWeaponFactory implements Factory{
    public Gun createGun(){
        return new LongGun();
    }
    public Connon createConnon(){
        return new LongConnon();
    }
}
class ShortWeaponFactory implements Factory{
    public Gun createGun(){
        return new ShortGun();
    }
    public Connon createConnon(){
        return new ShortConnon();
    }
}


package hello;

import java.io.*;

/**
 * 简单工厂简单示例
 * @author HZ20232
 *
 */

public class FactoryMethodPattern{
    public static void main(String args[]){
        try {
            System.out.println("请输入数字1:");
            String strA = new BufferedReader(new InputStreamReader(System.in))
                    .readLine();
            System.out.println("请输入运算符:");
            String stroperate = new BufferedReader(new InputStreamReader(
                    System.in)).readLine();
            System.out.println("请输入数字2:");
            String strB = new BufferedReader(new InputStreamReader(System.in))
                    .readLine();
            Operation oper;
            OperationFactory factory = new OperationFactory();
            oper=factory.getOperation(stroperate);
            oper.setA(Integer.parseInt(strA));
            oper.setB(Integer.parseInt(strB));
            int result = oper.operator();
            System.out.println(result);
        } catch (Exception e) {
  
        }
    }
}
abstract class Operation{
    int operandA;
    int operandB;
    public Operation(){
        this.setA(0);
        this.setB(0);
    }
    public Operation(int a,int b){
        this.setA(a);
        this.setB(b);
    }
    public void setA(int numA){
        this.operandA = numA;
    }
    public int getA(){
        return this.operandA;
    }
    public void setB(int numB){
        this.operandB = numB;
    }
    public int getB(){
        return this.operandB;
    }
    public abstract int operator();
}
class AddOperation extends Operation{
    public int operator(){
        return this.operandA+this.operandB;
    }
}
class MulOperation extends Operation{
    public int operator(){
        return this.operandA*this.operandB;
    }
}
interface Factory{
    Operation getOperation(String a);
}
class OperationFactory implements Factory{
    public Operation getOperation(String operator){
        Operation operation = null;
        if("+".equalsIgnoreCase(operator))
            operation = new AddOperation();
        else if("*".equalsIgnoreCase(operator))
            operation = new MulOperation();
        return operation;
    }
}


阅读(1817) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2011-03-06 08:58:12

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com