《Java设计模式详解》

一、抽象类与适配器模式

在接口与这个类中间可以加一个抽象类:

抽象类去覆写接口中的全部方法,而那个类去继承这个抽象类,根据需要覆写抽象类中的方法。(简单的适配器模式)

 

interface Eat{
	public void eatBread();
	public void eatApple();
	public void eatBanana();
}

abstract class PersonEat implements Eat{
	public void eatBread(){
		
	}
	public void eatApple(){}
	public void eatBanana(){}
}

class Person extends PersonEat{
	public void eatBread(){
		System.out.println("我在吃面包");
	}
	
	public void eatApple(){
		System.out.println("我在吃苹果");
	}
}


public class Demo02 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Person p=new Person();
		p.eatBread();
		p.eatApple();

	}

}

 

 

 二、单态模式和简单工厂模式

 

class Single{
	private Single(){}
	
	private static final Single s1=new Single();
	public static Single getSingleInstance(){
		return s1;
	}
	public void Say(){
		System.out.println("我开始说话了。。。。");
	}
	
}

public class SingletonDemo {
	public static void main(String[] args) {
		Single s=Single.getSingleInstance();
		s.Say();

	}

}

 

interface Car{
	public void run();
	public void stop();
}

class Benz implements Car{
	public void run(){
		System.out.println("Benz开始启动了。。。。。");
	}
	public void stop(){
		System.out.println("Benz停车了。。。。。");
	}
}

class Ford implements Car{
	public void run(){
		System.out.println("Ford开始启动了。。。");
	}
	public void stop(){
		System.out.println("Ford停车了。。。。");
	}
}

class Toyota implements Car{
	public void run(){
		System.out.println("Toyota开始启动了。。。");
	}
	public void stop(){
		System.out.println("Toyota停车了。。。。");
	}
}

class Factory{
	public static Car getCarInstance(String type){
		Car c=null;
		try {
			c=(Car)Class.forName("org.jzkangta.factorydemo03."+type).newInstance();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		return c;
	}
}
public class FactoryDemo03 {

	public static void main(String[] args) {
		Car c=Factory.getCarInstance("Toyota");
		if(c!=null){
			c.run();
			c.stop();
		}else{
			System.out.println("造不了这种汽车。。。");
		}
		

	}

}

 

 

 三、工厂方法模式

 

 

abstract interface AbstractFactory
{
}

  

class CarFactory   implements AbstractFactory
{
  public Car getCar(String type)
  {
    Car c = null;
    try {
      c = (Car)Class.forName("org.jzkangta.factorydemo02." + type).newInstance();
    }
    catch (InstantiationException e) {
      e.printStackTrace();
    }
    catch (IllegalAccessException e) {
      e.printStackTrace();
    }
    catch (ClassNotFoundException e) {
      e.printStackTrace();
    }
    return c;
  }
}

 

class BusFactory   implements AbstractFactory
{
  public Car getBus(String type)
  {
    Car c = null;
    try {
      c = (Car)Class.forName("org.jzkangta.factorydemo02." + type).newInstance();
    }
    catch (InstantiationException e) {
      e.printStackTrace();
    }
    catch (IllegalAccessException e) {
      e.printStackTrace();
    }
    catch (ClassNotFoundException e) {
      e.printStackTrace();
    }
    return c;
  }
}

 

