实验 21:观察者模式
package observer_pattern;
public interface Guancha {
public void sheng();
public void jiang();
}
package observer_pattern;
public class Gumin implements Guancha{
private String name;
public Gumin(String a){name=a;}
public void sheng(){
System.out.println(name+"买了一些股票(股票升了)");
}
public void jiang(){
System.out.println("股票降价了,哭了");
}
}
package observer_pattern;
import java.util.ArrayList;
public class Fen{
private ArrayList<Guancha> oblist;
public Fen(){
oblist=new ArrayList<>();
}
public void add(Guancha a){
oblist.add(a);
}
public void remove(Guancha a){
oblist.remove(a);
}
public void sheng(double a){
if(a>0.05)
for(Guancha guan:oblist){
guan.sheng();
}
}
public void jiang(double a){
if(a>0.05)
for(Guancha guan:oblist){
guan.jiang();
}
}
}
package observer_pattern;
public class main {
public static void main(String[] args) {
Fen f=new Fen();
Gumin g1=new Gumin("第一个股民");
Gumin g2=new Gumin("第二个股民");
f.add(g1);
f.add(g2);
System.out.println("股票升了");
f.sheng(0.1);
System.out.println("比率小于0.05");
System.out.println();
f.sheng(0.001);
System.out.println("股票降了");
f.jiang(0.1);
System.out.println("比率小于0.05");
System.out.println();
f.jiang(0.001);
}
}
实验 22:状态模式
package rjsj.no22;
public class GreenState extends AccountState{
public GreenState(double balance,Account acc) {
this.balance = balance;
this.account = acc;
this.stateName = "正常状态";
}
}
package rjsj.no22;
public class RedState extends AccountState{
public RedState(AccountState state) {
this.balance = state.balance;
this.account = state.account;
this.stateName="透支状态";
}
public void withdraw(double amount) {
System.out.println(account.getOwner()+"取款"+amount);
System.out.println("抱歉,您的账户已处于透支状态,不能取款!");
}
}
package rjsj.no22;
public class YellowState extends AccountState{
public YellowState(AccountState state) {
this.balance = state.balance;
this.account = state.account;
this.stateName="欠费状态";
}
}
package rjsj.no22;
public class Account {
private AccountState state;
private String owner;
public AccountState getState() {
return state;
}
public void setState(AccountState state) {
this.state = state;
}
public String getOwner() {
return owner;
}
public void deposit(double amount) {
state.deposit(amount);
}
public void withdraw(double amount) {
state.withdraw(amount);
}
public Account(String owner, double initBalance) {
this.owner = owner;
this.state = new GreenState(initBalance,this);
System.out.println(this.owner + "开户成功!银行卡初始金额:" + initBalance);
System.out.println("----------------------------------------------------");
}
}
package rjsj.no22;
public abstract class AccountState {
protected Account account;//账户名
protected double balance;//账户金额
protected String stateName;//当前状态
public void deposit(double amount) {
System.out.println(account.getOwner() + "存款" + amount);
this.balance = this.balance + amount;
stateCheck();
System.out.println("账户余额:" + this.balance);
System.out.println("账户状态:" + account.getState().stateName);
}
public void withdraw(double amount) {
System.out.println(account.getOwner() + "取款" + amount);
this.balance = this.balance - amount;
stateCheck();
System.out.println("账户余额:" + this.balance);
System.out.println("账户状态:" + account.getState().stateName);
}
public void stateCheck(){//状态审查
if( balance >= -1000 && balance < 0 ) {
account.setState(new YellowState(this));
}else if( balance < -1000 ) {
account.setState(new RedState(this));
}else if ( balance > 0 ){
account.setState(new GreenState(this.balance,this.account));
}
}
}
package rjsj.no22;
public class Client {
public static void main(String[] args) {
Account account = new Account("李华",100);
account.deposit(8888);
System.out.println("------------------------------");
account.withdraw(666);
System.out.println("------------------------------");
account.deposit(50);
System.out.println("------------------------------");
account.withdraw(9000);
System.out.println("------------------------------");
account.withdraw(50000);
System.out.println("------------------------------");
account.deposit(3000);
System.out.println("------------------------------");
account.withdraw(100);
}
}
实验 23:策略模式
package rjsj.no23;
public class BicycleStrategy implements TravelStrategy{
@Override
public void travel() {
System.out.println("骑自行车旅游");
}
}
package rjsj.no23;
public class Client {
public static void main(String[] args) {
Traveler traveler = new Traveler();
System.out.println("旅游开始!");
TravelStrategy travelStrategy = new TrainStrategy();
traveler.setTravelStrategy(travelStrategy);
traveler.travel();
System.out.println("**************************************************");
System.out.println("旅游开始!");
travelStrategy = new AirplaneStrategy();
traveler.setTravelStrategy(travelStrategy);
traveler.travel();
System.out.println("**************************************************");
System.out.println("旅游开始!");
travelStrategy = new BicycleStrategy();
traveler.setTravelStrategy(travelStrategy);
traveler.travel();
System.out.println("**************************************************");
}
}
package rjsj.no23;
public class TrainStrategy implements TravelStrategy{
@Override
public void travel() {
System.out.println("坐火车旅游");
}
}
package rjsj.no23;
public class Traveler {
private TravelStrategy travelStrategy;
public void setTravelStrategy(TravelStrategy travelStrategy) {
this.travelStrategy = travelStrategy;
}
public void travel(){
travelStrategy.travel();
}
}
package rjsj.no23;
public interface TravelStrategy {
void travel();
}
package rjsj.no23;
public class AirplaneStrategy implements TravelStrategy{
@Override
public void travel() {
System.out.println("乘坐飞机旅游");
}
}
标签:account,每日,System,博客,void,println,public,out From: https://www.cnblogs.com/LiuLyuancn/p/17863093.html