标签:
简介
项目开发中发现问题、解决问题这个过程中会出现很多问题,比如重复出现、某个问题的遗留,这些问题的本质就是设计模式。
解决问题的复杂性
面向对象
责任。面向对象设计原则
依赖倒置原则(DIP)
高层模块(稳定)不应该赖低层模块(变化),二者都应该依赖于抽象(稳定)。
抽象(稳定)不应该依赖实现节(变化);实现节应该依赖抽象(稳定)。
开放封闭原则(OCP)
对扩展开放,对更改封闭。
类模块应该是可扩展的,但是不可修改。
单一职责原则(SRP)
一个类应该仅有一个引起它变化的原因。
变化的方向隐含着类的责任。
Liskov替换原则(LSP)
子类必须能够替换他们的基类(IS-A)。
继承表达类型抽象。
借口隔离原则(ISP)
不应该强迫客户程序依赖他们不用的方法。
借口应该小而完美。
优先使用对象组合,而不是类继承
类继承通常为白箱复用,对象组合通常为黑箱复用。
继承在某种程度上破坏了封装性,子类父类合度高。
对象组合只要求被组合的对象具有良好定义的借口,耦合度低。
封装变化点
使用封装来创建对象之间的分界层,让设计者可以在分界层的一侧进行修改,而不会对另一侧产生不良的影响,从而实现层次间的松耦合。
针对接口编程,而不是针对实现编程
不将变量类型声明为某个特定的具体类,而是声明为某个接口。
客户程序无需获知对象的具体类型,只需要知道对象所具有的接口。
减少系统中各部分的依赖关系,从而实现高类聚、松耦合的类型设计方案。
将设计原则提升为设计经验
类与相互通信的对象之间的组织关系,包括他们的角色、职责、写作方式等方面。本节设计目标:
设计一个简单的画图程序,用户可选择类型直线/矩形,实现效果如下
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"android:paddingBottom="@dimen/activity_vertical_margin"tools:context=".MainActivity"><TextView android:text="@string/hello_world"android:layout_width="wrap_content"android:layout_height="wrap_content" /></RelativeLayout>
//直线类class Line {public int startX;public int startY;public int endX;public int endY;public Line(int startX, int startY, int endX, int endY) {this.startX = startX;this.startY = startY;this.endX = endX;this.endY = endY;}}//矩形类class Rect {public int left;public int top;public int right;public int bottom;public Rect(int left, int top, int right, int bottom) {this.left = left;this.top = top;this.right = right;this.bottom = bottom;}}//添加...class Circle{}
public class MainActivity extends AppCompatActivity {private RadioGroup radioGroup;private RadioButton lineRadio;private RadioButton rectRadio;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);layoutInit();}//初始化用户界面void layoutInit() {MyView myView = new MyView(this);setContentView(myView);LinearLayout layout = new LinearLayout(this);layout.setOrientation(LinearLayout.HORIZONTAL);LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);radioInit();layout.addView(radioGroup);addContentView(layout, params);}void radioInit() {radioGroup = new RadioGroup(this);lineRadio = new RadioButton(this);lineRadio.setText("直线");radioGroup.addView(lineRadio);rectRadio = new RadioButton(this);rectRadio.setText("矩形");radioGroup.addView(rectRadio);lineRadio.setChecked(true);}//自定义视图public class MyView extends View {private Paint paint;//声明画笔private Point startPoint;//起点坐标private Point endPoint;public MyView(Context context) {super(context);paint = new Paint();paint.setStyle(Paint.Style.STROKE);//空心paint.setColor(Color.RED);paint.setStrokeJoin(Paint.Join.ROUND);paint.setStrokeCap(Paint.Cap.ROUND);paint.setStrokeWidth(5);startPoint = new Point();endPoint = new Point();}//保存界面上的线和矩形Vector<Line> lineVector = new Vector<Line>();Vector<Rect> rectVector = new Vector<Rect>();//更改...Vector<Circle> circleVector=new Vector<Circle>();@Overridepublic boolean onTouchEvent(MotionEvent event) {if (event.getAction() == MotionEvent.ACTION_DOWN) {startPoint.set((int) event.getX(), (int) event.getY());}if (event.getAction() == MotionEvent.ACTION_UP) {endPoint.set((int) event.getX(), (int) event.getY());if (lineRadio.isChecked()) {lineVector.add(new Line(startPoint.x,startPoint.y,endPoint.x,endPoint.y));} else if (rectRadio.isChecked()) {rectVector.add(new Rect(startPoint.x,startPoint.y,endPoint.x,endPoint.y));}//更改.../*else if(...){}*/}postInvalidate();return true;}@Overrideprotected void onDraw(Canvas canvas) {for (int i = 0; i < lineVector.size(); i++) {canvas.drawLine(lineVector.get(i).startX,lineVector.get(i).startY,lineVector.get(i).endX,lineVector.get(i).endY,paint);}for (int i = 0; i < rectVector.size(); i++) {canvas.drawRect(rectVector.get(i).left,rectVector.get(i).top,rectVector.get(i).right,rectVector.get(i).bottom,paint);}//更改...for(int i=0;i<circleVector.size();i++){}}}}
//形状基类abstract class Shape {abstract void draw(Canvas canvas, Paint paint);}//直线类class Line extends Shape {public int startX;public int startY;public int endX;public int endY;public Line(int startX, int startY, int endX, int endY) {this.startX = startX;this.startY = startY;this.endX = endX;this.endY = endY;}@Overridevoid draw(Canvas canvas, Paint paint) {canvas.drawLine(startX, startY, endX, endY, paint);}}//矩形类class Rect extends Shape {public int left;public int top;public int right;public int bottom;public Rect(int left, int top, int right, int bottom) {this.left = left;this.top = top;this.right = right;this.bottom = bottom;}@Overridevoid draw(Canvas canvas, Paint paint) {canvas.drawRect(left, top, right, bottom, paint);}}//添加class Circle extends Shape{@Overridevoid draw(Canvas canvas, Paint paint) {//...}}
public class MainActivity extends AppCompatActivity {//声明成员变量private RadioGroup radioGroup;private RadioButton lineRadio;private RadioButton rectRadio;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);layoutInit();}//初始化用户界面void layoutInit() {MyView myView = new MyView(this);setContentView(myView);LinearLayout layout = new LinearLayout(this);layout.setOrientation(LinearLayout.HORIZONTAL);LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);radioInit();layout.addView(radioGroup);addContentView(layout, params);}//动态生成RadioGroupvoid radioInit() {radioGroup = new RadioGroup(this);lineRadio = new RadioButton(this);lineRadio.setText("直线");radioGroup.addView(lineRadio);rectRadio = new RadioButton(this);rectRadio.setText("矩形");radioGroup.addView(rectRadio);lineRadio.setChecked(true);}public class MyView extends View {public Paint paint;private Point startPoint;private Point endPoint;public MyView(Context context) {super(context);paint = new Paint();paint.setStyle(Paint.Style.STROKE);paint.setColor(Color.RED);paint.setStrokeJoin(Paint.Join.ROUND);paint.setStrokeCap(Paint.Cap.ROUND);paint.setStrokeWidth(5);startPoint = new Point();endPoint = new Point();}Vector<Shape> shapeVector = new Vector<Shape>();@Overridepublic boolean onTouchEvent(MotionEvent event) {if (event.getAction() == MotionEvent.ACTION_DOWN) {startPoint.set((int) event.getX(), (int) event.getY());}if (event.getAction() == MotionEvent.ACTION_UP) {endPoint.set((int) event.getX(), (int) event.getY());if (lineRadio.isChecked()) {shapeVector.add(new Line(startPoint.x,startPoint.y,endPoint.x,endPoint.y));} else if (rectRadio.isChecked()) {shapeVector.add(new Rect(startPoint.x,startPoint.y,endPoint.x,endPoint.y));}//更改.../* else if(...){}*/}postInvalidate();return true;}@Overrideprotected void onDraw(Canvas canvas) {for (int i = 0; i < shapeVector.size(); i++) {shapeVector.get(i).draw(canvas, paint);}}}}
标签:
原文地址:http://www.cnblogs.com/wisemen/p/5827295.html