黑马程序员---java设计模式

------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------

《command 命令模式》

public interface Command  // 通过一个 command interface 让 ProcessArray 与具体的处理方式分离
{
    void process(int[] target) ;  // 定义一个 处理数组 的方法,具体的处理方式 由其实现类来定义
}

class PrintCommand implements Command {
	public void process(int[] target) {
		System.out.print("数组中的元素:");
		for (int temp : target) {
			System.out.print(temp+"   ");
		}
	}
}

class AddCommand implements Command {
	public void process(int[] target) {
		int sum = 0;
		for (int temp : target) {
			sum += temp;
		}
		System.out.print("数组中元素的和:" + sum);
	}
}

class ProcessArray {
	public void arrayWork(int[] target, Command cmd) {
		cmd.process(target);
	}
}
// 测试类
public class Demo {
	public static void main(String[] arga) {
		ProcessArray pa = new ProcessArray();
		int[] arr = { 1, 3, 5, 7 };
		pa.arrayWork(arr, new PrintCommand());  // 这里
		pa.arrayWork(arr, new AddCommand());   // 这里
	}
}

《ingleton   设 计 模 式(单态设计模式)》

class Single
{
     private static Single one = new Single();    
     /* 这句也可以写在 getSingle() 里边(前提: 去掉 private static),但是意义就变了,
         写在外边 用 static 修饰,便可以保证 该类中能产生一个对象,即使调用n次 getSingle(); 也
         只是产生一个对象(Single引用都指向同一块空间) */

     private Single(){}		// 将Single的构造函数 Single() 定义为 private,则在别的类中不可以 直接new Single(); 
				   // 因为创建对象必须调用构造函数
     public static Single getSingle()  // 定义该方法,如果别的类想要 new Single();  不能直接 new,必须调用本方法
     {
          return one;
      }
}

public class Test
{
   public static void main(String[] args)
   {
	// Single s = new Single();  //直接这样写会报错
        Single.getSingle();   // 要想创建对象,必须调用 getSingle();
    }

}

《适配器设计模式》

。该设计模式 在 GUI中常用,J2EE中并不常见

    如果一个类要 implements 一个 interface,则必须要重写该 interface 中的全部 abstract 方法,此时 如果一个 interface 中的抽象方法过多,
并且在该类中用不到这么多的 abstract 方法,则会很麻烦,所以需要一个中间的 abstract class 来过渡,即 一个 interface 先被一个 abstract
   class 实现(该 abstract class 通常被称为 适配器 Adapter) ,并在该 abstract class 中实现若干方法(方法体为空), 则 以后的需要实现该interface的 class 直接 extends 该 abstract class( Adapter )即可。 

interface Window{		// 定义Window接口,表示窗口操作
	public void open() ;	// 打开
	public void close() ;	// 关闭
	public void activated() ;	// 窗口活动
	public void iconified() ;	// 窗口最小化
	public void deiconified();// 窗口恢复大小
}
abstract class WindowAdapter implements Window{		
// 在该class 中 实现了全部的 interface Window 中的方法,虽然是 abstract class,但是
//该类中的方法都为已经重写的 非 abstract 方法
	public void open(){} ;	// 打开
	public void close(){} ;	// 关闭
	public void activated(){} ;	// 窗口活动
	public void iconified(){} ;	// 窗口最小化
	public void deiconified(){};// 窗口恢复大小
};
class WindowImpl extends WindowAdapter{
	public void open(){
		System.out.println("窗口打开。") ;
	}
	public void close(){
		System.out.println("窗口关闭。") ;
	}
};
public class AdapterDemo{
	public static void main(String args[]){
		Window win = new WindowImpl() ;
		win.open() ;
		win.close() ;
	}
};

《工厂设计模式》

原设计思路:

interface Fruit{	// 定义一个水果接口
	public void eat() ;	// 吃水果
}
class Apple implements Fruit{
	public void eat(){
		System.out.println("** 吃苹果。") ;
	}
};
class Orange implements Fruit{
	public void eat(){
		System.out.println("** 吃橘子。") ;
	}
};
public class InterfaceCaseDemo03{
	public static void main(String args[]){
		Fruit f = new Apple() ;	
<span style="white-space:pre">	</span>// 实例化接口  此时在 main() 中指定了要操作的 subclass,如果要更换 subclass,
	// <span style="font-family: Arial, Helvetica, sans-serif;">则肯定要修改 client,</span>所以此种设计 不合理
		f.eat() ;			


	}
};
修改后:
interface Fruit{	// 定义一个水果接口
	public void eat() ;	// 吃水果
}
class Apple implements Fruit{
	public void eat(){
		System.out.println("** 吃苹果。") ;
	}
};
class Orange implements Fruit{
	public void eat(){
		System.out.println("** 吃橘子。") ;
	}
};
class Factory{	// 定义工厂类
	public static Fruit getInstance(String className){
		Fruit f = null ;
		if("apple".equals(className)){
		// 判断是否要的是苹果的子类 如果此处为 className.equals("apple");  则传入 null 时会有空指针异常
			f = new Apple() ;
		}
		if("orange".equals(className)){	// 判断是否要的是橘子的子类 此处同 apple一样
			f = new Orange() ;
		}
		return f ;
	}
};
public class InterfaceCaseDemo05{
	public static void main(String args[]){
		Fruit f = Factory.getInstance(args[0]) ;   // 实例化接口,
		if(f!=null){	// 判断是否取得实例
			f.eat() ;
		}
	}
};

《代理设计模式》

interface Network{
	public void browse() ;	// 浏览
}
class Real implements Network{
	public void browse(){
		System.out.println("上网浏览信息") ;
	}
};
class Proxy implements Network{
	private Network network ;	// 代理对象
	public Proxy(Network network){
		this.network = network ;
	}
	public void check(){
		System.out.println("检查用户是否合法。") ;
	}
	public void browse(){
		this.check() ;
		this.network.browse() ;	// 调用真实的主题操作
	}
};
public class ProxyDemo{
	public static void main(String args[]){
		Network net = null ;
		net  = new Proxy(new Real()) ;//  指定代理操作
		net.browse() ;	// 客户只关心上网浏览一个操作
	}
};




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值