Implement property enhancement adjustment similar to meitu xiuxiu-design mode exercises and xiuxiu Design Mode

Source: Internet
Author: User

Implement property enhancement adjustment similar to meitu xiuxiu-design mode exercises and xiuxiu Design Mode
1. There are three buttons at the bottom of the interface: brightness, color, and saturation.
2. click the button and select one. The value of brightness, color, and saturation is displayed with a scroll bar on the top. The three items use the same scroll bar.
3. Click the scroll bar to adjust the brightness, color, and saturation of the selected image. (You don't need to adjust the camera, but you just need to consider that it may be implemented in the future. You need to reserve the implementation Interface)

4. consider the situations where the above requirements may change. For example, the project to be adjusted may be increased or decreased, and the actions triggered during the adjustment of each adjustment project will be modified. The code must be easily scalable. Please adopt the appropriate design mode


Ideas & designs:

First, according to the requirements of the question, press different buttons to adjust different projects, which can be achieved using the status mode. Specifically, pressing a button is equivalent to switching the status. different statuses have different behaviors, such as adjusting the color status and brightness status.

Secondly, it is required that the algorithms corresponding to each project can be adjusted. For example, color adjustment can be implemented using three algorithms, and different algorithms may be selected based on different situations. This is actually the encapsulation of the algorithm, using the Policy mode.

The structure is as follows:



The Code is as follows:

Status Code:

Package com. wbp. designmodelpractice2; import android. widget. textView; // abstract status class abstract class ButtonState {protected int progress = 50; // The Position of the slider bar protected AdjustAlgo adjustAlgo; // algorithm class // algorithm start to execute abstract public void beginAdjust (); // update status (including Algorithm Execution) abstract public void update (); public void setAdjustAlgorithm (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: status class LumState extends ButtonState {TextView TV; // other status-related components may be added later, such as ImageView @ Overridepublic void beginAdjust () {// TODO Auto-generated method stubadjustAlgo. excute (progress) ;}@ Overridepublic void update () {// TODO Auto-generated method stubtv. setText (String. valueOf (progress); beginAdjust () ;}// Color adjustment status class: class ColorState extends ButtonState {TextView TV; // other status-related components may be added later, such as ImageView @ Overridepublic void beginAdjust () {// TODO Auto-generated method stubadjustAlgo. excute (progress) ;}@ Overridepublic void update () {// TODO Auto-generated method stubtv. setText (String. valueOf (progress); beginAdjust () ;}// saturation adjustment status class: class SaturaState extends ButtonState {TextView TV; // other status-related components may be added later, such as ImageView @ Overridepublic void beginAdjust () {// 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 algorithm class abstract public class AdjustAlgo {abstract public void excute (int progress );} // abstract algorithm class abstract class ColorAdjustAlgo extends AdjustAlgo {abstract public void excute (int progress );} // abstract algorithm class abstract class LumAdjustAlgo extends AdjustAlgo {abstract public void excute (int progress );} // abstract algorithm class 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: Specific algorithm class ColorAdjustAlgTwo extends ColorAdjustAlgo {public void excute (int progress) {// specific color adjustment algorithm System. out. println ("color ---------------> algorithm 2") ;}// brightness adjustment algorithm class: Specific algorithm class LumAdjustAlgoOne extends AdjustAlgo {public void excute (int progress) {// specific brightness adjustment algorithm System. out. println ("brightness ---------------> algorithm 1") ;}// brightness adjustment algorithm class: Specific algorithm class LumAdjustAlgoTwo extends AdjustAlgo {public void excute (int progress) {// specific brightness adjustment algorithm System. out. println ("brightness ---------------> algorithm 2") ;}// saturation adjustment algorithm class: Specific algorithm class SaturaAdjustAlgoOne extends AdjustAlgo {public void excute (int progress) {// specific saturation adjustment algorithm System. out. println ("saturation ---------------> algorithm 1") ;}// saturation adjustment algorithm class: Specific algorithm class SaturaAdjustAlgoTwo extends AdjustAlgo {public void excute (int progress) {// specific saturation adjustment algorithm System. out. println ("saturation ---------------> algorithm 2 ");}}

Client 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 () {// seekbar = (SeekBar) findViewById (R. id. seekBar1); seekbar. setProgress (50); 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 all statuses and set its 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. listener (new OnSeekBarChangeListener () {@ Overridepublic void onStopTrackingTouch (SeekBar seekBar) {// TODO Auto-generated method stub} @ Overridepublic void onStartTrackingTouch (SeekBar seekBar) {// TODO Auto-generated 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 present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}@ Overridepublic void onClick (View arg0) {// TODO Auto-generated method stubstate = (ButtonState) stateList. get (Integer) arg0.getTag (); seekbar. setProgress (state. getProcess (); state. update ();}}

Note:

1. To expand a project, you only need to add a new state class. The algorithm corresponding to the extension project only needs to add a new algorithm class. The extension is convenient and fully complies with the "open and closed" principle. One thing to note here is that when you expand a project, you may not only use TextView to display the value of the project, but also display other components, such as the display color block, allows you to intuitively feel the color changes. This is also easily achieved through the extension of the state class.

2. The standard state mode adds a new state class that needs to modify the source code responsible for state conversion. Therefore, it does not support the "Open and Close" principle, the Button is cleverly used to switch the status, which solves this problem.

3. About the Design Pattern Selection: When I first came into contact with this question, I saw the changes in the two dimensions of the project and algorithm. I felt that the bridge pattern should be used. However, after thinking about it, I think it is inappropriate to use it here. There are M and N changes in the two dimensions of the bridge mode, so the total number of changes is M * N, and all these M * N changes have significance. However, this topic is not like this. For example, the result of applying the color Algorithm to the brightness state is obviously nondescribable.

4. Experience on the design mode: in fact, the design mode in section 23 is only equivalent to some examples. The most important thing is the seven design principles of object-oriented design. The principle of opening and closing is the goal, the principle of the replacement of the lining is the basis, and the principle of relying on reversal is the means. My experience with programming programs with good scalability is that the design of classes and interfaces should be simple, and then programming for interfaces, so that it is easy to comply with the "open and closed" principle.


Others:

1. Recently, we have seen that many design modes adopt the configuration file + reflection mechanism. To expand the extension, you only need to add related classes and modify the configuration file. The client does not need to be changed at all. I think it's amazing !! I tried every means to use it in this example, but I still couldn't do it. At the very least, you have to add buttons and button responses. Can you add these content without modifying the client? Solution % >_<%

2. I feel that the design is not good. I hope the passing experts will not be enlightened.



Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.