博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
设计模式
阅读量:2346 次
发布时间:2019-05-10

本文共 7583 字,大约阅读时间需要 25 分钟。

工厂模式和策略模式

public class TestDemo {
//策略模式实现 public static void testDemo(){
// CatchSuper catchSuper = CatchFactory.createCatchAccept(CatchPlicy.CatchRebat);// double accptMoney = catchSuper.acceptCatch(1000); double accptMoney = new CatchContext(CatchPlicy.CatchRebat).GetResult(1000); System.out.print(accptMoney); }}abstract class CatchSuper {
public abstract double acceptCatch(double money);}//不打折class NormarCatc extends CatchSuper {
@Override public double acceptCatch(double money) {
return money; }}//打折class CatchRebat extends CatchSuper{
private double rebat = 1d; public CatchRebat(double rebat){
this.rebat = rebat; } @Override public double acceptCatch(double money) {
return money*this.rebat; }}//满减class CatchReturn extends CatchSuper{
private double moneyCondition = 0.0d; private double moneyReturn = 0.0d; public CatchReturn(double moneyCo,double moneyre){
this.moneyCondition = moneyCo; this.moneyReturn = moneyre; } @Override public double acceptCatch(double money) {
double result = money; if (money >= this.moneyCondition){
result = money - Math.floor(money/moneyCondition)*moneyReturn; } return result; }}//活动策略enum CatchPlicy{
NormarCatc,CatchRebat,CatchReturn}// 工厂实现//class CatchFactory{
// public static CatchSuper createCatchAccept(CatchPlicy policy){
// CatchSuper cs = null;// switch (policy){
// case NormarCatc:// cs = new NormarCatc();// break;// case CatchReturn:// cs = new CatchReturn(300,100);// break;// case CatchRebat:// cs = new CatchRebat(0.85);// break;// }// return cs;// }////}//策略管理类实现class CatchContext{
CatchSuper cs = null; public CatchContext(CatchPlicy policy){
switch (policy){
case NormarCatc: cs = new NormarCatc(); break; case CatchReturn: cs = new CatchReturn(300,100); break; case CatchRebat: cs = new CatchRebat(0.85); break; } } public double GetResult(double money){
return cs.acceptCatch(money); }}

代理模式

/** 实现通过代理实现送礼物给校花,本人不用直接出面* */public class TestDelegateMode {
public static void testDelegate(){
BeautyGirl girl = new BeautyGirl("校花"); Proxy proxy = new Proxy(girl); proxy.GiveDolls(); proxy.GiveFlowers(); }}//接口interface IGiveGift {
void GiveDolls(); void GiveFlowers();}//被代理方class BeautyGirl{
public String name; public BeautyGirl(String name){
this.name = name; } public String getName() {
return name; }}//代理方class Pursuit implements IGiveGift {
String name; BeautyGirl beauty; public Pursuit(BeautyGirl beauty){
this.beauty = beauty; this.name = "john"; } @Override public void GiveDolls() {
System.out.print(this.name+" GiveDolls to "+beauty.name); } @Override public void GiveFlowers() {
System.out.print(this.name+" GiveFlowers to "+beauty.name); }}//代理class Proxy implements IGiveGift{
Pursuit pursuit; public Proxy(BeautyGirl beautyGirl){
this.pursuit = new Pursuit(beautyGirl); } @Override public void GiveDolls() {
pursuit.GiveDolls(); } @Override public void GiveFlowers() {
pursuit.GiveFlowers(); }}

观察者模式也叫发布订阅模式

第一次画uml图,mac使用umlStar

在这里插入图片描述

