Clever Geek Handbook
📜 ⬆️ ⬇️

Private data class allocation

Highlighting a private data class is a structural design pattern used to hide attributes and their manipulations.

Private data class allocation
Described in Design PatternsNot

Content

Description

Usually classes always initially look clear and understandable. They do their job and do not meddle in the duties of other classes. However, when developing the program, new logic is added. As a result, some classes mistakenly receive a ton of additional responsibilities. A private refactoring method known as the “Extract Class” comes to the rescue. It allows you to comply with the "principle of sole responsibility" , thereby making classes more reliable and resistant to change.

Implementation Examples

To apply this design pattern to the class you need:

  1. Create a new class that will have one responsibility, using refactoring methods: field selection and method selection.
  2. Create a connection between the old and the new class.

You should make a little change and test the result after each move, this will save you from having to fix a large number of errors at the very end. At the end, after reviewing the new classes again, return to the old class, perhaps his responsibility has changed, and now it makes sense to name it differently.

C # implementation example
  namespace Private_class_data
 {
     class program
     {
         static void Main ( string [] args )
         {
             var radius = new double ();
             var color = new Color ();
             var origin = new Point ();

             var circle_before = new before .  Circle ( radius , color , origin );
             var circle_after = new after .  Circle ( radius , color , origin );
         }
     }

     class color
     {
     }

     class graphics
     {
     }

     class point
     {
     }


 }

 namespace Private_class_data.before
 {
     class circle
     {
         private double radius ;
         private color color ;
         private point origin ;
         public Circle ( double radius , Color color , Point origin )
         {
             this .  radius = radius ;
             this .  color = color ;
             this .  origin = origin ;
         }
         public double circumference
         {
             get { return 2 * Math .  PI * this .  radius ;  }
         }
         public double Diameter
         {
             get { return 2 * this .  radius ;  }
         }
         public void Draw ( Graphics graphics )
         {
             // ...
         }

     }
 }

 namespace Private_class_data.after
 {
     class CircleData
     {
         private double radius ;
         private color color ;
         private point origin ;

         public CircleData ( double radius , Color color , Point origin )
         {
             this .  radius = radius ;
             this .  color = color ;
             this .  origin = origin ;
         }

         public double Radius
         {
             get { return this .  radius ;  }
         }
         public color color
         {
             get { return this .  color ;  }
         }
         public point origig
         {
             get { return this .  origin ;  }
         }

     }

     class circle
     {
         private CircleData circleData ;
        
         public Circle ( double radius , Color color , Point origin )
         {
             circleData = new CircleData ( radius , color , origin );
         }
         public double circumference
         {
             get { return 2 * Math .  PI * this .  circleData .  Radius  }
         }
         public double Diameter
         {
             get { return 2 * this .  circleData .  Radius  }
         }
         public void Draw ( Graphics graphics )
         {
             // ...
         }

     }
 }

See also

  • Design Patterns
  • Refactoring
  • Principle of sole responsibility

Literature


Source - https://ru.wikipedia.org/w/index.php?title= Highlight private_data_class&oldid = 88786102


More articles:

  • Kortatu
  • Katrina and the Waves
  • Martinez Conchita
  • Wen Xuan Di (Northern Qi)
  • Improvement (heat treatment)
  • Chukhray, Pavel G.
  • Kdenlive
  • Ice Hockey World Championship 1994
  • Dobropolye (Chernihiv region)
  • Primitive idealization

All articles

Clever Geek | 2019