Java23种设计模式(一)

本文深入探讨了多种设计模式,包括Factory模式、抽象工厂模式、单例模式、Builder模式、Prototype模式等,通过代码实例详细解释了每种模式的实现方式及其应用场景。

(i)创建模式

Factory模式

工厂方法

在这里插入图片描述

package com.test;


public class Factory {
	
	/**
	 * Method main
	 *
	 *
	 * @param args
	 *
	 */
	public static void main(String[] args) {
		// TODO: Add your code here
		Sample sampleA=new SampleA();
		Sample sampleB=new SampleB();
		sampleA.say();sampleA.hello();
		sampleB.say();sampleB.hello();
		
		//通过工厂实例化Sample,不涉及具体子类
		System.out.println("通过工厂实例化Sample,不涉及具体子类");
		Sample sampleA1=Factory.creare(1);
		Sample sampleB1=Factory.creare(2);
		sampleA1.say();sampleA1.hello();
		sampleB1.say();sampleB1.hello();
	}	
		
		
	public static Sample creare(int which){
		switch(which){
		case 1:return new SampleA();
		case 2:return new SampleB();
		default:return new SampleA();
		}
	}
}

interface Sample{
	public void say();
	abstract void hello();
}

class SampleA implements Sample{
	public void say(){
		System.out.println("this is SampleA...");
	}
	
	public void hello(){
		System.out.println("Hello...");
	}
}

class SampleB implements Sample{
	public void say(){
		System.out.println("this is SampleB...");
	}
	
	public void hello(){
		System.out.println("Hello...");
	}
}

this is SampleA...
Hello...
this is SampleB...
Hello...
通过工厂实例化Sample,不涉及具体子类
this is SampleA...
Hello...
this is SampleB...
Hello...

抽象工厂

package com.test;

interface Sample1{
	public void say();
	abstract void hello();
}

interface Sample2{
	public void say();
	abstract void hello();
}


class SampleA1 implements Sample1{
	public void say(){
		System.out.println("this is SampleA1...");
	}
	
	public void hello(){
		System.out.println("Hello...");
	}
}

class SampleB1 implements Sample1{
	public void say(){
		System.out.println("this is SampleB1...");
	}
	
	public void hello(){
		System.out.println("Hello...");
	}
}

class SampleA2 implements Sample2{
	public void say(){
		System.out.println("this is SampleA2...");
	}
	
	public void hello(){
		System.out.println("Hello...");
	}
}

class SampleB2 implements Sample2{
	public void say(){
		System.out.println("this is SampleB2...");
	}
	
	public void hello(){
		System.out.println("Hello...");
	}
}

public abstract class Factory {
	
	/**
	 * Method main
	 *
	 *
	 * @param args
	 *
	 */
	public static void main(String[] args) {
		// TODO: Add your code here
		//cannot instance type of abstract class
		SimpleFactory simpleFactory=new SimpleFactory();
		BombFactory bombFactory=new BombFactory();
		simpleFactory.create().say();
		simpleFactory.create().hello();
		simpleFactory.create("sampleA2").say();
		simpleFactory.create("sampleA2").hello();
		bombFactory.create().say();
		bombFactory.create().hello();
		bombFactory.create("sampleB2").say();
		bombFactory.create("sampleB2").hello();
	}	
	
	public abstract Sample1 create();
	public abstract Sample2 create(String name);
		
	
}

class SimpleFactory extends Factory{
	public Sample1 create(){
		return new SampleA1();
	}
	public Sample2 create(String name){
		return new SampleA2();
	}
}

class BombFactory extends Factory{
	public Sample1 create(){
		return new SampleB1();
	}
	public Sample2 create(String name){
		return new SampleB2();
	}
}

this is SampleA1…
Hello…
this is SampleA2…
Hello…
this is SampleB1…
Hello…
this is SampleB2…
Hello…

单例模式

package com.test;

public class Singleton {
	private Singleton(){}
	private static Singleton instance=new Singleton();
	public static Singleton getInstance(){
		return instance;
	}
	
	void say(int count){
		count++;
		System.out.println("this is Singleton mode"+count);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int count=0;
		Singleton singleton1=Singleton.getInstance();
		singleton1.say(count);
		Singleton singleton2=Singleton.getInstance();
		singleton2.say(count);
		System.out.println(singleton1);
		System.out.println(singleton2);
		System.out.println("count="+count);
		Singleton1.getInstance().say();
	}

}

class Singleton1{
	private static Singleton1 instance=null;
	public static synchronized Singleton1 getInstance(){
		//这个方法较上面有所改进,不用每次都生成对象
		if(instance==null){
			instance=new Singleton1();
		}
		return instance;
	}
	void say(){
		System.out.println("this is Singleton1 mode");
	}
}

this is Singleton mode1
this is Singleton mode1
com.test.Singleton@6b97fd
com.test.Singleton@6b97fd
count=0
this is Singleton1 mode

Builder模式

将一个复杂对象的构建与它的表示分离,是的同样的构建过程可以创建不同的表示

package com.test;

public class Test{
	public static void main(String[] args){
		ConcreteBuilder concreteBuilder=new ConcreteBuilder();
		Director director=new Director(concreteBuilder);
		director.construct();
		Product product=concreteBuilder.getProduct();
	}
}

interface Builder{
	void buildPartA();
	void buildPartB();
	void buildPartC();
	Product getProduct();
}

interface Product{
	
}

interface Part{
	
}

class Director{
	private Builder builder;
	public Director(Builder builder){
		this.builder=builder;
	}
	public void construct(){
		builder.buildPartA();
		builder.buildPartB();
		builder.buildPartC();
	}
}

class ConcreteBuilder implements Builder{

	public void buildPartA() {
		// TODO Auto-generated method stub
		System.out.println("buildPartA...");
	}

	public void buildPartB() {
		// TODO Auto-generated method stub
		System.out.println("buildPartB...");
	}

	public void buildPartC() {
		// TODO Auto-generated method stub
		System.out.println("buildPartC...");
	}

	public Product getProduct() {
		// TODO Auto-generated method stub
		return null;
	}
	
}

buildPartA…
buildPartB…
buildPartC…

prototype模式

用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。

package com.test;

public class Test{
	public static void main(String[] args){
		AbstractSpoon spoon=new SoupSpoon();
		AbstractSpoon spoon2=(SoupSpoon)spoon.clone();
		System.out.println("spoon "+spoon);
		System.out.println("spoon2 "+spoon2);
		spoon.drinkSoup();
		spoon2.drinkSoup();
	}
}

abstract class AbstractSpoon implements Cloneable{
	String spoonName;

	public String getSpoonName() {
		return spoonName;
	}

	public void setSpoonName(String spoonName) {
		this.spoonName = spoonName;
	}
	
	public Object clone(){
		Object obj=null;
		try {
			obj=super.clone();
		} catch (CloneNotSupportedException e) {
			// TODO: handle exception
			System.err.println("AbstractSpoon is not Cloneable");
		}
		return obj;
	}
	public void drinkSoup(){
		System.out.println("drink soup...");
	}
}

class SoupSpoon extends AbstractSpoon{
	public SoupSpoon(){
		setSpoonName("Soup Spoon");
	}
}

spoon com.test.SoupSpoon@f6a746
spoon2 com.test.SoupSpoon@15ff48b
drink soup…
drink soup…

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值