第三次的作业: > emmm,感觉要写作业的时间花的越来越长了呢。+ 要做项目 + 写文档 + 弄 PPT(每天都好困呐) ## 1. Strategy 模式 以下是完成要求的代码: ```java import java.util.*; interface IBehaviour { public int moveCommand(Robot a); } class Robot { private String name; private double x, y; IBehaviour behavior; double hp; public Robot(String name, IBehaviour behavior, double x, double y) { this.name = name; this.behavior = behavior; this.x = x; this.y = y; hp = 100.0; } public double getPositionx() { return this.x; } public double getPositiony() { return this.y; } public String getname() { return this.name; } public void move() { behavior.moveCommand(this); } } class AgressiveBehaviour implements IBehaviour { @Override public int moveCommand(Robot a) { System.out.println(a.getname() + "is ATTACKED!"); a.hp -= 10; return 0; } } class DefensiveBehaviour implements IBehaviour { @Override public int moveCommand(Robot a) { System.out.println(a.getname() + " RUN AWAY!"); return 0; } } class NormalBehaviour implements IBehaviour { @Override public int moveCommand(Robot a) { System.out.println(a.getname() + " Rest"); a.hp += 10; return 0; } } public class StrategyTest { static void newbehavior(Robot a) { Random r=new Random(); int num = r.nextInt(3); if (num == 0) a.behavior = new AgressiveBehaviour(); else if (num == 1) a.behavior = new DefensiveBehaviour(); else a.behavior = new NormalBehaviour(); } public static void main(String args[]) { Random r=new Random(); Robot BIGGER = new Robot("BIGGER", new AgressiveBehaviour(), r.nextInt(100) , r.nextInt(100)); Robot George = new Robot("George", new DefensiveBehaviour(), r.nextInt(100), r.nextInt(100) ); Robot R2 = new Robot("R2", new NormalBehaviour(), r.nextInt(100) , r.nextInt(100) ); BIGGER.move(); George.move(); R2.move(); Scanner scan = new Scanner(System.in); String inputs; while (true) { newbehavior(BIGGER); newbehavior(George); newbehavior(R2); BIGGER.move(); George.move(); R2.move(); System.out.println("Continue?Y/N"); inputs = scan.next(); if (inputs.equals("N")) break; } } } ``` ### UML 图 ![UML][1] ## 2. Façade 模式和 Adapter 模式 门面模式与适配器模式的区别: 在使用适配器模式时,我们会将一个接口转换成用户希望的另一个接口,从而使得原本接口不兼容而不能一起使用的类可以一起使用,而门面模式只是给一组接口或类提供了一个一致的界面,只做到了简化原有接口或类的作用,并无法让无法兼容的两个类相互兼容。 门面模式与适配器模式适用条件: 门面模式隐藏系统的复杂性,并向客户端提供了一个客户端可以访问系统的接口。这种类型的设计模式属于结构型模式,它向现有的系统添加一个接口,来隐藏系统的复杂性。 例如下面的例子(以画图为例): ```java import java.util.*; class ShapeMaker { private Shape circle; private Shape rectangle; private Shape square; public ShapeMaker() { circle = new Circle(); rectangle = new Rectangle(); square = new Square(); } public void drawCircle(){ circle.draw(); } public void drawRectangle(){ rectangle.draw(); } public void drawSquare(){ square.draw(); } } public class FacadePatternDemo { public static void main(String[] args) { ShapeMaker shapeMaker = new ShapeMaker(); shapeMaker.drawCircle(); shapeMaker.drawRectangle(); shapeMaker.drawSquare(); } } ``` 而适配器模式则是(以求解斐波那契数列为例): ```cpp #include using namespace std; typedef long long ll ll calc(ll n) { if(n==1||n==2) return 1; else if(n%2) return calc(n/2)*calc(n/2)+calc(n/2+1)*calc(n/2+1); else return calc(n/2+1)*calc(n/2+1)-calc(n/2-1)*calc(n/2-1); } ll Adapter_String_to_Integer(string a) { ll ans=0; for(int i= 0;i < a.size();i++){ ans*=10; ans+=a[i]-'0'; } return ans; } int main() { string number; cin >> number; cout << "fibonaci " << number << " is " << calc(Adapter_String_to_Integer(number)) << endl; return 0; } ``` ## 3. Observer 模式 ```java import java.util.*; class popularProduct extends Observable{ private int available; popularProduct(int number){ this.available=number; } public int getAvailable(){ return this.available; } public void setAvailable(int available) { this.available = available; } public void message(){ if(available>=1) { setChanged(); notifyObservers(); } else { System.out.println("Sorry,there is no product avaliable"); } } public void getProduct(int num,String usernames){ message(); if(available>=num) available-=num; else if(num>0) System.out.println(usernames+":Sorry,there is no enough product avaliable,please change the amount."); } } class Users implements Observer { private String username; Users(Observable obz,String name){ obz.addObserver(this); username=name; } @Override public void update(Observable obz,Object obj){ System.out.println(this.username+" :The product is available!"); } } public class ObserverTest{ public static void main(String[] args) { popularProduct product = new popularProduct(3); Users user1 = new Users(product,"doge"); Users user2 = new Users(product,"meow"); Users user3 = new Users(product,"meowmeow"); product.addObserver(user1); product.addObserver(user2); product.addObserver(user3); product.getProduct(1,"doge"); product.getProduct(1,"meow"); product.getProduct(2,"meowmeow"); } } ``` ### UML ![UML][2] ## 4. 解释 Observer 模式 意图:定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新。 主要解决:一个对象状态改变给其他对象通知的问题,而且要考虑到易用和低耦合,保证高度的协作。 何时使用:一个对象(目标对象)的状态发生改变,所有的依赖对象(观察者对象)都将得到通知,进行广播通知。 何时会增加复杂度:如果一个被观察者对象有很多的直接和间接的观察者的话,将所有的观察者都通知到会花费很多时间。 例如以下的代码: ```cpp #include using namespace std; struct product{ vector user; void adduser(string name){ user.push_back(name); } void message(string msg) { for(int i = 0;i < user.size();i++){ cout << user[i] << ":" << msg << endl; } } } int main() { product newpro; newpro.adduser("doge"); newpro.adduser("meow"); newpro.adduser("meowmeow"); newpro.message("no product available"); return 0; } ``` [1]: https://hodam.top/myfile/image/homework/3-1.png [2]: https://hodam.top/myfile/image/homework/3.bmp Loading... 第三次的作业: > emmm,感觉要写作业的时间花的越来越长了呢。+ 要做项目 + 写文档 + 弄 PPT(每天都好困呐) ## 1. Strategy 模式 以下是完成要求的代码: ```java import java.util.*; interface IBehaviour { public int moveCommand(Robot a); } class Robot { private String name; private double x, y; IBehaviour behavior; double hp; public Robot(String name, IBehaviour behavior, double x, double y) { this.name = name; this.behavior = behavior; this.x = x; this.y = y; hp = 100.0; } public double getPositionx() { return this.x; } public double getPositiony() { return this.y; } public String getname() { return this.name; } public void move() { behavior.moveCommand(this); } } class AgressiveBehaviour implements IBehaviour { @Override public int moveCommand(Robot a) { System.out.println(a.getname() + "is ATTACKED!"); a.hp -= 10; return 0; } } class DefensiveBehaviour implements IBehaviour { @Override public int moveCommand(Robot a) { System.out.println(a.getname() + " RUN AWAY!"); return 0; } } class NormalBehaviour implements IBehaviour { @Override public int moveCommand(Robot a) { System.out.println(a.getname() + " Rest"); a.hp += 10; return 0; } } public class StrategyTest { static void newbehavior(Robot a) { Random r=new Random(); int num = r.nextInt(3); if (num == 0) a.behavior = new AgressiveBehaviour(); else if (num == 1) a.behavior = new DefensiveBehaviour(); else a.behavior = new NormalBehaviour(); } public static void main(String args[]) { Random r=new Random(); Robot BIGGER = new Robot("BIGGER", new AgressiveBehaviour(), r.nextInt(100) , r.nextInt(100)); Robot George = new Robot("George", new DefensiveBehaviour(), r.nextInt(100), r.nextInt(100) ); Robot R2 = new Robot("R2", new NormalBehaviour(), r.nextInt(100) , r.nextInt(100) ); BIGGER.move(); George.move(); R2.move(); Scanner scan = new Scanner(System.in); String inputs; while (true) { newbehavior(BIGGER); newbehavior(George); newbehavior(R2); BIGGER.move(); George.move(); R2.move(); System.out.println("Continue?Y/N"); inputs = scan.next(); if (inputs.equals("N")) break; } } } ``` ### UML 图 ![UML][1] ## 2. Façade 模式和 Adapter 模式 门面模式与适配器模式的区别: 在使用适配器模式时,我们会将一个接口转换成用户希望的另一个接口,从而使得原本接口不兼容而不能一起使用的类可以一起使用,而门面模式只是给一组接口或类提供了一个一致的界面,只做到了简化原有接口或类的作用,并无法让无法兼容的两个类相互兼容。 门面模式与适配器模式适用条件: 门面模式隐藏系统的复杂性,并向客户端提供了一个客户端可以访问系统的接口。这种类型的设计模式属于结构型模式,它向现有的系统添加一个接口,来隐藏系统的复杂性。 例如下面的例子(以画图为例): ```java import java.util.*; class ShapeMaker { private Shape circle; private Shape rectangle; private Shape square; public ShapeMaker() { circle = new Circle(); rectangle = new Rectangle(); square = new Square(); } public void drawCircle(){ circle.draw(); } public void drawRectangle(){ rectangle.draw(); } public void drawSquare(){ square.draw(); } } public class FacadePatternDemo { public static void main(String[] args) { ShapeMaker shapeMaker = new ShapeMaker(); shapeMaker.drawCircle(); shapeMaker.drawRectangle(); shapeMaker.drawSquare(); } } ``` 而适配器模式则是(以求解斐波那契数列为例): ```cpp #include <bits/stdc++.h> using namespace std; typedef long long ll ll calc(ll n) { if(n==1||n==2) return 1; else if(n%2) return calc(n/2)*calc(n/2)+calc(n/2+1)*calc(n/2+1); else return calc(n/2+1)*calc(n/2+1)-calc(n/2-1)*calc(n/2-1); } ll Adapter_String_to_Integer(string a) { ll ans=0; for(int i= 0;i < a.size();i++){ ans*=10; ans+=a[i]-'0'; } return ans; } int main() { string number; cin >> number; cout << "fibonaci " << number << " is " << calc(Adapter_String_to_Integer(number)) << endl; return 0; } ``` ## 3. Observer 模式 ```java import java.util.*; class popularProduct extends Observable{ private int available; popularProduct(int number){ this.available=number; } public int getAvailable(){ return this.available; } public void setAvailable(int available) { this.available = available; } public void message(){ if(available>=1) { setChanged(); notifyObservers(); } else { System.out.println("Sorry,there is no product avaliable"); } } public void getProduct(int num,String usernames){ message(); if(available>=num) available-=num; else if(num>0) System.out.println(usernames+":Sorry,there is no enough product avaliable,please change the amount."); } } class Users implements Observer { private String username; Users(Observable obz,String name){ obz.addObserver(this); username=name; } @Override public void update(Observable obz,Object obj){ System.out.println(this.username+" :The product is available!"); } } public class ObserverTest{ public static void main(String[] args) { popularProduct product = new popularProduct(3); Users user1 = new Users(product,"doge"); Users user2 = new Users(product,"meow"); Users user3 = new Users(product,"meowmeow"); product.addObserver(user1); product.addObserver(user2); product.addObserver(user3); product.getProduct(1,"doge"); product.getProduct(1,"meow"); product.getProduct(2,"meowmeow"); } } ``` ### UML ![UML][2] ## 4. 解释 Observer 模式 意图:定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新。 主要解决:一个对象状态改变给其他对象通知的问题,而且要考虑到易用和低耦合,保证高度的协作。 何时使用:一个对象(目标对象)的状态发生改变,所有的依赖对象(观察者对象)都将得到通知,进行广播通知。 何时会增加复杂度:如果一个被观察者对象有很多的直接和间接的观察者的话,将所有的观察者都通知到会花费很多时间。 例如以下的代码: ```cpp #include <bits/stdc++.h> using namespace std; struct product{ vector<string> user; void adduser(string name){ user.push_back(name); } void message(string msg) { for(int i = 0;i < user.size();i++){ cout << user[i] << ":" << msg << endl; } } } int main() { product newpro; newpro.adduser("doge"); newpro.adduser("meow"); newpro.adduser("meowmeow"); newpro.message("no product available"); return 0; } ``` [1]: https://hodam.top/myfile/image/homework/3-1.png [2]: https://hodam.top/myfile/image/homework/3.bmp Last modification:April 26, 2019 © Allow specification reprint Support Appreciate the author Like 如果觉得我的文章对你有用,请随意赞赏