Java-常用设计模式

1.模板方法设计模式
定义一个操作中算法的骨架,而将一些可变的部分 延伸到子类中,以不改变一个算法的结构,即可重新定义该算法的某些步骤
abstract  class Animal{
	public String name;
	public Animal(String name){
		this.name = name;
	}
	// 该抽象方法 用于在子类中实现
	public abstract void hobby();
}

class Dog extends Animal{
	public Dog(String name){
		super(name);
	}
	public void hobby(){
		System.out.println("我叫:" + this.name + "我的兴趣是running...");
	}
}
class Pig extends Animal{
	public Pig(String name){
		super(name);
	}
	public void hobby(){
		System.out.println("我叫:" + this.name + "我的兴趣是eatting...");
	}
}

class T{
	public static void main(String [] args){
		Animal a = new Dog("哈士奇");
		a.hobby();
		Animal b = new Pig("佩奇");
		b.hobby();
	}
}
2.策略模式
定义了一系列算法,将每一种算法封装起来并且可以相互替换使用,策略模式让算法独立于使用它的客户应用而独立变化
public class T{
	public static void main(String [] args){
		Duck d = new Duck("白天鹅",new FlyImpl());
		d.fly();
		Duck e = new Duck("丑小鸭",new NotFlyImpl());
		e.fly();
	}
}
interface FlyAble{
	public void fly();
}
class FlyImpl implements FlyAble{
	public void fly(){
		System.out.println("I can fly...");
	}
}
class NotFlyImpl implements FlyAble{
	public void fly(){
		System.out.println("I can not fly...");
	}
}

class Duck{
	public String name;
	public FlyAble fly;
	public Duck(String name,FlyAble fly){
		this.name = name;
		this.fly = fly;
	}
	public void fly(){
		System.out.println("My name is" + this.name);
		fly.fly();
	}
}
3.简单工厂模式
是由一个工厂对象决定创建出哪种对象
public class T{
	public static void main(String [] args){
		CookWork food = Factory.getFood("noodles");
		food.cooking();
		CooWork food1 = Factory.getFood("rice");
		food1.cooking()
	}
}
interface CookWork{
	public void cooking();
}
class Noodles implements CookWork{
	public void cooking{
		System.out.println("面条出锅啦...");
	}
}
class Rice implements CookWork{
	public void cooking{
		System.out.println("米饭熟啦...");
	}
}
// 工厂函数
class Factory{
	public static CookWork getFood(String foodName){
		if("noodles".equals(foodName)){
			return new Noodles();
		}else{
			return new Rice();
		}
	}
}
4.代理模式
为其他对象提供一种代理以控制这个对象的访问
public class T{
	public static void main(String [] args){
		//李四代理张三去购物
		LiSi lisi = new LiSi(new ZhangSan());
		lisi.shopping();
	}
}
//代理业务接口
interface Subject{
	public void shopping();
}
// 被代理人 张三
class ZhangSan implements Subject{
	public void shopping(){
		System.out.println("I am shopping...");
	}
}
// 代理人
class LiSi implements Subject{
	private ZhangSan target;
	public LiSi(ZhangSan name){
		this.target = name;
	}
	public void shopping(){
		System.out.println("购物前准备工作...");
		target.shopping(); // 核心业务
		System.out.println("购物后的工作...");
	}
}
5.适配器模式
将一个类的接口转换成客户端希望的另外一个接口
class T{
	public static void main(String [] args){
		PowerA a = new PowerAImpl();
		input(a);
		// b 适配 a
		PowerB b = new PowerBImpl();
		PowerAdapter adapter = new PowerAdapter(b);
		input(adapter);
	}
	public static void input(PowerA a){
		a.connect();
	}
}
interface PowerA{
	public void connect();
}
class PowerAImpl implements PowerA{
	public void connect(){
		System.out.println("接口A正常工作...");
	}
}

interface PowerB{
	public void insert();
}
class PowerImpl implements PowerB{
	public void insert(){
		System.out.println("接口B正常工作...");
	}
}
// b 接口 适配 a 接口
class PowerAdapter implements PowerA{
	private PowerB b;
	public PowerAdapter(PowerB b){
		this.b = b;
	}
	public void connect(){
		b.insert()
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值