  四、抽象工厂模式

 

//NWFactory-->女娲
interface NWFactory{
	public Person getPerson(String type);
	public Animal getAnimal(String type);
}
//阳绳-->用来造男人和雄性动物(Bull)
class YangSheng implements NWFactory{
	Man m=null;
	Bull b=null;
	public Man getPerson(String type){
		try {
			m=(Man)Class.forName("org.jzkangta.factorydemo03."+type).newInstance();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return m;
	}
	public Bull getAnimal(String type){
		try {
			b=(Bull)Class.forName("org.jzkangta.factorydemo03."+type).newInstance();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return b;
	}
}
//阴绳-->用来造女人和雌性动物(Cow)
class YinSheng implements NWFactory{
	Woman w=null;
	Cow c=null;
	public Woman getPerson(String type){
		try {
			w=(Woman)Class.forName("org.jzkangta.factorydemo03."+type).newInstance();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return w;
	}
	public Cow getAnimal(String type){
		try {
			c=(Cow)Class.forName("org.jzkangta.factorydemo03."+type).newInstance();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return c;
	}
}

 

 

   五、代理模式(proxy)

 

    静态代理

abstract class SaleComputer{
	abstract public void SaleComputer();
}
//真实的主题角色(ComputerMaker)
class ComputerMaker extends SaleComputer{
	public void SaleComputer(){
		System.out.println("卖出了一台电脑。。。");
	}
}
//代理的主题角色(ComputerProxy)
class ComputerProxy extends SaleComputer{
	ComputerMaker cm=null;
	public void youHui(){
		System.out.println("我给你优惠....");
	}
	public void giveMouse(){
		System.out.println("我还送你一个鼠标。。。");
	}
	public void SaleComputer(){
		youHui();
		giveMouse();
		if(cm==null){
			cm=new ComputerMaker();
		}
		cm.SaleComputer();
	}
}
public class ProxyDemo {

	
	public static void main(String[] args) {
		//SaleComputer sc=new ComputerMaker();
		SaleComputer sc=new ComputerProxy();
		sc.SaleComputer();

	}

}

 

    动态代理

 

public interface SaleComputer {
	public void sale(String type);
}

 

public class ComputerMaker implements SaleComputer {

	public void sale(String type) {
		System.out.println("卖出了一台"+type+"电脑");

	}

}

 

public class ComputerProxy {
	public static SaleComputer getComputerMaker(){
		ProxyFunction pf=new ProxyFunction();
		return (SaleComputer)Proxy.newProxyInstance(ComputerMaker.class.getClassLoader(), ComputerMaker.class.getInterfaces(), pf);
	}
}

 

public class ProxyFunction implements InvocationHandler {
	private ComputerMaker cm;
	
	public void youHui(){
		System.out.println("我给你一些优惠。。。");
	}
	
	public void giveMouse(){
		System.out.println("我还要送你一个鼠标。。。 ");
	}
	public Object invoke(Object arg0, Method arg1, Object[] arg2)
			throws Throwable {
		String type=(String)arg2[0];
		if(type.equals("联想")||type.equals("三星")){
			if(cm==null){
				cm=new ComputerMaker();
				youHui();
				giveMouse();
				arg1.invoke(cm, type);
			}
		}else{
			System.out.println("我没有你要的这个牌子的电脑。。。。");
		}
		return null;
	}

}

 

 六、策略模式(Strategy) 

 

       对算法的包装,把使用算法和算法本身分开,委派给不同的对象管理。

 

//抽象的策略角色
public abstract class Operation {
	public abstract void oper(float a,float b);
}

public class Add extends Operation{
	public void oper(float a,float b){
		float result=a+b;
		System.out.println("相加的结果为-->"+result);
	}
}

public class Calc {
	private Operation o;
	
	public final static Operation add=new Add();
	public final static Operation jian=new Jian();
	public final static Operation cheng=new Cheng();
	public final static Operation chu=new Chu();
	
	public void oper(float a,float b){
		o.oper(a, b);
	}
}

 

七、门面模式(Facade)  

 

1.为一个复杂的子系统提供一个简单的接口。 

2.仅仅通过门面通信,简化层次之间的依赖。

 

八、建造模式(Builder)  

 

建造模式是对象的创建模式,可以讲一个产品的内部表象与产品的生成过程分割开来,从而可以使一个建造过程生成具有不同的内部表象的产品

 

九、模板模式(Template)  

 

准备一个抽象类,将部分逻辑以具体方法的形式实现,然后申明一些抽象方法来迫使子类实现剩余的逻辑。不同的子类可以以不同的方式实现这些抽象方法,从而对剩余的逻辑有不同的实现。

 

public abstract class Template {
	protected String pcType;
	public Template(String pcType){
		this.pcType=pcType;
	}
	
	//留给子类去实现的基本方法1
	abstract protected void makeCPU(String pcType);
	//留给子类去实现的基本方法2
	abstract protected void makeMainBorad(String pcType);
	//留给子类去实现的基本方法3
	abstract protected void makeHD(String pcType);
	
	//基本方法,已经实现好了
	public final void makeOver(String pcType){
		System.out.println(pcType+"造好了");
	}
	
	//模板方法,造电脑
	public final void makePC(){
		makeCPU(pcType);
		makeMainBorad(pcType);
		makeHD(pcType);
		makeOver(pcType);
	}
}

 

public class PC extends Template{

	public PC(String pcType) {
		super(pcType);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void makeCPU(String pcType) {
		// TODO Auto-generated method stub
		System.out.println(pcType+"的CPU造好了");
		
	}

	@Override
	protected void makeHD(String pcType) {
		// TODO Auto-generated method stub
		System.out.println(pcType+"的硬盘造好了");
	}

	@Override
	protected void makeMainBorad(String pcType) {
		// TODO Auto-generated method stub
		System.out.println(pcType+"的主板造好了");
	}

}

  

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值