设计模式之开闭原则
开闭原则说的通俗一点就是对扩展开放,对修改关闭;这样原则设计出来的系统,遇到增加功能的需求时,几乎不用修改源代码,只是增加几个类,然后调用就好。这样既增加了新功能满足了需求,又维护了原本系统的稳定性。下面我写一个demo,具体分为5步,作用是比较两个事物的大小,比如可以比较2个人的大小、2个房子的大小、2头猪的大小等,看了demo就会明白。
一、类图
二、需要比较的对象,暂时只有房子、人
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 (isAccpet(arg1, arg2)) {
object1 = arg1;
object2 = arg2;
} else {
throw new RuntimeException(arg1 + "and" + arg2 + " isn't the them type ");
}
}
protected abstract boolean isAccpet(Object arg1, Object arg2);
public abstract Object getMax();
public abstract Object getMin();
}
四、比较类,主要实现具体的比较方法,人呢是比较年龄,房子是比较面积。
public class CompareHouse extends CompareObject {
@Override
protected boolean isAccpet(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 ComparePerson extends CompareObject {
@Override
protected boolean isAccpet(Object arg1, Object arg2) {
if ((arg1 instanceof Person && arg2 instanceof Person)) {
return true;
}
return false;
}
@Override
public Object getMax() {
Person person = (Person) object1;
Person person1 = (Person) object2;
if (person.getAge() > person1.getAge()) {
return person;
}
return person1;
}
@Override
public Object getMin() {
if (((Person) object1).getAge() > ((Person) object2).getAge()) {
return (Person) object2;
}
return (Person) object1;
}
}
五、调用
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Person person = new Person("petter", 20);
Person person1 = new Person("mery", 21);
ComparePerson comparePerson = new ComparePerson();
comparePerson.setData(person, person1);
Log.d(TAG,"Max_Person"+ comparePerson.getMax().toString());
// house
House house = new House(8, 12, "beijing");
House house1 = new House(10, 11, "shenzhen");
CompareHouse compareHouse = new CompareHouse();
compareHouse.setData(house, house1);
Log.d(TAG,"Max_House"+ compareHouse.getMax().toString());
}
}
运行结果:
2019-03-14 18:09:26.008 15027-15027/com.example.openingdemo D/MainActivity: Max_PersonPerson{name='mery', age=21}
2019-03-14 18:09:26.009 15027-15027/com.example.openingdemo D/MainActivity: Max_HouseHouse [width=10, length=11, addr=shenzhen]
这样做的好处就是,如果下次要添加比较2头猪的需求时,我们无需改动原本的代码,只要添加一个pig类、一个ComparePig类就好了。