STUDYnotesshnu / Design-Pattern

学习笔记
0 stars 0 forks source link

行为型模式 #5

Open BronzeJi opened 4 years ago

BronzeJi commented 4 years ago

行为模式涉及到算法与对象之间责任的分配。着重描述他们的通信方式,刻画了运行时难以跟踪的复杂的控制流。

类行为模式 使用继承

对象行为模式 使用对象组合,注重对象之间的合作,即对象之间要相互了解

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("欢迎"))

            JOptionPane.showMessageDialog(null,"欢迎使用本系统,版本1.0");    

            else
    super.execute(s);
}

}`

`import javax.swing.JOptionPane;

public class OperationExecute extends Execute { public void execute(String s) { if(s.equals("操作"))

    JOptionPane.showMessageDialog(null,"您选择了操作菜单"); 

    else
    super.execute(s);
}

} import javax.swing.JOptionPane;

public class OpenExecute extends Execute {

public void execute(String s)
{
    if(s.equals("打开文件"))

    JOptionPane.showMessageDialog(null,"您选择了打开文件菜单");   

    else
    super.execute(s);
}

} 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);

    this.setSize(500,300);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible( true);
}

/* (非 Javadoc)
 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
 */
public void actionPerformed(ActionEvent arg0) {
    // TODO 自动生成方法存根
    action.execute(arg0.getActionCommand());        
}

} import java.awt.*;

import javax.swing.*;

public class Test {

public static void main(String[] args) {

    //MyFrame f=new MyFrame();
    Execute a=new OperationExecute();
    Execute b=new HelpExecute();
    Execute c=new OpenExecute();
    a.setNext(b);b.setNext(c);
    JFrame frame = new MyFrame(a);
}

} `

工厂(观察者模式 桥模式 适配器模式 代理模式 职责链模式 ) 使用职责链模式添加方便,为每一个对象添加属性(代替数组存储对象),通过父类的自我调用,递归解决分类问题(代替ifelse)。看起来十分有逼格。 image

`package Factor_equipment;

import java.util.Random; import java.util.Scanner;

class Worker extends Person {

   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  updateEnginCheckingState(String  state)  {
           qualityinfo  =  qualityinfo  +  state;
   }

   public  void  showInfo()  {
           System.out.print("QualityDepartment  knows:"  +  qualityinfo  +  "\n");
   }

}

class WorkerProxy extends Person { Worker worker;

   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  updateEnginCheckingState(String  state)  {
           hrinfo  =  hrinfo  +  state;
   }

   public  void  showInfo()  {
           System.out.print("HRDepartment  knows:"  +  hrinfo  +  "\n");
   }

}

class TechnicalDepartment {

   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;
   }

} `