设计模式作业一源代码江苏大学版本.docx

上传人:b****7 文档编号:25707528 上传时间:2023-06-11 格式:DOCX 页数:23 大小:16.42KB
下载 相关 举报
设计模式作业一源代码江苏大学版本.docx_第1页
第1页 / 共23页
设计模式作业一源代码江苏大学版本.docx_第2页
第2页 / 共23页
设计模式作业一源代码江苏大学版本.docx_第3页
第3页 / 共23页
设计模式作业一源代码江苏大学版本.docx_第4页
第4页 / 共23页
设计模式作业一源代码江苏大学版本.docx_第5页
第5页 / 共23页
点击查看更多>>
下载资源
资源描述

设计模式作业一源代码江苏大学版本.docx

《设计模式作业一源代码江苏大学版本.docx》由会员分享,可在线阅读,更多相关《设计模式作业一源代码江苏大学版本.docx(23页珍藏版)》请在冰豆网上搜索。

设计模式作业一源代码江苏大学版本.docx

设计模式作业一源代码江苏大学版本

《设计模式》作业

一、计数器简单工厂实现加减乘除

publicclassAddOperatorimplementsOperator{

@Override

publicdoublecalculate(doublea,doubleb)

{

//TODOAuto-generatedmethodstub

returna+b;

}

}

publicclassDivOperatorimplementsOperator{

@Override

publicdoublecalculate(doublea,doubleb)

{

//TODOAuto-generatedmethodstub

returna/b;

}

}

publicclassMulOperatorimplementsOperator{

@Override

publicdoublecalculate(doublea,doubleb)

{

//TODOAuto-generatedmethodstub

returna*b;

}

}

publicclassSubOperatorimplementsOperator{

@Override

publicdoublecalculate(doublea,doubleb)

{

//TODOAuto-generatedmethodstub

returna-b;

}

}

publicclassCalculateOperator{

publicstaticOperatorcreateOperate(Stringoperator){

Operatoropr=null;

if(operator.equals("+"))

opr=newAddOperator();

elseif(operator.equals("-"))

opr=newSubOperator();

elseif(operator.equals("*"))

opr=newMulOperator();

elseif(operator.equals("/"))

opr=newDivOperator();

returnopr;

}

}

publicinterfaceOperator{

doublecalculate(doublea,doubleb);

}

importjava.util.Scanner;

