1. Below the interface there are 3 buttons, namely brightness, color, saturation
2. Click the button, select a, above with a scroll bar display brightness, color, saturation value, three with the same scroll bar.
3. When selected, slide the scroll bar to adjust the brightness, color, and saturation of the current selection. (no real adjustment of the camera, false implementation can be, but need to consider the future may be implemented, to reserve the implementation of the interface)
4. Consider that the above requirements may change, for example, may increase the reduction of the items to be adjusted, each adjustment item triggered by the action will be modified, the code can be easily extended. Please use suitable design mode to solve
Ideas & Design:
First, according to the requirements of the topic, press the different buttons, adjust the different items, this can be achieved using the state mode. Specifically, the button presses the equivalent of switching state, in different states have different behavior, such as color state adjust color, brightness state adjust brightness.
Secondly, it is required that the corresponding algorithm of each project can be adjusted, for example, color adjustment can have 3 kinds of algorithms to achieve, and may choose different algorithms according to different situations. This is actually the encapsulation of the algorithm, the use of strategy mode.
The structure diagram is as follows:
The code is as follows:
Status-related code:
Package Com.wbp.designmodelpractice2;import android.widget.textview;//Abstract class ButtonState {protected int progress = 50;//Slide bar position protected Adjustalgo Adjustalgo; The algorithm class//algorithm begins execution of the abstract public void beginadjust ();//Update state (contains algorithm execution) abstract public void update ();p ublic void Setadjustalgor Ithm (Adjustalgo adjustalgo) {This.adjustalgo = Adjustalgo;} public void setprocess (int progress) {this.progress = progress;} Public Adjustalgo Getadjustalgo () {return adjustalgo;} public int getprocess () {return progress;}} Brightness Adjustment Status class: Specific status classes Lumstate extends ButtonState {TextView tv;//later may also add other state related components such as imageview@overridepublic void Begina Djust () {//TODO auto-generated Method Stubadjustalgo.excute (progress);} @Overridepublic void Update () {//TODO auto-generated method Stubtv.settext (string.valueof (progress)); Beginadjust ();}} Color Adjustment Status class: Specific status classes Colorstate extends ButtonState {TextView tv;//later may also add other state related components such as imageview@overridepublic void Begi Nadjust () {//TODO auto-generated method StubadjustalGo.excute (progress);} @Overridepublic void Update () {//TODO auto-generated method Stubtv.settext (string.valueof (progress)); Beginadjust ();}} Saturation adjustment State Class: Specific state classes Saturastate extends ButtonState {TextView tv;//later may also add other state related components such as imageview@overridepublic void be Ginadjust () {//TODO auto-generated Method Stubadjustalgo.excute (progress);} @Overridepublic void Update () {//TODO auto-generated method Stubtv.settext (string.valueof (progress)); Beginadjust ();}}
Algorithm-related code:
Package com.wbp.designmodelpractice2;//Abstract Method class Adjustalgo {abstract public void Excute (int progress) ;} Abstract class Coloradjustalgo extends Adjustalgo{abstract public void Excute (int progress);} Abstract class Lumadjustalgo extends Adjustalgo{abstract public void Excute (int progress);} Abstract class Saturaadjustalgo extends Adjustalgo{abstract public void Excute (int progress);} Color Adjustment algorithm class: Specific algorithm class Coloradjustalgoone extends Coloradjustalgo {public void Excute (int progress) {//Specific color adjustment algorithm System.out . println ("Color---------------> Algorithm 1");}} Color Adjustment Algorithm Class: Concrete algorithm class Coloradjustalgtwo extends Coloradjustalgo {public void Excute (int progress) {// Specific color adjustment algorithm System.out.println ("Color---------------> Algorithm 2");}} Brightness Adjustment Algorithm Class: Concrete algorithm class Lumadjustalgoone extends Adjustalgo {public void Excute (int progress) {// Specific brightness adjustment algorithm System.out.println ("Brightness---------------> Algorithm 1");}} Brightness Adjustment algorithm classes: Specific algorithm class Lumadjustalgotwo extends Adjustalgo {public void Excute (int progress) {//Specific brightness adjustment algorithm SystEm.out.println ("Brightness---------------> Algorithm 2");}} Saturation tuning algorithm class: Concrete algorithm class Saturaadjustalgoone extends Adjustalgo {public void Excute (int progress) {// Specific saturation adjustment algorithm System.out.println ("Saturation---------------> Algorithm 1");}} Saturation tuning algorithm class: Concrete algorithm class Saturaadjustalgotwo extends Adjustalgo {public void Excute (int progress) {// Specific saturation adjustment algorithm System.out.println ("Saturation---------------> Algorithm 2");}}
Client-related code:
Package Com.wbp.designmodelpractice2;import Java.util.arraylist;import Java.util.list;import android.os.Bundle; Import Android.app.activity;import Android.view.menu;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.seekbar;import Android.widget.seekbar.onseekbarchangelistener;import Android.widget.textview;public class MainActivity extends Activity implements Onclicklistener {SeekBar SeekBar; Button Btnlum; Button Btncolor; Button Btnsatura; TextView Tvlum; TextView Tvcolor; TextView Tvsatura; list<buttonstate> statelist = new arraylist<buttonstate> (); ButtonState state; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate) ; Setcontentview (R.layout.activity_main); init ();} private void Init () {//interface initialization SeekBar = (SeekBar) Findviewbyid (R.ID.SEEKBAR1); seekbar.setprogress (+); btnlum = (Button) Findviewbyid (R.id.btnlum); Btnlum.settag (0); Btnlum.setonclicklistener (this); Btncolor = (BUtton) Findviewbyid (R.id.btncolor); Btncolor.settag (1); Btncolor.setonclicklistener (this); Btnsatura = (Button) Findviewbyid (R.id.btnsatura); Btnsatura.settag (2) Btnsatura.setonclicklistener (this);//Create a new state and set its corresponding algorithm lumstate Lum = new Lumstate (); lum.tv = (TextView) Findviewbyid (R.id.tvlum); Lum.setadjustalgorithm (new Lumadjustalgoone ()); Statelist.add (Lum); Colorstate color = new Colorstate (), color.tv = (TextView) Findviewbyid (R.id.tvcolor); Color.setadjustalgorithm (new Coloradjustalgoone ()); Statelist.add (color); Saturastate Satura = new Saturastate () satura.tv = (TextView) Findviewbyid (R.id.tvsatura); Satura.setadjustalgorithm ( New Saturaadjustalgotwo ()); Statelist.add (satura); state = (buttonstate) statelist.get (0); Seekbar.setonseekbarchangelistener (New Onseekbarchangelistener () {@Overridepublic void Onstoptrackingtouch (Seekbar SeekBar) {//Todo auto-generated method stub} @Overridepublic void Onstarttrackingtouch (SeekBar seekBar) {//Todo Auto-gen erated method stub} @Overridepublic void OnProgressChanged (SeekBar SeekBar, int progress,boolean fromuser) {//TODO auto-generated method stub//application Algorithm state.setprocess ( progress); State.update ();});} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;} @Overridepublic void OnClick (View arg0) {//TODO auto-generated method stubstate = (buttonstate) statelist.get ((Integer) a Rg0.gettag ()); Seekbar.setprogress (State.getprocess ()); State.update ();}}
Description
1, expand the project only need to add new state class, extension project corresponding algorithm only need to add new algorithm class, extension is very convenient, fully conform to the "open and close" principle. One thing to note here is that when you extend a project, you don't have to show the value of the item just by TextView, but it's possible to show other components, such as a color block, that can make people feel the color change intuitively. This is also easy to do by extending the State class.
2, the standard state mode to add the new state class needs to modify the source code responsible for the state transformation, so the "open and close" principle of support and not good, here cleverly adopted a button click to switch state, solve the problem.
3, about the choice of design mode: Just contact this topic when you see the project and the algorithm two dimensions change, feel should use bridging mode. But after thinking about it, I think it is inappropriate to use it here. Bridge mode two dimensions respectively have m, N change, then the total change number is m*n, and this m*n kind of change all have existence significance. But this is not the case, for example, the color algorithm used in the brightness state, the result is obviously not nondescript.
4, about the design mode of experience: In fact, 23 design mode is just equivalent to some examples, the most important is the object-oriented design of the 7 design principles. The open and closed principle is the goal, the Richter substitution principle is the basis, the dependence reversal principle is the means. My experience with how to write scalable programs is that classes and interfaces are designed to function simply, and then programmed to interface with programming, so it's easy to conform to the "open and close" principle.
Other:
1, recently saw a lot of design patterns using configuration file + reflection mechanism, expansion only need to increase the relevant class, modify the configuration file, the client does not need to change at all. Feel this is too cow!! Try to use it in this case, but you can't do it in the end. At least the button and the response of the button to add, the content can not be added to do not modify the client? Solve it,%>_<%.
2, the feeling of design is not good, I hope that the passing of the GAO's liberal enlighten
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Implement the property enhancement adjustment of similar beauty 美图秀秀--Design pattern practice