Original address: xeblog.cn/articles/15

The introduction

“Cross Fire” this game was all the rage, as a mobile version of the NC fan, think that was also heroic ah. Heroes forget the brave, now almost not how to play… Don’t ask me why, ask be to have no money, ha ha ha ha belch ~ now of the game, as long as a charge money, all sorts of attribute enhancement, all sorts of pit father privilege, strength again high also play not win RMB player, in well-off road already lost road of I, can’t help but say 1: strength is not allowed.

Definition of decoration pattern

Decorator mode is the dynamic extension of an object’s functionality without changing the original class file or using inheritance. It wraps the real object by creating a wrapper object, a decoration.

For net addiction teenager, is the skin in the game, let the role in the game, weapons what become good-looking.

Generic class diagram for decorator pattern:

  • Component

    ComponentIs an interface or abstract class that defines a core object, a primitive business scenario.
  • ConcreteComponent

    ConcreteComponentIs the core, most primitive implementation of business logic, and that’s what you want to decorate.
  • Decorator

    Just as the name implies, it is a kind that does adornment technically, put it bluntly, decorate a worker namely. It is generally an abstract class that also needs to implement the most primitive business logic. Unlike other implementation classes, it needs to define oneprivateVariable pointing to theComponentObject, usually passed in as a constructComponentObject.
  • ConcreteComponentA and ConcreteComponentB are two concrete decoration classes, which enhance or weaken the original business logic. In other words, ConcreteComponentA and ConcreteComponentB are two skins in the game.

Implementation of decorative patterns

First, define a specific business game scenario, the most basic operations in CF games: login, fire, and grenade drop

UML class diagrams

Abstract artifact: CFGameService

/** * CF game service interface **@author anlingyi
 */
public interface CFGameService {

    /** * login *@param username
     * @param password
     */
    void login(String username, String password);

    /** * shoots */
    void fire(a);

    /** * discard grenade */
    void grenade(a);
}
Copy the code

Concrete component: CFerServiceImpl

/** ** CF player implementation class **@author anlingyi
 */
public class CFerServiceImpl implements CFGameService {

    @Override
    public void login(String username, String password) {
        System.out.println(username + ", login game successfully!);
    }

    @Override
    public void fire(a) {
        System.out.println("Fire...");
    }

    @Override
    public void grenade(a) {
        System.out.println("Deulei..."); }}Copy the code

Abstract decorator: CFGameDecorator

/** ** cf game decoration */
public abstract class CFGameDecorator implements CFGameService {

    /** * the object to be decorated */
    private CFGameService cfGameService;

    /** * passes the modified object * through the constructor@param cfGameService
     */
    public CFGameDecorator(CFGameService cfGameService) {
        this.cfGameService = cfGameService;
    }
    
    @Override
    public void login(String username, String password) {
        this.cfGameService.login(username, password);
    }

    @Override
    public void fire(a) {
        this.cfGameService.fire();
    }

    @Override
    public void grenade(a) {
        this.cfGameService.grenade(); }}Copy the code

Specific decorator classes: ArmsDecorator, EffectDecorator

/** * Weapon decoration **@author anlingyi
 */
public class ArmsDecorator extends CFGameDecorator {

    public ArmsDecorator(CFGameService cfGameService) {
        super(cfGameService);
    }

    @Override
    public void fire(a) {
        // Enhance weapons
        this.useFirearms();
        super.fire();
    }

    @Override
    public void grenade(a) {
        // Enhance grenade
        this.useGrenade();
        super.grenade();
    }

    /** * How to enhance the weapon */
    private void useFirearms(a) {
        System.out.println("Equipment: AK-47- Fire Unicorn");
    }

    /** * How to enhance grenade */
    private void useGrenade(a) {
        System.out.println("Gear: High-explosive hand Grenades."); }}Copy the code
/** * Effect decoration class **@author anlingyi
 */
public class EffectDecorator extends CFGameDecorator {

    public EffectDecorator(CFGameService cfGameService) {
        super(cfGameService);
    }

    @Override
    public void login(String username, String password) {
        / / VIP member
        System.out.println("Distinguished VVVVVIP Member");
        super.login(username, password);
    }

    @Override
    public void fire(a) {
        super.fire();
        // Add kill sound
        this.killSound();
    }

    @Override
    public void grenade(a) {
        super.grenade();
        // Add grenade dropping sound
        this.grenadeSound();
    }

    /** * Add kill sound */
    private void killSound(a) {
        System.out.println("Hey handsome!");
    }

    /** ** Added grenade drop sound */
    private void grenadeSound(a) {
        System.out.println("Go boom!"); }}Copy the code

Start the game

Civilian players enter the game…

/** * CF client **@author anlingyi
 */
public class CFClient {

    public static void main(String[] args) {
        CFGameService cf = new CFerServiceImpl();
        cf.login("Xiao"."123456"); cf.fire(); cf.grenade(); }}Copy the code

Game effects:

Xiao Yi, login the game successfully! Fire... Demine...Copy the code

No fun at all.

“This problem can be solved with money.” — Lao Tzu (Zi read 1)

RMB players enter the game…

/** * CF client **@author anlingyi
 */
public class CFClient {

    public static void main(String[] args) {
        CFGameService cf = new CFerServiceImpl();
        // Make money make money
        cf = new ArmsDecorator(cf);
        cf.login("Xiao"."123456"); cf.fire(); cf.grenade(); }}Copy the code

Game effects:

Xiao Yi, login the game successfully! Equip: AK-47- Fire Kirin fire... Equipment: High-explosive grenade mine...Copy the code

Instant NB much, even the next door children are greedy cry… Ha ha ha ~

Continue to charge

/** * CF client **@author anlingyi
 */
public class CFClient {

    public static void main(String[] args) {
        CFGameService cf = new CFerServiceImpl();
        // Make money make money
        cf = new ArmsDecorator(cf);
        // Recharge (modify effect)
        cf = new EffectDecorator(cf);
        cf.login("Xiao"."123456"); cf.fire(); cf.grenade(); }}Copy the code

Game effects:

Distinguished VVVVVIP Xiaoyi, login the game successfully! Equip: AK-47- Fire Kirin fire... Hey handsome! Equipment: High-explosive grenade mine... Send ah boom!Copy the code

More charge more NB, more NB more fun, more fun more charge… From then on, on the road of poverty walk farther and farther.

Advantages and disadvantages of decoration mode

advantages

  • Decorator and decorator classes can develop independently, rather than coupling to each other.ComponentClasses don’t need to knowDecoratorThe existence of classes,DecoratorClasses extend from the outsideComponentClass function, whileDecoratorClasses also don’t have to know the concrete artifacts.
  • The decorator pattern is an alternative to inheritance, and decorators have the same interface as real objects.
  • Decorator patterns can dynamically extend the functionality of an implementation class, enhancing or weakening the functionality without modifying the structure of the original object.

disadvantages

  • Excessive use of decorator mode can make the application more complex.

resources

  • The source code

reference

  • Zen of Design Patterns