publicclassTest{

publicstaticvoidmain(String[]args){

Scannersc=newScanner(System.in);

System.out.println("请输入你要进行的运算符号:

");

Stringa=sc.nextLine();

System.out.println("输入数字1");

doubleb=sc.nextDouble();

System.out.println("输入数字2");

doublec=sc.nextDouble();

System.out.println("结果为:

"+CalculateOperator.createOperate(a).calculate(b,c));

}

}

 

一、计数器工厂模式实现加减乘除

publicclassOperation{

protecteddoublea;

protecteddoubleb;

publicdoublegetA(){

returna;

}

publicvoidsetA(doublea){

this.a=a;

}

publicdoublegetB(){

returnb;

}

publicvoidsetB(doubleb){

this.b=b;

}

publicdoublegetResult(){

return0;

}

}

 

publicclassOperationAddextendsOperation{

publicdoublegetResult(){

returna+b;

}

}

publicclassOperationSubextendsOperation{

publicdoublegetResult(){

returna-b;

}

}

publicclassOperationMulextendsOperation{

publicdoublegetResult(){

returna*b;

}

}

publicclassOperationDivextendsOperation{

publicdoublegetResult(){

returna/b;

}

}

publicinterfaceOperationFactory{

publicOperationcreateOperation();

}

publicclassAddOperationFactoryimplementsOperationFactory{

publicOperationcreateOpetation()

{

returnnewOperationAdd();

}

}

publicclassSubOperationFactoryimplementsOperationFactory{

publicOperationcreateOpetation()

{

returnnewOperationSub();

}

}

publicclassMulOperationFactoryimplementsOperationFactory{

publicOperationcreateOpetation()

{

returnnewOperationMul();

}

}

publicclassDivOperationFactoryimplementsOperationFactory{

publicOperationcreateOpetation()

{

returnnewOperationDiv();

}

}

publicclassSimpleFactoryMain{

publicstaticvoidmain(String[]args){

doublea,b;

Stringc;

Scannerd=newScanner(System.in);

a=d.nextInt();

c=d.next();

b=d.nextDouble();

OperationFactoryfactory=newAddOperationFactory();

Operationoperation=factory.createOperation();

operation.setA(a);

operation.setB(b);

System.out.println(operation.getResult());

 

}

}

 

二、计数器工厂模式实现十进制、二进制的加减乘除

publicclassOperation{

protectedStringa;

protectedStringb;

publicStringgetA(){

returna;

}

publicvoidsetA(Stringa){

this.a=a;

}

publicStringgetB(){

returnb;

}

publicvoidsetB(Stringb){

this.b=b;

}

publicStringgetResult(){

return0;

}

}

 

publicclassDecOperationAddextendsOperation{

 

publicStringgetResult(){

doubleA=Double.parseDouble(a);

doubleB=Double.parseDouble(b);

returnString.valueof(A+B);

}

}

publicclassDecOperationSubextendsOperation{

publicStringgetResult(){

doubleA=Double.parseDouble(a);

doubleB=Double.parseDouble(b);

returnString.valueof(A-B);

}

}

publicclassDecOperationMulextendsOperation{

publicStringgetResult(){

doubleA=Double.parseDouble(a);

doubleB=Double.parseDouble(b);

returnString.valueof(A*B);

}

}

publicclassDecOperationDivextendsOperation{

publicStringgetResult(){

doubleA=Double.parseDouble(a);

doubleB=Double.parseDouble(b);

returnString.valueof(A/B);

}

}

 

publicclassBinaryOperationAddextendsOperation{

publicStringgetResult(){

intx1=a.length;

intx2=b.length;

intp=0,t=0,m=0,i=0;

Stringc=newString();

Stringd=newString();

Stringr=newString();

for(inti=0;i

c.setCharAt(i,a.charAt(x1-i-1));

for(inti=0;i

d.setCharAt(i,b.charAt(x1-i-1));

if(x1>=x2)

{

p=x1;

for(i=x2;i

d.setCharAt(i,'0');

}

else

{

p=x2;

for(i=x1;i

c.setCharAt(i,'0');

}

for(i=0;i

{

r.setCharAt(i,'0');

t=(c.charAt(i)-'0')+(d.charAt(i)-'0')+m;//m为进位

if(t==2)

{

r.setCharAt(i,'0');

m=1;

}

elseif(t==3)

{

r.setCharAt(i,'1');

m=1;

}

else

{

r.setCharAt(i,t+'0');

m=0;

}

}

if(m==1)//判断最后是否要进1

{

r.setCharAt(p,'1');

for(i=0;i

c.setCharAt(i,a.charAt(p-i-1));

}

else

{

for(i=0;i

c.setCharAt(i,a.charAt(p-i-1));

}

}

}

publicclassBinaryOperationSubextendsOperation{

publicStringgetResult(){

}

}

publicclassBinaryOperationDivextendsOperation{

publicStringgetResult(){

}

}

publicclassBinaryOperationMulextendsOperation{

publicStringgetResult(){

}

}

 

publicinterfaceOperationFactory{

publicOperationcreateOperation();

}

publicclassDecOperationFactoryimplementsOperationFactory{

publicstaticOperationCreatDecOperation(charc){

Operationoper=null

switch(c)

{

case'+':

oper=newDecOperationAdd();

case'-':

oper=newDecOperationSub();

case'*':

oper=newDecOperationMul();

case'/':

oper=newDecOperationDiv();

}

returnoper;

}

}

publicclassBinaryOperationFactoryimplementsOperationFactory{

publicstaticOperationCreatDecOperation(charc){

Operationoper=null

switch(c)

{

case'+':

oper=newBinaryOperationAdd();

case'-':

oper=newBinaryOperationSub();

case'*':

oper=newBinaryOperationMul();

case'/':

oper=newBinaryOperationDiv();

}

returnoper;

}

}

publicclassSimpleFactoryMain{

publicstaticvoidmain(String[]args){

doublea,b;

Stringc;

Scanners=newScanner(System.in);

a=s.nextDouble();

c=s.next();

b=s.nextDouble();

d=c.charAt(0);

OperationFactoryfactory=newDecOperationFactory();

Operationoperation=factory.createOperation(d);

operation.setA(a);

operation.setB(b);

System.out.println(operation.getResult());

}

}

 

三、计数器抽象工厂实现十进制、二进制的加减乘除

publicclassoperand{

protectedTa;

protectedTb;

publicTgetA(){

returna;

}

publicvoidsetA(Ta){

this.a=a;

}

publicTgetB(){

returnb;

}

publicvoidsetB(Tb){

this.b=b;

}

}

publicclassDecoperandextendsoperand{}

publicclassbinaryoperandextendsoperand{}

publicclassoperator{

publicTgetResult(Ta,Tb,charc){}

}

publicclassDecoperatorAddextendsoperator{

publicdoublegetResult(doublea,doubleb)

{returna+b;}

}

publicclassDecoperatorSubextendsoperator{

publicdoublegetResult(doublea,doubleb)

{returna-b;}

}

publicclassDecoperatorMulextendsoperator{

publicdoublegetResult(doublea,doubleb)

{returna*b;}

}

publicclassDecoperatorDivextendsoperator{

publicdoublegetResult(doublea,doubleb)

{returna/b;}

}

publicclassbinaryoperatorAddextendsoperator{

publicStringgetResult(Stringa,Stringb)

{

Stringc=newString();

Stringd=newString();

Stringe=newString();

intk1=a.length;

intk2=b.length;

intp=0,t=0,m=0,i=0;

for(inti=0;i

c.setCharAt(i,a.charAt(k1-i-1));

for(inti=0;i

d.setCharAt(i,b.charAt(k1-i-1));

if(k1>=k2)

{

p=k1;

for(i=k2;i

d.setCharAt(i,'0');

}

else

{

p=k2;

for(i=k1;i

c.setCharAt(i,'0');

}

for(i=0;i

{

e.setCharAt(i,'0');

t=(c.charAt(i)-'0')+(d.charAt(i)-'0')+m;//m为进位

if(t==2)

{

e.setCharAt(i,'0');

m=1;

}

elseif(t==3)

{

e.setCharAt(i,'1');

m=1;

}

else

{

e.setCharAt(i,t+'0');

m=0;

}

}

if(m==1)//判断最后是否要进1

{

e.setCharAt(p,'1');

for(i=0;i

c.setCharAt(i,a.charAt(p-i-1));

}

else

{

for(i=0;i

c.setCharAt(i,a.charAt(p-i-1));

}

}

}

publicclassbinaryoperatorSubextendsoperator{

publicStringgetResult(Stringa,Stringb){}

}

 

publicclassbinaryoperatorMulextendsoperator{

publicStringgetResult(Stringa,Stringb){}

}

publicclassbinaryoperatorDivextendsoperator{

publicStringgetResult(Stringa,Stringb){}

}

publicinterfaceFactory{

publicoperandcreateOperand();

publicoperatorcreateOperand();

}

publicclassDecFactoryimplementsFactory{

publicoperandcreateOperand()

{

returnnewDecoperand();

}

publicoperatorcreateOperand(charc)

{

Operatoroper=null

switch(c)

{

case'+':

oper=newDecOperatorAdd();

case'-':

oper=newDecOperatorSub();

case'*':

oper=newDecOperatorMul();

case'/':

oper=newDecOperatorDiv();

}

returnoper;

}

}

 

publicclassbinaryFactoryimplementsFactory{

publicoperandcreateOperand()

{

returnnewbinaryoperand();

}

publicoperatorcreateOperand(charc)

{

Operatoroper=null

switch(c)

{

case'+':

oper=newBinaryOperatorAdd();

case'-':

oper=newBinaryOperatorSub();

case'*':

oper=newBinaryOperatorMul();

case'/':

oper=newBinaryOperatorDiv();

}

returnoper;

}

}

publicclassSimpleFactoryMain{

publicstaticvoidmain(String[]args){

Factoryfactory=newbinaryFactory();

operandoperand1=factory.createOperand();

operatoroperator1=factory.createOperator('+');

operand.setA("1111");

operand.setB("1111");

System.out.println(operator.getResult(operand.getA,operand.getB));

}

}

 

四、简单工厂实现后花园,添加葡萄、草莓、苹果

publicinterfaceFruit{

voids

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 幼儿教育 > 唐诗宋词

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1