Engine engineHead = null;
public void add(Engine p) {
engineHead = p;
}
public void checkall() {
engineHead.getCheckingProject();
}
}
class Person {
public void checkall() {
}
}
class Factory {
public Person p;
public void setP(Person p1) {
p = p1;
}
public void command() {
p.checkall();
}
}
class Engine {
public Engine next;
public static int amount = 0;
protected String name = "";
protected CheckProject checkProject;
protected String state = "";
protected Observer observers[] = new Observer[10];
protected int observernum = 0;
public void addObserver(Observer observer) {
observers[observernum++] = observer;
}
public void setCheckingProject(CheckProject checkProject) {
this.checkProject = checkProject;
}
public Engine(String name) {
amount++;
this.name = name;
}
public void getCheckingProject() {
if (next != null)
next.getCheckingProject();
}
}
class AudiEngine extends Engine {
public AudiEngine(String name) {
super(name);
}
public void getCheckingProject() {
System.out.println("AudiEngine " + name + " will be cheking...\n");
state = "AudiEngine " + name + " is cheked|";
for (int i = 0; i < observernum; i++)
observers[i].updateEnginCheckingState(state);
checkProject.chekingProjectWorking();
super.getCheckingProject();
}
}
class BydEngine extends Engine {
public BydEngine(String name) {
super(name);
}
public void getCheckingProject() {
System.out.println("BydEngine " + name + " will be cheking...\n");
state = "BydEngine " + name + " is cheked|";
for (int i = 0; i < observernum; i++)
observers[i].updateEnginCheckingState(state);
checkProject.chekingProjectWorking();
super.getCheckingProject();
}
}
public class Test {
public static void main(String[] a) {
QualityDepartment qualityDepartment = new QualityDepartment();
HRDepartment hrDepartment = new HRDepartment();
EquipmentDepartment equipmentDepartment = new EquipmentDepartment();
TechnicalDepartment technicalDepartment = new TechnicalDepartment();
technicalDepartment.goWorking(qualityDepartment, hrDepartment, equipmentDepartment);
Worker worker = new Worker();
WorkerProxy workerProxy = new WorkerProxy();
workerProxy.workFor(worker);
worker.add(equipmentDepartment.getEngines());
Factory f = new Factory();
f.setP(workerProxy);
f.command();
qualityDepartment.showInfo();
hrDepartment.showInfo();
}
}
interface Observer {
public void updateEnginCheckingState(String state);
public void showInfo();
}
class QualityDepartment implements Observer {
String qualityinfo = "";
public void workFor(Worker worker) {
this.worker = worker;
}
public void checkall() {
Scanner sc = new Scanner(System.in);
System.out.println("what time is it?(0-24):");
int time_hour = sc.nextInt();
sc.close();
if (time_hour <= 17 && time_hour >= 9)
worker.checkall();
else if (time_hour > 24 || time_hour < 0)
System.out.print("error!\n");
else
System.out.print("not working time,worker is not there!\n");
}
}
class HRDepartment implements Observer {
String hrinfo = "";
public void goWorking(Observer qualityDepartment, Observer hrDepartment, EquipmentDepartment equipmentDepartment) {
CheckProject checkProject = new CheckProject("check oil pip");
VipCheckProject vipcheckProject = new VipCheckProject("check oil pip");
Engine machine;
machine = new BydEngine("No1Engine");
machine.setCheckingProject(checkProject);
machine.addObserver(qualityDepartment);
machine.addObserver(hrDepartment);
equipmentDepartment.addEquipment(machine);
machine = new AudiEngine("No2Engine");
machine.setCheckingProject(vipcheckProject);
machine.addObserver(qualityDepartment);
machine.addObserver(hrDepartment);
equipmentDepartment.addEquipment(machine);
machine = new BydEngine("No3Engine");
machine.setCheckingProject(vipcheckProject);
machine.addObserver(qualityDepartment);
machine.addObserver(hrDepartment);
equipmentDepartment.addEquipment(machine);
machine = new AudiEngine("No4Engine");
machine.setCheckingProject(checkProject);
machine.addObserver(qualityDepartment);
machine.addObserver(hrDepartment);
equipmentDepartment.addEquipment(machine);
machine = new BydEngine("No5Engine");
machine.setCheckingProject(checkProject);
machine.addObserver(qualityDepartment);
machine.addObserver(hrDepartment);
equipmentDepartment.addEquipment(machine);
ShipEngine shipEngine = new ShipEngine("No6Engine");
machine = new ShipEngineAdapter(shipEngine);
machine.setCheckingProject(vipcheckProject);
machine.addObserver(qualityDepartment);
machine.addObserver(hrDepartment);
equipmentDepartment.addEquipment(machine);
}
}
class CheckProject {
protected String name;
public CheckProject(String name) {
this.name = name;
}
public void chekingProjectWorking() {
System.out.print(name + " cheking project working...\n");
System.out.print("_________\n");
}
}
class VipCheckProject extends CheckProject {
public VipCheckProject(String name) {
super(name);
}
public void chekingProjectWorking() {
System.out.print("Dear VIP client,thank you very much,your engine will be cheching...\n");
System.out.print(name + " cheking vip project working...\n");
System.out.print("cheking end...\n");
System.out.print("_____\n");
}
}
class ShipEngine {
public String name = "";
public ShipEngine(String name) {
this.name = name;
}
public void findError() {
System.out.print(" cheking ShipEngine " + name + " to find error!\n");
}
}
class ShipEngineAdapter extends Engine {
ShipEngine shipEngine;
public ShipEngineAdapter(ShipEngine shipEngine) {
super(shipEngine.name);
this.shipEngine = shipEngine;
}
public void getCheckingProject() {
shipEngine.findError();
state = "ShipEngine " + name + " is cheked|";
for (int i = 0; i < observernum; i++)
observers[i].updateEnginCheckingState(state);
checkProject.chekingProjectWorking();
super.getCheckingProject();
}
}
class EquipmentDepartment {
Engine chainhead = null;
public void addEquipment(Engine engine) {
//头插法,为每一个加入的Engine添加next属性。
engine.next=chainhead;
chainhead = engine;
}
public Engine getEngines() {
return chainhead;
}
行为模式涉及到算法与对象之间责任的分配。着重描述他们的通信方式,刻画了运行时难以跟踪的复杂的控制流。
类行为模式 使用继承
对象行为模式 使用对象组合,注重对象之间的合作,即对象之间要相互了解
Mediator 中介者模式 实现对象之间的松耦合
### Chain of Responsibility 职责链模式 通过一条候选对象隐式的向一个对象发送请求。 适用:有多个对象可以处理一个请求,不用给每一个请求一一指定处理方式 D B
public class Execute { Execute next; public void setNext(Execute next) { this.next=next; } public void execute(String s) { if(next!=null) next.execute(s); } }
`import javax.swing.JOptionPane;public class HelpExecute extends Execute { public void execute(String s) { if(s.equals("欢迎"))
}`
`import javax.swing.JOptionPane;
public class OperationExecute extends Execute { public void execute(String s) { if(s.equals("操作"))
}
import javax.swing.JOptionPane;
public class OpenExecute extends Execute {
}
import java.awt.*;
import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
import javax.swing.*; import javax.swing.JPanel;
public class MyFrame extends JFrame implements ActionListener{ JMenuBar b=new JMenuBar(); JMenu sys=new JMenu("系统"); JMenuItem open=new JMenuItem("打开文件"); JMenuItem operation=new JMenuItem("操作"); JMenu h=new JMenu("帮助"); JMenuItem help_welcome=new JMenuItem("欢迎"); Execute action; public MyFrame(Execute action) { this.action=action; this.setJMenuBar(b); b.add(sys);b.add(h); sys.add(open);sys.add(operation); h.add(help_welcome); open.addActionListener(this); operation.addActionListener(this); help_welcome.addActionListener(this);
}
import java.awt.*;
import javax.swing.*;
public class Test {
} `
工厂(观察者模式 桥模式 适配器模式 代理模式 职责链模式 ) 使用职责链模式添加方便,为每一个对象添加属性(代替数组存储对象),通过父类的自我调用,递归解决分类问题(代替ifelse)。看起来十分有逼格。
`package Factor_equipment;
import java.util.Random; import java.util.Scanner;
class Worker extends Person {
}
class Person { public void checkall() {
}
class Factory { public Person p;
}
class Engine { public Engine next;
}
class AudiEngine extends Engine { public AudiEngine(String name) { super(name); }
}
class BydEngine extends Engine { public BydEngine(String name) { super(name); }
}
public class Test { public static void main(String[] a) { QualityDepartment qualityDepartment = new QualityDepartment(); HRDepartment hrDepartment = new HRDepartment(); EquipmentDepartment equipmentDepartment = new EquipmentDepartment(); TechnicalDepartment technicalDepartment = new TechnicalDepartment();
}
interface Observer { public void updateEnginCheckingState(String state);
}
class QualityDepartment implements Observer { String qualityinfo = "";
}
class WorkerProxy extends Person { Worker worker;
}
class HRDepartment implements Observer { String hrinfo = "";
}
class TechnicalDepartment {
}
class CheckProject { protected String name;
}
class VipCheckProject extends CheckProject { public VipCheckProject(String name) { super(name); } public void chekingProjectWorking() { System.out.print("Dear VIP client,thank you very much,your engine will be cheching...\n"); System.out.print(name + " cheking vip project working...\n"); System.out.print("cheking end...\n"); System.out.print("_____\n"); } }
class ShipEngine { public String name = "";
}
class ShipEngineAdapter extends Engine { ShipEngine shipEngine;
}
class EquipmentDepartment { Engine chainhead = null;
} `