Appearance mode ---- C ++ implementation

Source: Internet
Author: User

Appearance Mode

Definition:

Provides a consistent interface for a group of interfaces in the subsystem. The appearance mode defines a high-level interface, which makes the subsystem easier to use.

Applicability:

1. When you want to provide a simple interface for a complex subsystem.

2. There is a large dependency between the implementation part of the client program and the abstract class.

3. When you need to build a layered subsystem, use the appearance mode to define the entry points for each layer of the subsystem.

Structure:


Implementation: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + Ly/X08 + left "left"> class ClassA

{

Public:

VoiddoSomethingA ()

{

Cout <"ClassA doSomethingA" <

}

};

Class ClassB

{

Public:

VoiddoSomethingB ()

{

Cout <"ClassB doSomethingB" <

}

};

Class ClassC

{

Public:

VoiddoSomethingC ()

{

Cout <"ClassC doSomethingC" <

}

};

// The encapsulated appearance class, which provides three methods to access the sub-system class

Class Facade

{

Public:

Facade ()

{

M_pa = newClassA;

M_pb = newClassB;

M_pc = newClassC;

}

~ Facade ()

{

If (m_pa)

{

Deletem_pa;

}

If (m_pb)

{

Deletem_pb;

}

If (m_pc)

{

Deletem_pc;

}

}

VoidmethodA ()

{

M_pa-> doSomethingA ();

}

VoidmethodB ()

{

M_pb-> doSomethingB ();

}

VoidmethodC ()

{

M_pc-> doSomethingC ();

}

Private:

ClassA * m_pa;

ClassB * m_pb;

ClassC * m_pc;

};

Facade * pf = newFacade;

Pf-> methodA ();

Pf-> methodB ();

Pf-> methodC ();

Note:

1. subsystems can have multiple appearances. Different modules may require different functions of the method subsystem, so there may be multiple appearances

2. The encapsulated appearance class does not participate in the business logic of the subsystem. This means that the following situations should not occur:

Void methodB ()

{

M_pb-> doSomethingB ();

M_pc-> doSomethingC ();

}

If you do need to do this, it is best to add an encapsulation class independently of ClassB and ClassC.

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.