How to use the Abstract class? Abstract class power

Summary:

  Today I want to talk about how to use abstract classes, abstract classes and real power. This will combine the specific business to illustrate how to use an abstract class, because I am in contact with business not long, if the bar fine, please look under the old title.

Business Brief:

  I am currently only contact with PMS (Property Management System), the company's main business is US rental business. Because the United States and China renters renting may be a bit different in the background, not described herein. Let us straight into the subject.

  When the user clicks on the rent, we have to create an order, and when the order is created, you need to do a lot of things, only a simplified version of this article, to avoid off-topic. When you create an order, you need to do the following things:

    1.Available check availability
    2.MinDay check minimum of Stay matches
    3.Check in / Check out Check-in time and check the departure time match

    4.quote calculate the price.

  And for the first three, the only judge of the room Can rent. And the future will likely join other rules, if they write together is also possible, but not conducive to expansion. Here I look at how to use abstract classes to implement his Scalability:

 

  1. First, create the following class

    1.1 create an abstract class OrderAddDTO.cs    

    public  class OrderAddDTO 
    { 
        ///  <Summary> 
        /// Property Id
         ///  </ Summary> 
        public the Guid PropertyId { GET ; SET ;}
         ///  <Summary> 
        /// Check
         ///  </ Summary> 
        public {CheckInDate the DateTime GET ; SET ;}
         ///  <Summary> 
        /// departure time
         ///  </ Summary> 
        public the DateTime CheckOutDate { GET ; SET ;} 


        // the TODO: other information
    }

     1.2 create an abstract class  PropertyValidAbstract.cs

    public abstract class PropertyValidAbstract
    {
        protected PropertyValidAbstract _baseValid;
        public void SetNext(PropertyValidAbstract BaseValid)
        {
            this._baseValid = BaseValid;
        }
        public static string ErrorMsg { get; set; }
        public abstract bool IsValid(OrderAddDTO dto);
    }

 

       1.3 Creating Available validation class: AvailableValid, and inherited from PropertyValidAbstract

    public  class AvailableValid: PropertyValidAbstract 
    { 
        public  the override  BOOL the IsValid (OrderAddDTO DTO) 
        { 
            // the TODO: Remove the property (property) of the Available 


            // IF (calculated property is available in the time dto.CheckInDate-dto.CheckOutDate)
             // {
             // not available, then the ErrorMsg assignment, and returns to false;
             // the TODO: embodied
             // } 


            // if there is no next verified returns true 
            IF ( the this ._baseValid == null ) 
            { 
                return  true ; 
            } 

            / /If available, the next enter a verification 
            return  the this ._baseValid.IsValid (DTO); 

        } 
    }

 

      1.4 Creating MinDay validation class MinDayValid.cs, and inherited from PropertyValidAbstract

    public  class MinDayValid: PropertyValidAbstract 
    { 
        public  the override  BOOL the IsValid (OrderAddDTO DTO) 
        { 
            // the TODO: Remove the property (property) of MinDay rules 


            // analog minday rules specific minday more complicated than this 
            var checkDays = (dto.CheckOutDate - dto.CheckInDate) .Days + . 1 ;
             IF (checkDays < . 3 ) 
            { 
                ErrorMsg = " Stay Least AT. 3 Days " ;
                 return  to false ; 
            } 

            // if there is no next verified returns true 
            IF (the this ._baseValid == null ) 
            { 
                return  to true ; 
            } 

            // if so, then entering a validation next 
            return  the this ._baseValid.IsValid (DTO); 
        } 
    }

 

 

      1.5 Creating CheckIn / Out validation class: CheckInOutDateValid.cs, and inherited from PropertyValidAbstract

    public class CheckInOutDateValid : PropertyValidAbstract
    {
        public override bool IsValid(OrderAddDTO dto)
        {

            //模拟checkin/checkout规则判断
            DayOfWeek checkInWeek = dto.CheckInDate.DayOfWeek;
            DayOfWeek checkOutWeek = dto.CheckOutDate.DayOfWeek;
            if (checkOutWeek!= DayOfWeek.Saturday||checkInWeek!= DayOfWeek.Tuesday)
            {
                ErrorMsg = "checkin/out Does not meet the";
                return false;
            }
            // If there is no next verified Returns true 
            IF ( the this ._baseValid == null ) 
            { 
                return  true ; 
            } 
            return  the this ._baseValid.IsValid (DTO); 
        } 
    }

 

      1.6 Finally, create a verification configuration class: ValidSettings.cs, and inherited from PropertyValidAbstract

    public class ValidSettings: PropertyValidAbstract
    {public override bool IsValid(OrderAddDTO dto)
        {
            //先进行AvailableValid
           PropertyValidAbstract BaseValid = new AvailableValid();
            //在验证MinDayValid
            var mindayvalid = new MinDayValid();
            BaseValid.SetNext(mindayvalid);
            //最后是CheckInOutDateValid
            var checkinOutvalid = new CheckInOutDateValid();
            mindayvalid.SetNext(checkinOutvalid);

            return BaseValid.IsValid(dto);
        }
    }

 

 

  2. In the console simulation:

        static  void the Main ( String [] args) 
        { 
            /// This DTO, is passed over from the client, at which analog 
            OrderAddDTO DTO = new new OrderAddDTO (); 
            dto.CheckInDate = the DateTime.Now; 
            dto.CheckOutDate = the DateTime. Now.AddDays ( . 1 ); 


            PropertyValidAbstract baseValid = new new ValidSettings (); 

            IF (baseValid.IsValid (DTO) == to false ) 
            { 
                Console.WriteLine (PropertyValidAbstract.ErrorMsg); 
            } 
            the else 
            { 
                //TODO: price calculation 
                Console.WriteLine ( "calculate price");
} the Console.ReadKey (); }

 

  3. Run Results:

    3.1 minday rule does not meet the test

    

 

 

 

 

     3.2 Test are met

    

 

 

 

  Operating results no problem, this time customers may demand that you give me add maxday (maximum number of days stay rule). At this time we can do this:

    1. Create a new maxday validation class: MaxDayValid.cs, and inherited from PropertyValidAbstract

    public  class MaxDayValid: PropertyValidAbstract 
    { 
        public  the override  BOOL the IsValid (OrderAddDTO DTO) 
        { 
            // the TODO: Remove the property (property) of MaxDay rules 


            // analog maxday rules specific maxday more complicated than this 
            var checkDays = (dto.CheckOutDate - dto.CheckInDate) .Days + . 1 ;
             IF (checkDays> 300 ) 
            { 
                ErrorMsg = " Stay up to 300 Days " ;
                 return  to false ; 
            } 

            // if there is no next verified, true is returned 
            IF (this._baseValid == null)
            {
                return true;
            }
            return this._baseValid.IsValid(dto);
        }
    }

    2. Modify the class ValidSettings.cs

    public class ValidSettings: PropertyValidAbstract
    {

        public override bool IsValid(OrderAddDTO dto)
        {
            //先进行AvailableValid
            PropertyValidAbstract BaseValid = new AvailableValid();
            //在验证MinDayValid
            var mindayvalid = new MinDayValid();
            BaseValid.SetNext(mindayvalid);



            //新增一个maxday
            var maxdayvalid = new MaxDayValid();
            mindayvalid.SetNext(maxdayvalid);

            //最后是CheckInOutDateValid
            var checkinOutvalid = new CheckInOutDateValid();
            maxdayvalid.SetNext(checkinOutvalid);

            return BaseValid.IsValid(dto);
        }
    }

 

 

 

    3. We look at the results:

  

 

 

Conclusion:

  I hope my article can help you, it is actually an example of an abstract class example of a design pattern used above, it seems to be Jiaosha chain of responsibility. I remember very clearly. About design patterns can not remember the dead, we have done "obtaining meaning, forget their shape." Thanks for watching.

 

Guess you like

Origin www.cnblogs.com/norain/p/11441869.html