2.设计模式用例(一)
2018-12-16 本文已影响0人
月巴月巴
- 工厂模式: 根据不同的歌手,生产出对应的歌。歌手是Visitor, 歌曲是Acceptor
package designPattern;
import static designPattern.Cast.JIANG_DA_WEI;
import static designPattern.Cast.XIAO_YUE_YUE;
enum Cast {
XIAO_YUE_YUE, JIANG_DA_WEI;
}
interface Song {
void showLyric();
}
class WuHuan implements Song {
@Override
public void showLyric() {
System.out.println("啊~啊~~啊~五环,你比四环多一环");
}
}
class MuDan implements Song {
@Override
public void showLyric() {
System.out.println("啊~啊~~啊~牡丹,百花丛中最鲜艳");
}
}
class SongFactory {
Song getSong(Cast cast) {
switch (cast) {
case XIAO_YUE_YUE: return new WuHuan();
case JIANG_DA_WEI: return new MuDan();
default: return null;
}
}
}
public class FactoryStudy {
public static void main(String[] args) {
SongFactory songFactory = new SongFactory();
Song wuHuan = songFactory.getSong(XIAO_YUE_YUE);
Song mudan = songFactory.getSong(JIANG_DA_WEI);
wuHuan.showLyric();
mudan.showLyric();
}
}
/// result
// 啊~啊~~啊~五环,你比四环多一环
// 啊~啊~~啊~牡丹,百花丛中最鲜艳
- 访问者模式: Visitor 访问 不同的Element会有不同的操作 (添加了对象结构)
public interface Element {
void accept(Visitor visitor);
}
public interface Visitor {
void visit(Student student);
void visit(Programmer programmer);
}
public class Student implements Element {
private int score;
private String name;
public Student(int score, String name) {
this.score = score;
this.name = name;
}
public void accept(Visitor visitor) {
visitor.visit(this);
}
public int getScore() {
return score;
}
public String getName() {
return name;
}
}
public class Programmer implements Element {
private int codeQuality;
private String name;
public Programmer(int codeQuality, String name) {
this.codeQuality = codeQuality;
this.name = name;
}
public void accept(Visitor visitor) {
visitor.visit(this);
}
public int getCodeQuality() {
return codeQuality;
}
public String getName() {
return name;
}
}
public class Critics implements Visitor {
public void visit(Programmer programmer) {
if (programmer.getCodeQuality() > 80) {
System.out.println(programmer.getName() + " is a good programmer");
} else {
System.out.println(programmer.getName() + " is a bad programmer");
}
}
public void visit(Student student) {
if (student.getScore() > 80) {
System.out.println(student.getName() + " is a good student");
} else {
System.out.println(student.getName() + " is a bad student");
}
}
}
public class ObjectStructure {
private List<Element> elements = new ArrayList<>();
public void accept(Visitor visitor) {
for (Element element: elements) {
element.accept(visitor);
}
}
public void addElement(Element element) {
elements.add(element);
}
}
public class VisitorStudy {
public static void main(String[] args) {
ObjectStructure objectStructure = new ObjectStructure();
objectStructure.addElement(new Student(50, "S1"));
objectStructure.addElement(new Programmer(50, "P1"));
objectStructure.addElement(new Student(80, "S2"));
objectStructure.addElement(new Programmer(90, "P2"));
objectStructure.addElement(new Student(90, "S3"));
Critics critics = new Critics();
objectStructure.accept(critics);
}
}
/// output
// S1 is a bad student
// P1 is a bad programmer
// S2 is a bad student
// P2 is a good programmer
// S3 is a good student
- 适配器模式
/**
* 目标操作是 keyDown
* 物理键盘能直接实现 按键的功能,而触屏只有tap的功能
* 这时候创建一个触屏的Adapter,由Adapter实现keyDown的功能
*/
interface InputTool {
void keyDown(String s);
}
class Keyboard implements InputTool{
public void keyDown(String key) {
System.out.println("Keyboard: Key " + key + " is pressed");
}
}
class VirtualKeyboard {
void tap(String s) {
System.out.println("Phone: key " + s + " is touched");
}
}
class VirtualKeyBoardAdapter implements InputTool {
private VirtualKeyboard virtualKeyboard;
VirtualKeyBoardAdapter(VirtualKeyboard virtualKeyboard) {
this.virtualKeyboard = virtualKeyboard;
}
@Override
public void keyDown(String s) {
virtualKeyboard.tap(s);
}
}
public class AdapterStudy {
public static void main(String[] args) {
InputTool inputTool = new VirtualKeyBoardAdapter(new VirtualKeyboard());
inputTool.keyDown("1");
}
}
4.享元模式
package designPattern;
import java.util.HashMap;
/**
* make a guitar tab reader (guitar score)
* The Note on each string will be saved into a hash map, whose key is the string's label (EADGBE)
* When the string is plucked for the second time, the note will be retrieved from the hashmap instead of the constructor
*/
enum GuitarString {
L_E, A, D, G, B, H_E
}
class TabNote {
private GuitarString guitarString;
private int fret;
public GuitarString getGuitarString() {
return guitarString;
}
public void setGuitarString(GuitarString guitarString) {
this.guitarString = guitarString;
}
public int getFret() {
return fret;
}
public void setFret(int fret) {
this.fret = fret;
}
public void playSound() {
System.out.println("Plays " + guitarString.name() + " string, fret " + fret + "~~ ");
}
}
class TabNoteProvider {
HashMap<GuitarString, TabNote> notePool = new HashMap<>();
TabNote getNote(GuitarString s, int fret) {
TabNote note;
if (notePool.containsKey(s)) {
note = notePool.get(s);
note.setFret(fret);
} else {
note = new TabNote();
note.setFret(fret);
note.setGuitarString(s);
notePool.put(note.getGuitarString(), note);
}
return note;
}
}
public class FlyWeightStudy {
public static void main(String[] args) {
//there are 7 notes, but only two TabNote objects are created with the help of the hash map
TabNoteProvider provider = new TabNoteProvider();
provider.getNote(GuitarString.A, 3).playSound();
provider.getNote(GuitarString.A, 3).playSound();
provider.getNote(GuitarString.G, 0).playSound();
provider.getNote(GuitarString.G, 0).playSound();
provider.getNote(GuitarString.G, 2).playSound();
provider.getNote(GuitarString.G, 2).playSound();
provider.getNote(GuitarString.G, 0).playSound();
}
}