The beauty of the design pattern: Private Class Data (Private Class Data)

Source: Internet
Author: User

The intent encapsulates the initialization data of the class, controls the changes to the class attributes, and isolates the class data from the methods using the data. Encapsulate class data initialization, control write access to class attributes and separate data from methods that use it. Structure participant MainClass constructs an instance of the DataClass class Based on the constructor parameter list. DataClass encapsulates data. Applicability when the following conditions are met, you can use the Private Class Data mode: Class initialization Data is disposable Data that cannot be modified. You need to control the changes to the class's initialization data. Prevent unnecessary changes to the initialization data. Reduce the attributes exposed by the class. Remove the write permission for data from the class. Implementation Method (1): encapsulate the initialization data. Copy code 1 namespace PrivateClassDataPattern. implementation1 2 {3 public class CircleData 4 {5 public CircleData (double radius, Color color, Point origin) 6 {7 this. radius = radius; 8 this. color = color; 9 this. origin = origin; 10} 11 12 public double Radius {get; private set;} 13 public Color {get; private set;} 14 public Point Origin {get; private set ;} 15} 16 17 public class Circle18 {19 private CircleData _ circleData; 20 21 public Circle (double radius, Color color, Point origin) 22 {23 _ circleData = new CircleData (radius, color, origin); 24} 25 26 public double Circumference27 {28 get {return 2 * Math. PI * _ circleData. radius;} 29} 30 31 public double Diameter32 {33 get {return 2 * _ circleData. radius;} 34} 35 36 public void Draw (Graphics graphics) 37 {38} 39} 40}

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.