Robot Pattern)

Source: Internet
Author: User

 

Namespace Designpattern. Interpreter
{
Public   Abstract   Class Expression
{
Public Abstract BoolInterpret ();
}  
 
Public   Class Constant: Expression
{
Private   Bool M_val;
 
Public Constant ( Bool Val)
{
This. M_val=Val;
}  
 
Public   Override   Bool Interpret ()
{
Return This. M_val;
}  
 
Public   Override   String Tostring ()
{
Return This. M_val.tostring ();
}  
}  
 
Public   Class And: Expression
{
Public Expression left, right;

Public And (expression left, expression right)
{
This. Left=Left;
This. Right=Right;
}  
 
Public   Override   Bool Interpret ()
{
ReturnLeft. interpret ()&&Right. interpret ();
}  
 
Public   Override   String Tostring ()
{
Return   " ( " + This . Left. tostring () + " && " + This . Right. tostring () + " ) " ;
}  
}  
 
Public   Class Or: Expression
{
Public Expression left, right;

Public Or (expression left, expression right)
{
This. Left=Left;
This. Right=Right;
}  
 
Public   Override   Bool Interpret ()
{
ReturnLeft. interpret ()|Right. interpret ();
}  
 
Public   Override   String Tostring ()
{
Return   " ( " + This . Left. tostring () + " | " + This . Right. tostring () + " ) " ;
}  
}  
 
Public   Class Not: Expression
{
Private Expression exp;
 
Public Not (expression exp)
{
This. Exp=Exp;
}  
 
Public   Override   Bool Interpret ()
{
Return !Exp. interpret ();
}  
 
Public   Override   String Tostring ()
{
Return "(!"+Exp. tostring ()+")";
}  
}  
 
Public   Class Client
{
Public   Static   Void Main ()
{
Expression exp = New Or ( New And ( New Constant ( True ), New Constant ( False )),
New And ( New Constant ( True ), New Not ( New Constant ( False ))));
Console. writeline (exp. tostring () + " = " + Exp. interpret ());
}  
}  
}  

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.