目录
前言
开闭原则定义
开闭原则做用
开闭原则案例
1. 描述
2. uml 图
3. 具体代码
前言
在软件开发中,为了提升软件系统的可维护性和可复用性,增长软件的可扩展性和灵活性,程序员要尽可能根据 7 条原则来开发程序,从而提升软件开发效率、节约软件开发成本和维护成本。咱们将在下面的几节中依次来介绍这 7 条原则,本节首先介绍开闭原则。
开闭原则定义
开闭原则规定“软件中的对象(类,模块,函数等等)应该对于扩展是开放的,可是对于修改是封闭的”,这意味着一个实体是容许在不改变它的源代码的前提下变动它的行为。该特性在产品化的环境中是特别有价值的,在这种环境中,改变源代码须要代码审查,单元测试以及诸如此类的用以确保产品使用质量的过程。遵循这种原则的代码在扩展时并不发生改变,所以无需上述的过程。
咱们一般在程序设计中利用接口、抽象类、继承和实现等方式来体现开闭原则。
开闭原则做用
开闭原则是面向对象程序设计的终极目标,它使软件实体拥有必定的适应性和灵活性的同时具有稳定性和延续性。具体来讲,其做用以下
对软件测试的影响
软件遵照开闭原则的话,软件测试时只须要对扩展的代码进行测试就能够了,由于原有的测试代码仍然可以正常运行。
能够提升代码的可复用性
粒度越小,被复用的可能性就越大;在面向对象的程序设计中,根据原子和抽象编程能够提升代码的可复用性。
能够提升软件的可扩展性和可维护性
遵照开闭原则的软件,其稳定性高和延续性强,从而易于扩展和维护。
开闭原则案例
1. 描述
咱们设计图书接口、电子书接口、以及SSM电子书类和美食书籍类。电子书接口经过继承图书接口,对其进行了扩展,增长了getSize()方法,即获取电子书内存大大小。SSM电子书类经过实现电子书接口进一步扩展,增长了SSMComtent方法。美食书籍类经过实现图书接口,一样实现了扩展的方法。
之后若是咱们有了更多的武侠类、游戏类……等书籍,不须要对原来的图书接口进行修改,只须要继承完成扩展便可。
2. uml 图
3. 具体代码
图书接口
import java.util.Date;
/**
* 图书接口
* 价格、书名、出版日期
*/
public interface IBook {
double getPrince();
String getName();
Date publishDate();
}
电子书接口,扩展了一个方法获取电子书所占大小的方法。
/**
* 电子书接口
*/
public interface IComputerBook extends IBook{
/**
* 获取电子书大小 16kb
* */
String getSize();
}
实现图书接口实现扩展。
import java.util.Date;
public class FoodBook implements IBook{
private String name;
private double price;
private Date publishDate;
public FoodBook(String name, double price, Date publishDate) {
this.name = name;
this.price = price;
this.publishDate = publishDate;
}
@Override
public double getPrince() {
return price;
}
@Override
public String getName() {
return name;
}
@Override
public Date publishDate() {
return publishDate;
}
public void foodContent() {
System.out.println("美食书籍: " + name + "-内容丰富哦!仅须要" + price +"元");
}
}
实现电子书接口实现扩展。
import java.util.Date;
public class SSMBook implements IComputerBook {
private String name;
private double price;
private Date publishDate;
private String size;
private String author;
public SSMBook(String name, double price, Date publishDate, String size, String author) {
this.name = name;
this.price = price;
this.publishDate = publishDate;
this.size = size;
this.author = author;
}
@Override
public String getSize() {
return size;
}
@Override
public double getPrince() {
return price;
}
@Override
public String getName() {
return name;
}
@Override
public Date publishDate() {
return publishDate;
}
public void SSMComtent() {
System.out.println("SSM架构实战书籍:" + name + "-做者:" + author);
}
}
测试类
import java.util.Date;
public class Main {
public static void main(String[] args) {
FoodBook foodBook = new FoodBook("厨房秘籍", 30.00, new Date());
showBookConten(foodBook);
SSMBook ssmBook = new SSMBook("ssm架构", 57.20, new Date(), "20MB", "anben");
showBookConten(ssmBook);
}
private static void showBookConten(IBook iBook) {
if (iBook instanceof IComputerBook) {
SSMBook ssmBook = (SSMBook) iBook;
ssmBook.SSMComtent();
} else {
FoodBook foodBook = (FoodBook) iBook;
foodBook.foodContent();
}
}
}
测试结果
美食书籍: 厨房秘籍-内容丰富哦!仅须要30.0元
SSM架构实战书籍:ssm架构-做者:anben
MainActivity:
复制代码
1 //定义底部文字
2 private final int[] TAB_TITLES = new int[]{
3 R.string.menu_msg, R.string.menu_history, R.string.menu_mine
4 };
5 //定义底部图标
6 private final int[] TAB_IMGS = new int[]{
7 R.drawable.tab_main_msg, R.drawable.tab_main_history, R.drawable.tab_main_mine
8 };
9 //黄油刀 找到控件
10 @BindView(R.id.view_pager)
11 ViewPager viewPager;
12 @BindView(R.id.tab_layout)
13 TabLayout tabLayout;
14
15 //定义适配器
16 private PagerAdapter pagerAdapter;
17
18
19
20
21 //初始化页卡
22 private void initPager() {
23 pagerAdapter = new MainFragmentAdapter(getSupportFragmentManager());
24 viewPager.setAdapter(pagerAdapter);
25 viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
26 tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
27 @Override
28 public void onTabSelected(TabLayout.Tab tab) {
29 viewPager.setCurrentItem(tab.getPosition(), false);
30 }
31
32 @Override
33 public void onTabUnselected(TabLayout.Tab tab) {
34
35 }
36
37 @Override
38 public void onTabReselected(TabLayout.Tab tab) {
39
40 }
41 });
42 }
43
44
45 //设置页卡显示效果
46 private void setTabs(TabLayout tabLayout, LayoutInflater inflater, int[] tabTitles, int[] tabImgs) {
47 for (int i = 0; i < tabImgs.length; i++) {
48 TabLayout.Tab tab = tabLayout.newTab();
49 View view = inflater.inflate(R.layout.item_main_menu, null);
50 //使用自定义视图,便于修改
51 tab.setCustomView(view);
52 TextView tvTitle = (TextView) view.findViewById(R.id.txt_tab);
53 tvTitle.setText(tabTitles[i]);
54 ImageView imgTab = (ImageView) view.findViewById(R.id.img_tab);
55 imgTab.setImageResource(tabImgs[i]);
56 tabLayout.addTab(tab);
57 }
58 }
复制代码
MainActivity.xml:
复制代码
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2 xmlns:app="http://schemas.android.com/apk/res-auto"
3 xmlns:tools="http://schemas.android.com/tools"
4 android:layout_width="match_parent"
5 android:layout_height="match_parent"
6 android:orientation="vertical"
7 tools:context=".MainActivity">
8 <RelativeLayout
9 android:layout_width="match_parent"
10 android:layout_height="wrap_content"
11 >
12 <com.example.myapplication.mytoobar.CustomToolbar
13 android:id="@+id/bar1"
14 android:layout_width=www.xcdeyiju.com"match_parent"
15 android:layout_height="wrap_content"/>
16 <View
17 android:layout_width="match_parent"
18 android:layout_height="0.5dp"
19 android:background="@color/line_gray"
20 android:layout_alignBottom="@+id/bar1"></View>
21 </RelativeLayout>
22 <android.support.v4.view.ViewPager
23 android:id="@+id/view_pager"
24 android:layout_width="match_parent"
25 android:layout_height="0dip"
26 android:layout_weight="1" />
27
28 <View
29 android:layout_width="match_parent"
30 android:layout_height=www.yuchenghd.com"0.5dip"
31 android:background="@color/line_gray" />
32
33 <android.support.design.widget.TabLayout
34 android:id="@+id/tab_layout"
35 android:layout_width="match_parent"
36 android:layout_height="100dp"
37 app:tabIndicatorHeight="0dip" />
38
39 </LinearLayout>
复制代码
Adapter:
复制代码
1 @Override
2 public Fragment getItem(int i) {
3 Fragment fragment = null;
4 switch (www.yuchengyuLedL.com) {
5 case 0:
6 fragment = new MsgFragment();
7 break;
8 case 1:
9 fragment = new HistoryFragment();
10 break;
11 case 2:
12 fragment = new MineFragment();
13 break;
14 default:
15 break;
16 }
17 return fragment;
18
19 }
20
21 @Override
22 public int getCount() {
23 return 3;
24 }
复制代码
四:自定义标题栏的实现
关键代码:
toolbar.xml:
复制代码
1 <ImageView
2 android:layout_width="www.xingtuyLgw.com wrap_www.yifayuled.cn content"
3 android:layout_height="wrap_content"
4
5
6 android:src="@mipmap/ic_launcher"
7 android:layout_gravity="left"
8 />
9 <TextView
10 android:id="@+id/tv_title"
11 android:layout_width="wrap_content"
12 android:layout_height="wrap_content"
13 android:text=www.whonyLpt.com"SubwayGo"
14 android:textColor="#000000"
15 android:layout_gravity="center"
16 android:singleLine="true"
17 android:visibility="visible"
18 android:textSize="30dp"
19 android:fontFamily="cursive"
20 />
21 <TextView
22 android:layout_width="wrap_content"
23 android:layout_height="wrap_content"
24 android:textSize="20dp"
25 android:textColor="@color/colorPrimaryDark"
26 android:text="设置"
27 android:gravity="center"
28 android:layout_gravity="right"
29 android:visibility=www.xingtuyuLept.com"visible"
30 />
复制代码
复制代码
1 public class CustomToolbar extends Toolbar {
2
3 public CustomToolbar(Context context)www.chaoyuepint.com{
4 this(context,null);
5 }
6 public CustomToolbar(Context www.baiyidLu.com context, AttributeSet attrs) {
7 this(context, attrs,0);
8 }
9 public CustomToolbar(Context context,www.baitengpt.com AttributeSet attrs,int defStyleAttr){
10 super(context,attrs,defStyleAttr);
11 inflate(context, R.layout.toolbar,this);
12 }
13 }
复制代码
五:总结一下,以上代码都是关键代码,至于点击事件的设置,页面数据的传递等等比较基础,就不附上了。下一篇会对线路的最优路径进行选择。java