欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java开闭原则实例

程序员文章站 2022-03-15 15:36:44
...

题记:

以后想多写几篇blog,不为别的,为了加深自己学习印象吧。也许有人会问,你都做java这么久了还在学这些啊,没错,正是因为我做久了java,才发现自己的基础知识有些欠缺,概念懂一些,但未必深刻,所以还要再学一下下。。。


java开闭原则,即对扩展开放,对修改关闭。这样原则设计出来的系统,遇到增加功能的需求时,几乎不用修改源代码,只是增加几个类,然后调用就好。这样既增加了新功能满足了需求,又维护了原本系统的稳定性。

下面我会写一个demo,具体分为5步,作用是比较两个事物的大小,比如可以比较2个人的大小、2个房子的大小、2头猪的大小等,看了demo瞬间明白。


一、类图

java开闭原则实例



二、需要比较的对象,暂时只有房子、人

//房子
public class House {

	int width;
	int length;
	String addr;
	
	public House (int width, int length, String addr){
		this.width = width;
		this.length = length;
		this.addr = addr;
	}
	
	public int getWidth() {
		return width;
	}
	public void setWidth(int width) {
		this.width = width;
	}
	public int getLength() {
		return length;
	}
	public void setLength(int length) {
		this.length = length;
	}
	public String getAddr() {
		return addr;
	}
	public void setAddr(String addr) {
		this.addr = addr;
	}
	
	public int getSquare() {
		return width * length;
	}
	
	
	@Override
	public String toString() {
		return "House [width=" + width + ", length=" + length + ", addr="
				+ addr + "]";
	}
	
	
}

//人
public class Person {

	private String name;
	private int age;
	
	public Person(String name, int age) {
		this.name = name;
		this.age = age;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}

	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + "]";
	}
	
	
	
}


三、抽象类,把随着比较对象不同而改变的方法抽象出来

public abstract class CompareObject {

	protected Object object1;
	protected Object object2;
	
	public void setData(Object arg1, Object arg2){
		
		if (isAccept(arg1,arg2)) {
			object1 = arg1;
			object2 = arg2;
		}else {
			throw new RuntimeException( arg1 + " and " + arg2 + " isn't the them type ");
		}
		
	}
	
	
	protected abstract boolean isAccept(Object arg1, Object arg2);

	
	public abstract Object getMax();
	
	
	public abstract Object getMin();
	
	
}


四、比较类,主要实现具体的比较方法,人呢是比较年龄,房子是比较面积。

/**
 * @author lei
 *
 */
public class ComparePerson extends CompareObject{

	@Override
	public Object getMax() {
		Person person1 = (Person) object1;
		Person person2 = (Person) object2;
		if (person1.getAge() > person2.getAge()) {
			return person1;
		}
		return person2;
	}

	@Override
	public Object getMin() {
		if (((Person) object1).getAge() > ((Person) object2).getAge()) {
			return (Person) object2;
		}
		return (Person) object1;
	}

	@Override
	protected boolean isAccept(Object arg1, Object arg2) {
		if ((arg1 instanceof Person) && (arg2 instanceof Person)) {
			return true;
		}
		return false;
	}

	
	
}


public class CompareHouse extends CompareObject {

	@Override
	protected boolean isAccept(Object arg1, Object arg2) {
		if ((arg1 instanceof House) && (arg2 instanceof House)) {
			return true;
		}
		return false;
	}

	@Override
	public Object getMax() {
		if (((House) object1).getSquare() > ((House) object2).getSquare()) {
			return (House) object1;
		}
		return (House) object2;
	}

	@Override
	public Object getMin() {
		if (((House) object1).getSquare() > ((House) object2).getSquare()) {
			return (House) object2;
		}
		return (House) object1;
	}
	
	public Object getCheaper() {
		
		return null;
	}

}

五、最后的调用

public class MainClass {

	
	public static void main(String[] args) {
		
		Person person1 = new Person("petter", 20);
		Person person2 = new Person("mery", 21);
		ComparePerson comparePerson = new ComparePerson();
		comparePerson.setData(person1, person2);
		System.out.println("max_person: " + comparePerson.getMax().toString());
		
		//house
		House house1 = new House(8,12," ShangHai ");
		House house2 = new House(10,11," ShenZheng ");
		CompareHouse compareHouse = new CompareHouse();
		compareHouse.setData(house1, house2);
		System.out.println("max_House: " + compareHouse.getMax().toString());
	}

}

打印结果:

max_person: Person [name=mery, age=21]
max_House: House [width=10, length=11, addr= ShenZheng ]

好了,demo已经结束,差不多已经明白了吧。这样做的好处就是,如果下次要添加比较2头猪的需求时,我们无需改动原本的代码,只要添加一个pig类、一个ComparePig类就好了。