(1) 理解设计模式的基本概念
(2) 理解设计模式遵循的原则
(3) 掌握经典设计模式及应用
理解开-闭原则(Open-Closed Principle,OCP–对扩展开放-而对修改关闭
public interface Sound {
public abstract void playSound();
}
public class Dog implements Sound{
public void playSound(){
System.out.println("汪汪…汪汪");
}
}
public class Violin implements Sound{
public void playSound(){
System.out.println("小提琴.梁祝");
}
}
public class Simulator {
private Sound sound;
public void setSound(Sound sound){
this.sound=sound;
}
public void play(){
if (sound!=null){
sound.playSound();
}else {
System.out.println("没有可播放的声音");
}
}
}
public class Application {
public static void main(String[] args) {
Simulator simulator=new Simulator();
simulator.setSound(new Dog());
simulator.play();
simulator.setSound(new Violin());
simulator.play();
}
}
女娲架起了八卦炉(技术术语:建立工厂),开始造人。
过程如下:先捏泥巴成人形,再放入八卦炉里烤,最后扔到地上成长。时间烤短了,造出了“白种人”;时间烤长了,造出了“黑种人”;时间烤得不长不短,造出了“黄种人”。
public interface Human {
public void talk();
}
public class WhiteHuman implements Human{
public void talk(){
System.out.println("Hello");
}
}
public class YellowHuman implements Human{
public void talk(){
System.out.println("您好");
}
}
public class BlackHuman implements Human{
public void talk(){
System.out.println("i am a BlackHuman");
}
}
public class HumanFactory {
public static Human createHuman(String s){
Human human=null;
if (s.equals(new String("WhiteHuman"))){
human=new WhiteHuman();
}
else if (s.equals(new String("YellowHuman"))){
human=new YellowHuman();
}
else if (s.equals(new String("BlackHuman"))){
human=new BlackHuman();
}
return human;
}
}
public class NvWa {
public static void main(String[] args) {
Human human=null;
human=HumanFactory.createHuman("WhiteHuman");
human.talk();
human=HumanFactory.createHuman("YellowHuman");
human.talk();
human=HumanFactory.createHuman("BlackHuman");
human.talk();
}
}
public interface Human {
public void talk();
}
public class WhiteHuman implements Human{
public void talk(){
System.out.println("Hello");
}
}
public class YellowHuman implements Human{
public void talk(){
System.out.println("您好");
}
}
public class BlackHuman implements Human{
public void talk(){
System.out.println("i am a BlackHuman");
}
}
public interface HumanFactory {
public Human createHuman();
}
public class WhiteHumanF implements HumanFactory{
public Human createHuman(){
return new WhiteHuman();
}
}
public class YellowHumanF implements HumanFactory{
public Human createHuman(){
return new YellowHuman();
}
}
public class BlackHumanF implements HumanFactory{
public Human createHuman(){
return new BlackHuman();
}
}
public class NvWa {
public static void main(String[] args) {
HumanFactory humanFactory=null;
Human human=null;
humanFactory=new WhiteHumanF();
human=humanFactory.createHuman();
human.talk();
humanFactory=new YellowHumanF();
human=humanFactory.createHuman();
human.talk();
humanFactory=new BlackHumanF();
human=humanFactory.createHuman();
human.talk();
}
}
用户家里现有一台洗衣机,洗衣机使用交流电,现在用户新买了一台录音机,录音机只能使用直流电。由于供电系统供给用户家里的是交流电,因此用户需要用适配器将交流电转化直流电供录音机使用。
public interface AlternateCurrent {
public String giveAlternateCurrent();
}
public class PowerCompany implements AlternateCurrent{
public String giveAlternateCurrent(){
return "10101010101010101010";
}
}
public interface DirectCurrent {
public String giveDirectCurrent();
}
public class ElectricAdapter implements DirectCurrent {
AlternateCurrent out;
ElectricAdapter (AlternateCurrent out){
this.out=out;
}
public String giveDirectCurrent(){
String m=out.giveAlternateCurrent();
StringBuffer str=new StringBuffer(m);
for (int i = 0; i < str.length(); i++)
if (str.charAt(i)=='0') str.setCharAt(i,'1');
m=new String(str);
return m;
}
}
public class Wash {
String name;
Wash(){
name="洗衣机";
}
public void turnOn(AlternateCurrent a){
String s =a.giveAlternateCurrent();
System.out.println(name+"使用交流电:\n"+s);
System.out.println("开始洗衣物");
}
}
public class Recorder {
String name;
Recorder (){
name="录音机";
}
public void turnOn(DirectCurrent a){
String s=a.giveDirectCurrent();
System.out.println(name+"使用直流电:\n"+s);
System.out.println("开始录音");
}
}
public class Application {
public static void main(String[] args) {
AlternateCurrent aElectric =new PowerCompany();
Wash wash=new Wash();
wash.turnOn(aElectric);
DirectCurrent dElectric=new ElectricAdapter(aElectric);
Recorder recorder=new Recorder();
recorder.turnOn(dElectric);
}
}
在多个裁判负责打分的比赛中,每位裁判给选手一个得分,选手的最后得分是根据全体裁判的得分计算出来的。请给出几种计算选手得分的评分方案(策略),对于某次比赛,可以从你的方案中选择一种方案作为本次比赛的评分方案。
public interface Strategy {
public double computerAverage(double []a);
}
public class StrategyA implements Strategy{
public double computerAverage(double a[]){
double score=0,sum=0;
for(int i=0;i<a.length;i++) {
sum=sum+a[i];
}
score=sum/a.length;
return score;
}
}
import java.util.Arrays;
public class StrategyB implements Strategy{
public double computerAverage(double a[]){
if(a.length<=2)
return 0;
double score=0,sum=0;
Arrays.sort(a); //排序数组
for (int i=1; i<a.length-1; i++) {
sum=sum+a[i];
}
score=sum/(a.length-2);
return score;
}
}
public class AverageScore {
Strategy strategy;
public void setStrategy(Strategy strategy){
this.strategy=strategy;
}
public double getAverage(double a[]){
return strategy.computerAverage(a);
}
}
public class Application {
public static void main(String[] args) {
AverageScore game=new AverageScore();
game.setStrategy(new StrategyA());
double []a={9.12,9.25,8.87,9.99,6.99,7.88};
double aver=game.getAverage(a);
System.out.println(aver);
}
}