java第十四天

工厂设计模式

耦合度代码的耦合度越低越好

工厂设计模式好处就解耦(降低耦合度)

代理类设计模式:保护我的核心代码

将能够确定的字符串写在equals之前

/**
	工厂设计模式:
		耦合度:代码的耦合度越低越好
		工厂设计模式的好处就是解耦
		

*/
class  JavaDemo38
{
	public static void main(String[] args) 
	{
		//System.out.println("Hello World!");
		Fruit f = new Orange();
		Fruit f2 = new Apple();
	
		Factory fac = new Factory();
		System.out.println(fac.creatFruit("橘子"));



	}
}

//等学完反射才能完整?????
class Factory
{
	public Fruit creatFruit(String temp){
		if("苹果".equals(temp)){
			return new Apple();
		}
		if("橘子".equals(temp)){
			return new Orange();
		}
		if("香蕉".equals(temp)){
			return new Banana();
		}

		return  null;
	}
}

interface Fruit
{
}

class Orange implements Fruit
{
}

class Apple implements Fruit
{
}

class Banana implements Fruit
{
}
/**
	代理设计模式:保护我的核心代码
		

*/
class JavaDemo39 
{
	public static void main(String[] args) 
	{
		new ProxyNet().connectNet();
	}
}

interface Net
{
	 void connectNet();
}

class RealNet implements Net
{	//上网的核心
	public void connectNet(){

		System.out.println("真的实现上网!");
	}

}

class ProxyNet implements Net
{
	public boolean validate(String username,String password){
		//将能够确定的字符串写在equals之前
		if("haoren".equals(username)&&"123".equals(password)){
			return true;
		}
		
		return false;
		
	}
	public boolean validateMoney(double money){
		if(money>0){
			return true;
		}
		return false;
	}

	public void connectNet(){
			
			
		//验证用户名和密码
			if(this.validate("haoren","123")){
				//检查费用
				if(validateMoney(-12)){
					//实现上网
						new RealNet().connectNet();
				}else{
					System.out.println("您的余额不足,请续费!");
				}
			}else{
				System.out.println("用户名或密码有错!");

			}
			
		
		
	}
}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值