Development Mode Study Notes -- OCP open-closed Principle

Source: Internet
Author: User

We need to draw the congratulation and square application on the standard GUI. The circle and Square must be drawn in a specific order.

In the following example, the structured scheme and the evolution process of implementing the OCP principle are used respectively. Let's take a look at the code.

 

1. Structured Solutions

 Class _ procedural solution <br/>{< br/> // a new shape is required, so all the places where this enumeration is used must be re-compiled and deployed! <Br/> Enum shaptype <br/> {<br/> circle, square <br/>}</P> <p> class shape <br/> {<br/> Public shaptype itstype = 0; <br/>}</P> <p> // -------------------------- <br/> class circle: shape <br/>{< br/> Public double itsradius = 0; <br/> Public point itscenter = new point (); <br/>}</P> <p> void drawcircle (circle) <br/>{}</P> <p> // ----------------------------- <br/> class square: shape <br/> {<Br/> Public double itsside = 0; <br/> Public point itstopleft = new point (); <br/>}</P> <p> void drawsquare (square) <br/>{}</P> <p> // ---------------------------- <br/> // if a new type needs to be added, this type needs to be modified instead of extended! <Br/> void drawallshapes (list <shape> shapelist) <br/>{< br/> foreach (shape in shapelist) <br/>{< br/> switch (shape. itstype) <br/>{< br/> case shaptype. circle: <br/> drawcircle (circle) shape); <br/> break; <br/> case shaptype. square: <br/> drawsquare (square) shape); <br/> break; <br/> default: <br/> break; <br/>}</P> <p>/* all other types that need to be used, use a similar switch statement <br/> * and add a new shape, all of which need to be modified <br/> **/</P> <p>}

 

2. OCP Solution

Class _ OCP solution <br/>{< br/> Public interface ishape <br/>{< br/> void draw (); <br/>}</P> <p> public class square: ishape <br/>{< br/> # region ishape member </P> <p> Public void draw () <br/>{< br/> throw new exception ("the method or operation is not implemented. "); <br/>}</P> <p> # endregion <br/>}</P> <p> public class circle: ishape <br/>{< br/> # region ishape member </P> <p> Public void draw () <br/>{< br/> throw new exception ("the method or operation is not implemented. "); <br/>}</P> <p> # endregion <br/>}</P> <p> Public void drawallshapes (list <ishape> shapes) <br/>{< br/> foreach (ishape shape in shapes) <br/>{< br/> shape. draw (); <br/>}</P> <p> // ---------------- <br/> // If a triangle is added, this class is added, and the rest is not changed <br/> public class triangle: ishape <br/>{< br/> # region ishape member </P> <p> Public void draw () <br/>{< br/> throw new exception ("the method or operation is not implemented. "); <br/>}</P> <p> # endregion <br/>}< br/>}

 

3. Solutions to draw all circles first

Class _ a solution that draws all circles <br/>{< br/> // This is an incorrect solution, added the icomparabe interface <br/> Public interface ishape: icomparable <br/> {<br/> void draw (); <br/>}</P> <p> public class circle: ishape <br/>{< br/> Public void draw () <br/>{< br/> throw new exception ("the method or operation is not implemented. "); <br/>}</P> <p> Public int compareto (Object OBJ) <br/>{< br/> If (obj is square) <br/> return-1; <Br/> else <br/> return 0; <br/>}</P> <p> public class square: ishape <br/> {<br/> Public void draw () <br/> {<br/> throw new exception ("the method or operation is not implemented. "); <br/>}</P> <p> Public int compareto (Object OBJ) <br/>{< br/> throw new exception ("the method or operation is not implemented. "); <br/>}</P> <p> // rewrite the drawallshapes method. <br/> Public void drawallshapes (List <ishape> shapes) <br/>{< br/> shapes. sort (); // sort and draw again <br/> foreach (ishape shape in shapes) <br/>{< br/> shape. draw (); <br/>}</P> <p> // -------------- <br/> // this solution does not solve the problems encountered by the process solution. <br/> // adds a new shape, all the compareto methods of all classes must be modified. <br/> // the solution is to extract the changes !!!! (See the next class) <br/>}

 

4. Use the so-called "data-driven" method to obtain the closeness

// Use the so-called "data-driven" method to obtain the closeness <br/> class _ solutions that need to draw all circles first _ correct solutions <br/>{< br/> Public class shapecompare: icomparer <br/>{< br/> Private Static hashtable priorities = new hashtable (); </P> <p> static shapecompare () <br/> {<br/> priorities. add (typeof (circle), 1); <br/> priorities. add (typeof (square), 2); <br/> // priorities. add (typeof (triangle), 1); // This behavior is added later, other code remains unchanged <br/>}</P> <p> private int priorityfor (T Ype type) <br/>{< br/> If (priorities. contains (type) <br/> return (INT) priorities [type]; <br/> else <br/> return 0; <br/>}</P> <p> int icomparer. compare (Object X, object y) <br/>{< br/> int priority1 = priorityfor (X. getType (); <br/> int priority2 = priorityfor (Y. getType (); <br/> return priority1.compareto (priority2 ); <br/>}</P> <p> // rewrite drawallshapes <br/> Public void drawallshapes (ARR Aylist shapes) <br/>{< br/> shapes. sort (New shapecompare (); // sort and draw again <br/> foreach (ishape shape in shapes) <br/>{< br/> shape. draw (); <br/>}</P> <p>/*. When a triangle is added, you only need to add a new triangle class. <br/> * No other code is required! <Br/> **/</P> <p> Public interface ishape <br/> {<br/> void draw (); <br/>}</P> <p> public class square: ishape <br/>{< br/> Public void draw () <br/>{< br/> throw new exception ("the method or operation is not implemented. "); <br/>}</P> <p> public class circle: ishape <br/>{< br/> Public void draw () <br/>{< br/> throw new exception ("the method or operation is not implemented. "); <br/>}< br/>}

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.