import java.lang.reflect.Array;import java.util.ArrayList;import java.util.Iterator;public class ObserModeDemo {
public static void testObserverDemo(){
Boss boss = new Boss(); StockObserver worker1 = new StockObserver("bob",boss); StockObserver worker2 = new StockObserver("john",boss); StockObserver worker3 = new StockObserver("tom",boss); StockObserver worker4 = new StockObserver("lili",boss); boss.Attach(worker1); boss.Attach(worker2); boss.Attach(worker3); boss.Attach(worker4); boss.setSubjectState("老板回来了,注意点"); boss.Notify(); }}//被观察者要继承的抽象类abstract class Observer{
String name; Subject subject; public Observer(String name,Subject sub){
this.name = name; this.subject = sub; } public abstract void Update();}//接口interface Subject{
void Attach(Observer observer); void Detech(Observer observer); void Notify(); String getSubjectState(); void setSubjectState(String state);}//观察者class Boss implements Subject{
private String state; private ArrayList
observers = new ArrayList
(); @Override public void Attach(Observer observer) {
Iterator it = observers.iterator(); while (it.hasNext()){
Observer ob = (Observer) it.next(); if (observer.equals(ob) ){
//判断之前是否已经添加过 return; } } observers.add(observer); } @Override public void Detech(Observer observer) {
Boolean isContain = false; Iterator it = observers.iterator(); while (it.hasNext()){
Observer ob = (Observer) it.next(); if (observer.equals(ob) ){
isContain = true; } } //判断是否存在 if (isContain==true){
observers.remove(observer); } } @Override public void Notify() {
Iterator it = observers.iterator(); while (it.hasNext()){
//消息分发,当然也可以用链表,让观察者可以截断后续的接收者,类似广播 //这里可以进行消息分发顺序处理 Observer ob = (Observer) it.next(); ob.Update(); } } @Override public String getSubjectState() {
return this.state; } @Override public void setSubjectState(String state) {
this.state = state; //当然也可以设置一旦state状态发生改变就分发// this.Notify(); }}//被观察者class StockObserver extends Observer{
public StockObserver(String name,Subject sub) {
super(name,sub); } @Override public void Update() {
System.out.print(subject.getSubjectState()+"通知"+name+"注意 \n"); }}

观察者代理模式使用多代理回调实现

import java.util.HashMap;public class TestDelegateForNew {
public static void testDelegateNew(){
Work1 work1 = new Work1(); Work2 work2 = new Work2(); callbackUtils.addCallback("work1",work1); callbackUtils.addCallback("work2",work2); callbackUtils.delegateCallback("work1"); callbackUtils.publish(); }}//多代理回调interface CallBack{
public void dowork();}//回调管理类class callbackUtils{
private static HashMap
callbacks= new HashMap<>(); public static void addCallback(String s,CallBack bac){
callbacks.put(s,bac); } public static void delegateCallback(String s){
callbacks.remove(s); } public static void publish(){
if (callbacks.size()==0){
return; } for (CallBack callback:callbacks.values()){
callback.dowork(); } }}class Work1 extends Object implements CallBack{
@Override public void dowork() {
System.out.print("设计程序 \n"); }}class Work2 extends Object implements CallBack{
@Override public void dowork() {
System.out.print("敲代码 \n"); }}

转载地址:http://denvb.baihongyu.com/

你可能感兴趣的文章
删除vector中重复元素
查看>>
和为s的连续正数序列
查看>>
什么是Redis?什么是nosql?NoSQL数据库的四大分类
查看>>
为什么说Redis是单线程的以及Redis为什么这么快!
查看>>
redis的过期健删除策略以及内存淘汰机制
查看>>
map 如何使用结构体作为自定义键值
查看>>
Mysql几种索引类型的区别及适用情况
查看>>
判断一个数组,是否可以分成两个数组之和相等的数组
查看>>
背包问题
查看>>
结构体变量之间的比较和赋值原理
查看>>
产生死锁的必要条件及处理方法
查看>>
TCP和UDP的区别
查看>>
事务具有四个特性
查看>>
Hadoop Hdfs 配置
查看>>
tsung集群测试
查看>>
oracle定时删除表空间的数据并释放表空间
查看>>
解决文件提示: /bin/ksh^M: bad interpreter: bad interpreter:No such file or directory
查看>>
ajaxanywhere jsp 使用
查看>>
如何静态化JSP页面
查看>>
XML 与 Java 技术: 用 Castor 进行数据绑定
查看>>