概述
大话设计模式
- 各个模式包括:需求 + 各个实现版本
- 简单工厂
- 策略模式
- 装饰者模式
- 代理模式
- 工厂模式
- 原型模式
- 模板设计模式
各个模式包括:需求 + 各个实现版本
简单工厂
实现两数的加减乘除
- 无脑写,典型的面向过程编码,同时if会判断多次
System.out.println("请输入数字A:");
Scanner scanner = new Scanner(System.in);
int A = scanner.nextInt();
System.out.println("请选择运算符号(+,-,*,/):");
String B = scanner.next();
System.out.println("请输入数字B");
int C = scanner.nextInt();
int D = 0;
if(B.equals("+")){
D = A+C;
}
if(B.equals("-")){
D = A-C;
}
if(B.equals("*")){
D = A*C;
}
if(B.equals("/")){
D = A*C;
}
System.out.println("结果是:"+D);
2.使用switch
System.out.println("请输入数字A:");
Scanner scanner = new Scanner(System.in);
int strNumberA = scanner.nextInt();
System.out.println("请选择运算符号(+,-,*,/):");
String strOperate = scanner.next();
System.out.println("请输入数字B");
int strNumberB = scanner.nextInt();
int result = 0;
switch (strOperate) {
case "+":
result = strNumberA + strNumberA;
break;
case "-":
result = strNumberA - strNumberB;
break;
case "*":
result = strNumberA * strNumberB;
break;
case "/":
if (strNumberB != 0) {
result = strNumberA / strNumberB;
} else {
throw new RuntimeException("除数不能为0");
}
break;
default:
System.out.println("运算符号输入有误,请检查");
break;
}
System.out.println("计算的结果为:" + result);
} catch (Exception e) {
System.out.println("您的输入有错:" + e.getMessage());
}
3.将运算和显示进行分离
操作类Operation
public class Operation {
public static int GetResult(int numberA,int numberB,String operate){
int result = 0;
switch (operate) {
case "+":
result = numberA + numberB;
break;
case "-":
result = numberA - numberB;
break;
case "*":
result = numberA * numberB;
break;
case "/":
if (numberB != 0) {
result = numberA / numberB;
} else {
throw new RuntimeException("除数不能为0");
}
break;
default:
System.out.println("运算符号输入有误,请检查");
break;
}
return result;
}
}
主函数
try {
System.out.println("请输入数字A:");
Scanner scanner = new Scanner(System.in);
int strNumberA = scanner.nextInt();
System.out.println("请选择运算符号(+,-,*,/):");
String strOperate = scanner.next();
System.out.println("请输入数字B");
int strNumberB = scanner.nextInt();
int result = 0;
result = Operation.GetResult(strNumberA,strNumberB,strOperate);
System.out.println("计算的结果为:" + result);
} catch (Exception e) {
System.out.println("您的输入有错:" + e.getMessage());
}
4.抽象Operation,使用简单工厂
Operation 相当于抽象为 两个数据 和 通过getResult方法获取一种结果
private int numberA = 0;
private int numberB = 0;
public int getResult(){
return 0;
}
public int getNumberA() {
return numberA;
}
public void setNumberA(int numberA) {
this.numberA = numberA;
}
public int getNumberB() {
return numberB;
}
public void setNumberB(int numberB) {
this.numberB = numberB;
}
加法
public class OperationAdd extends Operation {
@Override
public int getResult(){
return getNumberA()+getNumberB();
}
}
减法
public class OperationSub extends Operation{
@Override
public int getResult(){
return getNumberA()-getNumberB();
}
}
乘法
public class OperationMul extends Operation {
@Override
public int getResult(){
return getNumberA()*getNumberB();
}
}
除法
public class OperationDiv extends Operation {
@Override
public int getResult(){
if(getNumberB()==0)
throw new RuntimeException("除数不能为0.");
return getNumberA()+getNumberB();
}
}
简单工厂
public class OperationFactory {
public static Operation createOperate(String operate) {
Operation operation = null;
switch (operate) {
case "+":
operation = new OperationAdd();
break;
case "-":
operation = new OperationSub();
break;
case "*":
operation = new OperationMul();
break;
case "/":
operation = new OperationDiv();
break;
default:
throw new RuntimeException("不支持对应的运算符!!!");
}
return operation;
}
}
主类
System.out.println("请输入数字A:");
Scanner scanner = new Scanner(System.in);
int strNumberA = scanner.nextInt();
System.out.println("请选择运算符号(+,-,*,/):");
String strOperate = scanner.next();
System.out.println("请输入数字B");
int strNumberB = scanner.nextInt();
int result = 0;
Operation operation = OperationFactory.createOperate(strOperate);
operation.setNumberA(strNumberA);
operation.setNumberB(strNumberB);
result = operation.getResult();
System.out.println("计算的结果为:" + result);
策略模式
商场收银
1.无脑写,如果增加打折呢?当然也可以参考简单工厂
public class DealPay {
public double total = 0.0;
//每个商品的单价
public double txtPrice;
//每个商品对应的数量
public int txtNum;
private void btnOn_Click(double txtPrice,int txtNum){
double totalPrices = txtPrice*txtNum;
total += totalPrices;
System.out.println("单价:"+txtPrice+",数量:"+txtNum+",合计:"+totalPrices+",总计:"+total);
}
public static void main(String[] args) {
DealPay dealPay = new DealPay();
dealPay.btnOn_Click(100,2);
}
}
2.简单工厂更改
收钱接口
public interface CashSuper {
double acceptCash(double money);
}
正常收钱
public class CashNormal implements CashSuper {
@Override
public double acceptCash(double money) {
return money;
}
}
打折收
多了一个折扣率
public class CashRebate implements CashSuper {
private double moneyRabate = 1d;
@Override
public double acceptCash(double money) {
return money * moneyRabate;
}
public CashRebate(String moneyRabate){
this.moneyRabate = Double.parseDouble(moneyRabate);
}
}
300返100
public class CashReturn implements CashSuper {
private double moneyCondition = 300.0d;
private double moneyReturn = 100.0d;
public CashReturn(String moneyCondition,String moneyReturn){
this.moneyCondition = Double.parseDouble(moneyCondition);
this.moneyReturn = Double.parseDouble(moneyReturn);
}
@Override
public double acceptCash(double money) {
double result = money;
if(money >= moneyCondition)
result = money - Math.floor(money/moneyCondition)*moneyReturn;
return result;
}
}
收银-简单工厂
public class CashFactory {
public static CashSuper createCashAccept(String type) {
CashSuper cashSuper = null;
switch (type) {
case "正常收费":
cashSuper = new CashNormal();
break;
case "满300返100":
cashSuper = new CashReturn("300", "100");
break;
case "打8折":
cashSuper = new CashRebate("0.8");
break;
default:
break;
}
return cashSuper;
}
}
主类
public double total = 0.0;
//每个商品的单价
public double txtPrice;
//每个商品对应的数量
public int txtNum;
public void btnOn_Click(String type){
CashSuper cashSuper = CashFactory.createCashAccept(type);
double totalPrices = cashSuper.acceptCash(300);
total += totalPrices;
System.out.println("单价:"+txtPrice+",数量:"+txtNum+",合计:"+totalPrices+",总计:"+total);
}
public static void main(String[] args) {
DealPay dealPay = new DealPay();
dealPay.btnOn_Click("满300返100");
}
3.增加策略
满足了策略模式,但是策略的对象获取又回到了客户端进行判断的模式
相当于增加了一个持有 CashSuper 实例的一个对象 CashContext
策略器
public class CashContext {
private CashSuper cashSuper;
public CashContext(CashSuper cashSuper){
this.cashSuper = cashSuper;
}
public double getResult(double money){
return cashSuper.acceptCash(money);
}
}
CashSuper
CashNormal
CashRebate
CashReturn
都和2一样,省略
主类
public double total = 0.0;
//每个商品的单价
public double txtPrice;
//每个商品对应的数量
public int txtNum;
public void btnOn_Click(String type) {
CashContext cashContext = null;
switch (type) {
case "正常收费":
cashContext = new CashContext(new CashNormal());
break;
case "满300返100":
cashContext = new CashContext(new CashReturn("300", "100"));
break;
case "打8折":
cashContext = new CashContext(new CashRebate("0.8"));
break;
default:
break;
}
double totalPrices = cashContext.getResult(300);
total += totalPrices;
System.out.println("单价:" + txtPrice + ",数量:" + txtNum + ",合计:" + totalPrices + ",总计:" + total);
}
public static void main(String[] args) {
DealPay dealPay = new DealPay();
dealPay.btnOn_Click("满300返100");
}
4.策略+简单工厂
将3中的switch移动到CashContext中
和3相比,改变的只是CashContext 和主类
在需求发生变化的时候,还是需要修改CashContext中的switch方法的,后面会用反射实现解决这个问题
CashContext
public class CashContext {
private CashSuper cashSuper;
public CashContext(String type){
switch (type) {
case "正常收费":
this.cashSuper = new CashNormal();
break;
case "满300返100":
this.cashSuper = new CashReturn("300", "100");
break;
case "打8折":
this.cashSuper = new CashRebate("0.8");
break;
default:
break;
}
}
public double getResult(double money){
return cashSuper.acceptCash(money);
}
}
主类
public double total = 0.0;
//每个商品的单价
public double txtPrice;
//每个商品对应的数量
public int txtNum;
public void btnOn_Click(String type) {
double totalPrices = new CashContext(type).getResult(300);
total += totalPrices;
System.out.println("单价:" + txtPrice + ",数量:" + txtNum + ",合计:" + totalPrices + ",总计:" + total);
}
public static void main(String[] args) {
DealPay dealPay = new DealPay();
dealPay.btnOn_Click("打8折");
}
装饰者模式
给人换衣服
1.普通写法,增加其他装扮,需要修改Person,违反了开放-封闭原则
人
public class Person {
private String name;
public Person(String name){
this.name = name;
}
public void wearTShirts(){
System.out.println("大T恤");
}
public void wearBigTrouser(){
System.out.println("裤衩");
}
public void wearSneakers(){
System.out.println("破球鞋");
}
public void wearSuit(){
System.out.println("西装");
}
public void wearTie(){
System.out.println("领带");
}
public void wearLeatherShoes(){
System.out.println("皮鞋");
}
public void show(){
System.out.println("装扮的"+name);
}
}
主类
public static void main(String[] args) {
Person person = new Person("xiaoma");
System.out.println("第一种装扮:");
person.wearBigTrouser();
person.wearSneakers();
person.wearSuit();
System.out.println("第二种装扮:");
person.wearTShirts();
person.wearLeatherShoes();
person.wearTie();
}
2.将Person和 装扮Finery分开,装扮的过程还是在主类中
装扮接口
public interface Finery {
void show();
}
大裤衩
public class BigTrouser implements Finery {
@Override
public void show() {
System.out.println("裤衩");
}
}
T恤
public class TShirts implements Finery {
@Override
public void show() {
System.out.println("大T恤");
}
}
人
public class Person {
private String name;
public Person(String name){
this.name = name;
}
public void show(){
System.out.println("装扮的"+name);
}
}
主类进行装饰
public static void main(String[] args) {
Person person = new Person("xiaoma");
Finery trouser = new BigTrouser();
Finery shirt = new TShirts();
System.out.println("第一种装扮:");
trouser.show();
shirt.show();
System.out.println("第二种装扮:");
trouser.show();
shirt.show();
person.show();
}
3.装饰者模式,将 装饰 作为 人 的 一个功能
人组件
public interface PersonComponent {
void show();
}
人
public class Person implements PersonComponent{
private String name;
public Person(String name){
this.name = name;
}
public Person(){};
public void show(){
System.out.println("装扮的"+name);
}
}
装饰者 继承Person
public class Finery extends Person {
protected Person person;
//装扮
public void decorate(Person person){
this.person = person;
}
public void show(){
if(person!=null){
person.show();
}
}
}
大裤衩 注意调用 super.show() 两种装扮只会执行一次!!!因为T恤的super里的person是大裤衩,
大裤衩的super里面的person才是最初的person,通过debug比较容易理解
所以这个super是谁,要看装饰的过程
public class BigTrouser extends Finery {
@Override
public void show() {
System.out.println("裤衩");
super.show();
}
}
T恤
public class TShirts extends Finery {
public void show() {
System.out.println("大T恤");
super.show();
}
}
主类
public static void main(String[] args) {
Person person = new Person("xiaoma");
Finery finery = new Finery();
Finery trouser = new BigTrouser();
Finery shirt = new TShirts();
System.out.println("第一种装扮:");
trouser.decorate(person);
shirt.decorate(trouser);
finery.decorate(shirt);
finery.show();
}
代理模式
内容:一个小伙追求校花的过程
1.普通无脑写法,小伙心中有她,做出一些举动
追求者
public class Pursuit {
private SchoolGirl mm;
public Pursuit(SchoolGirl mm){
this.mm = mm;
}
public void giveDolls(){
System.out.println(mm.getName()+"送你洋娃娃");
}
public void giveFlowers(){
System.out.println(mm.getName()+"送你鲜花");
}
public void giveChocolate(){
System.out.println(mm.getName()+"送你巧克力");
}
}
校花
public class SchoolGirl {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
采取行动
我喜欢被动
public class Client {
public static void main(String[] args) {
SchoolGirl jiaojiao = new SchoolGirl();
jiaojiao.setName("李娇娇");
Pursuit zhuojiayi = new Pursuit(jiaojiao);
zhuojiayi.giveChocolate();
zhuojiayi.giveDolls();
zhuojiayi.giveFlowers();
}
}
2.增加一个中间人 ,它具有和追求者一样的行为,并持有追求者的实例
共同行为的抽象接口
public interface IgiveGift {
void giveDolls();
void giveFlowers();
void giveChocolate();
}
小伙实现接口
public class Pursuit implements IgiveGift {
private SchoolGirl mm;
public Pursuit(SchoolGirl mm){
this.mm = mm;
}
@Override
public void giveDolls() {
System.out.println(mm.getName()+"送你洋娃娃");
}
@Override
public void giveFlowers() {
System.out.println(mm.getName()+"送你鲜花");
}
@Override
public void giveChocolate() {
System.out.println(mm.getName()+"送你巧克力");
}
}
代理的人实现接口,并持有小伙
public class Proxy implements IgiveGift{
private Pursuit pursuit;
public Proxy(SchoolGirl schoolGirl){
this.pursuit = new Pursuit(schoolGirl);
}
@Override
public void giveDolls() {
pursuit.giveDolls();
}
@Override
public void giveFlowers() {
pursuit.giveFlowers();
}
@Override
public void giveChocolate() {
pursuit.giveChocolate();
}
}
校花
public class SchoolGirl {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
被行动
public class Client {
public static void main(String[] args) {
SchoolGirl jiaojiao = new SchoolGirl();
jiaojiao.setName("李娇娇");
Proxy daili = new Proxy(jiaojiao);
daili.giveChocolate();
daili.giveDolls();
daili.giveFlowers();
}
}
3.(java反射代理)从前有个老板,自己卖衣服,后来发现太累了,找了个代理帮他卖,但是代理讲价便宜10块钱
老板的想法
public interface IBoss {//接口
int yifu(String size);
}
老板自己干
public class Boss implements IBoss {
@Override
public int yifu(String size){
System.err.println("天猫小强旗舰店,老板给客户发快递----衣服型号:"+size);
//这件衣服的价钱,从数据库读取
return 50;
}
}
老板开张
public class SaleAction {
@Test
public void saleByBossSelf() throws Exception {
IBoss boss = new Boss();
System.out.println("老板自营!");
int money = boss.yifu("xxl");
System.out.println("衣服成交价:" + money);
}
}
老板累了,找了代理商
public class ProxyBoss {
/**
* 对接口方法进行代理
*/
@SuppressWarnings("unchecked")
public static <T> T getProxy(final int discountCoupon,
final Class<?> interfaceClass, final Class<?> implementsClass)
throws Exception {
return (T) Proxy.newProxyInstance(interfaceClass.getClassLoader(),
new Class[] { interfaceClass }, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method,
Object[] args) throws Throwable {
Integer returnValue = (Integer) method.invoke(
implementsClass.newInstance(), args);// 调用原始对象以后返回的值
return returnValue - discountCoupon;
}
});
}
}
代理帮忙干
public class ProxySaleAction {
/**
*使用代理,在这个代理中,只代理了Boss的yifu方法
*定制化业务,可以改变原接口的参数、返回值等
*/
@Test
public void saleByProxy() throws Exception {
IBoss boss = ProxyBoss.getProxy(10,IBoss.class, Boss.class);// 将代理的方法实例化成接口
//IBoss boss = new Boss();// 将代理的方法实例化成接口
System.out.println("代理经营!");
int money = boss.yifu("xxl");// 调用接口的方法,实际上调用方式没有变
System.out.println("衣服成交价:" + money);
}
}
工厂模式
雷锋和喜欢学习雷锋的大学生以及志愿者进行一下义务劳动
1.简单工程模式
雷锋自己的行为
public class LeiFeng {
public void sweep(){
System.out.println("扫地");
}
public void wash(){
System.out.println("洗衣");
}
public void buyRice(){
System.out.println("买米");
}
}
学习雷锋的大学生
public class Undergraduate extends LeiFeng{
}
学习雷锋的志愿者
public class Volunteer extends LeiFeng {
}
生产雷锋类似的同志们
public static LeiFeng createLeiFeng(String type){
LeiFeng result = null;
switch (type){
case "学雷锋的大学生":
result = new Undergraduate();
break;
case "社区志愿者":
result = new Volunteer();
break;
}
return result;
}
雷锋和雷锋们进行劳动
public static void main(String[] args) {
LeiFeng xueLeiFeng = new Undergraduate();
xueLeiFeng.buyRice();
xueLeiFeng.sweep();
xueLeiFeng.wash();
//三个人代替他做这些事情
LeiFeng student1 = new Undergraduate();
student1.buyRice();
LeiFeng student2 = new Undergraduate();
student2.sweep();
LeiFeng student3 = new Undergraduate();
student3.wash();
//其实客户端不必关心是谁来,只要是学雷锋的人就行了
LeiFeng studentA = SimpleFactory.createLeiFeng("学雷锋的大学生");
studentA.buyRice();
LeiFeng studentB = SimpleFactory.createLeiFeng("学雷锋的大学生");
studentB.sweep();
LeiFeng studentC = SimpleFactory.createLeiFeng("学雷锋的大学生");
studentC.wash();
//实例化的时候都写出了工厂的代码,是坏代码的味道
}
2.工厂模式实现 其实相当于给工厂有加了一级 普通工厂变更为了 创建 大学生的工厂和 创建志愿者的工厂
雷锋和大学生和志愿者和1是一样的
创建雷锋工厂的抽象类
public interface IFactory {
LeiFeng createLeiFeng();
}
创建大学生的工厂
public class UndergraduateFactory implements IFactory {
@Override
public LeiFeng createLeiFeng() {
return new Undergraduate();
}
}
创建志愿者的工厂
public class VolunteerFactory implements IFactory {
@Override
public LeiFeng createLeiFeng() {
return new Volunteer();
}
}
这些雷锋干好事
发现这么写new 操作都在各个工厂中了,确实简洁了,维护也方便
public static void main(String[] args) {
IFactory factory = new UndergraduateFactory();
LeiFeng student = factory.createLeiFeng();
student.buyRice();
student.sweep();
student.wash();
}
原型模式
复印简历
1.无脑写复印
简历
public class Resume {
private String name;
private String sex;
private String age;
private String timeArea;
private String company;
public Resume(String name){
this.name = name;
}
//设置个人信息
public void setPersonalInfo(String sex,String age){
this.sex = sex;
this.age = age;
}
//设置个人经历
public void setWorkExperience(String timeArea,String company){
this.timeArea = timeArea;
this.company = company;
}
//显示
public void display(){
System.out.println(name+" "+sex+" "+ age);
System.out.println("工作经历:"+timeArea+" "+company);
}
}
无脑复印
public static void main(String[] args) {
Resume a = new Resume("大鸟");
a.setPersonalInfo("男","29");
a.setWorkExperience("1998-2000","xx公司");
Resume b = new Resume("大鸟");
b.setPersonalInfo("男","29");
b.setWorkExperience("1998-2000","xx公司");
Resume c = new Resume("大鸟");
c.setPersonalInfo("男","29");
c.setWorkExperience("1998-2000","xx公司");
a.display();
b.display();
c.display();
//引用传递,其实还是一份简历
Resume d = a;
Resume e = a;
a.display();
d.display();
e.display();
}
2.体会Object的clone() 方法得到的是另一个对象
原型的抽象类
其中的abstrat方法子类必须实现,要不然子类还要是抽象类
public abstract class Prototype implements Cloneable{
private String id;
public Prototype(){};
public Prototype(String id){
this.id = id;
}
public void setId(String id) {
this.id = id;
}
public String getId(){
return this.id;
}
public abstract Prototype Myclone() throws CloneNotSupportedException;
}
原型复制实现子类
public class ConcretePrototype1 extends Prototype {
public ConcretePrototype1(String id){
super(id);
}
@Override
public Prototype Myclone() throws CloneNotSupportedException {
return (Prototype)this.clone();
}
}
使用clone复制体会输出结果
public static void main(String[] args) throws CloneNotSupportedException {
ConcretePrototype1 p1 = new ConcretePrototype1("I");
ConcretePrototype1 c1 = (ConcretePrototype1) p1.Myclone();
System.out.println(p1.getId());
System.out.println(c1.getId());
p1.setId("II");
System.out.println(p1.getId());
System.out.println(c1.getId());
}
3.使用clone来复制简历
简历
同1中的简历
复制简历
public static void main(String[] args) throws CloneNotSupportedException {
Resume a = new Resume("大鸟");
a.setPersonalInfo("男","29");
a.setWorkExperience("1998-2000","xx公司");
Resume b = (Resume)a.clone();
b.setWorkExperience("1998-2006","xx公司");
Resume c = (Resume) a.clone();
c.setPersonalInfo("男","24");
a.display();
b.display();
c.display();
}
4.体会clone对象作用于对象中的复杂类型的时候,只是复制了引用
简历
public class Resume implements Cloneable{
private String name;
private String sex;
private String age;
private WorkExperience workExperience;
public Resume(String name){
this.name = name;
workExperience = new WorkExperience();
}
//设置个人信息
public void setPersonalInfo(String sex,String age){
this.sex = sex;
this.age = age;
}
//设置个人经历
public void setWorkExperience(String timeArea,String company){
workExperience.setWorkDate(timeArea);
workExperience.setCompany(company);
}
//显示
public void display(){
System.out.println(name+" "+sex+" "+ age);
System.out.println("工作经历:"+workExperience.getWorkDate()+" "+workExperience.getCompany());
}
@Override
public Object clone() throws CloneNotSupportedException {
return (Object)super.clone();
}
}
简历中的复合对象工作经历
public class WorkExperience {
private String workDate;
private String company;
public String getWorkDate() {
return workDate;
}
public void setWorkDate(String workDate) {
this.workDate = workDate;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
}
复印简历,体会结果
public static void main(String[] args) throws CloneNotSupportedException {
Resume a = new Resume("大鸟");
a.setPersonalInfo("男","29");
a.setWorkExperience("1998-2000","xx公司");
Resume b = (Resume)a.clone();
b.setWorkExperience("1998-2006","xx公司");
Resume c = (Resume) a.clone();
c.setPersonalInfo("男","24");
a.display();
b.display();
c.display();
}
5.处理4的问题,则需要修改简历类中的clone()方法,相当于在clone中对复合对象再进行一次clone后,将目前的其他基本属性赋值给它
@Override
public Object clone() throws CloneNotSupportedException {
Resume obj = new Resume(this.workExperience);
obj.name = this.name;
obj.sex = this.sex;
obj.age = this.age;
return obj;
}
public Resume(WorkExperience workExperience) throws CloneNotSupportedException {
this.workExperience = (WorkExperience) workExperience.clone();
}
模板设计模式
答问卷
1.普通的写法
问卷
public class TestPaper {
public void question1(){
System.out.println("第一个问题是:...");
}
public void question2(){
System.out.println("第一个问题是:...");
}
public void question3(){
System.out.println("第一个问题是:...");
}
}
A来答题
public class TestPaperA extends TestPaper {
public void question1(){
super.question1();
System.out.println("b");
}
public void question2(){
super.question2();
System.out.println("a");
}
public void question3(){
super.question3();
System.out.println("c");
}
}
B来答题
public class TestPaperB extends TestPaper {
public void question1(){
super.question1();
System.out.println("A");
}
public void question2(){
super.question2();
System.out.println("B");
}
public void question3(){
super.question3();
System.out.println("C");
}
}
主类
public static void main(String[] args) {
cn.yyzy.model.paper.two.TestPaper testPaperA = new TestPaperA();
testPaperA.question1();
testPaperA.question2();
testPaperA.question3();
TestPaper testPaperB = new TestPaperB();
testPaperB.question1();
testPaperB.question2();
testPaperB.question3();
}
2.使用模板模式,既然一定要回答,其实只要答案就行了,题目和要答题的动作都可以在模板中
问卷
public class TestPaper {
public void question1() {
System.out.println("第一个问题是:..." + answer1());
}
public void question2() {
System.out.println("第二个问题是:..." + answer2()
);
}
public void question3() {
System.out.println("第三个问题是:..." + answer3()
);
}
public String answer1() {
return "";
}
public String answer2() {
return "";
}
public String answer3() {
return "";
}
}
A答
public class TestPaperA extends TestPaper {
public String answer1(){
return "a";
}
public String answer2(){
return "b";
}
public String answer3(){
return "c";
}
}
B答
public class TestPaperB extends TestPaper {
public String answer1(){
return "c";
}
public String answer2(){
return "b";
}
public String answer3(){
return "a";
}
}
主类和1一样
最后
以上就是凶狠爆米花为你收集整理的大话设计模式java解读各个模式包括:需求 + 各个实现版本的全部内容,希望文章能够帮你解决大话设计模式java解读各个模式包括:需求 + 各个实现版本所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复