抽象工厂模式

[color=red]工厂方法模式:
一个抽象产品类,可以派生出多个具体产品类。
一个抽象工厂类,可以派生出多个具体工厂类。
每个具体工厂类只能创建一个具体产品类的实例。

抽象工厂模式:
多个抽象产品类,每个抽象产品类可以派生出多个具体产品类。
一个抽象工厂类,可以派生出多个具体工厂类。
每个具体工厂类可以创建多个具体产品类的实例。

区别:
工厂方法模式只有一个抽象产品类,而抽象工厂模式有多个。
工厂方法模式的具体工厂类只能创建一个具体产品类的实例,而抽象工厂模式可以创建多个。[/color]

示例1:

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

示例2:

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;
}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值