zoukankan      html  css  js  c++  java
  • gof-组件模式

    应用场景:类关联的域比较多,功能过多 你懒通过多重继承实现功能细分时

    体验:逻辑更有层次感,功能迭代更加便捷,运维难度-1

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using System;
    
    namespace ComponentPatter_original
    {
    
        public class World {
            public void resolveCollision(int x,int y,int z) { }
        }
    
        public class Graphic {
            public void draw(int x,int y) { }
        }
    
        public static class InputInfo {
            public static int GetJoyDirection() {
                return 0;
            }
        }
    
    
        public class Bjron {
            private static readonly int WALK_ACCCELERATION = 1;
            private int x, y, velocity;
            private Sprite stand, walkLeft, walkRight;
            public Bjron():this(0,0,0) {}
            public Bjron(int x, int y, int velocity) {
                this.x = x;
                this.y = y;
                this.velocity = velocity;
            }
            public void Update(World world, Graphic graphics) {
                switch (InputInfo.GetJoyDirection()) {
                    case 1: x += WALK_ACCCELERATION; break;
                    case 2: y += WALK_ACCCELERATION; break;
                }
    
    
                this.velocity += WALK_ACCCELERATION;
                world.resolveCollision(x,y,velocity);
                graphics.draw(x,y);
            }
        }
    }
    
    namespace ComponentPatter_Improve {
        public class World
        {
            public void resolveCollision(int x, int y, int z) { }
        }
    
        public class Graphic
        {
            public void draw(int x, int y) { }
        }
    
        public static class InputInfo
        {
            public static int GetJoyDirection()
            {
                return 0;
            }
        }
    
        //将处理输入操作的部分独立出来
        public class InputComponent {
            public void Update(Bjron bjron) {
                switch (InputInfo.GetJoyDirection())
                {
                    case 1: bjron.x += WALK_ACCCELERATION; break;
                    case 2: bjron.y += WALK_ACCCELERATION; break;
                }
            }
            private readonly int WALK_ACCCELERATION = 1;
        }
    
        public class PhysicsComponent {
            private int velocity;
            public void Update(Bjron bjorn, World world) {
                world.resolveCollision(bjorn.x, bjorn.y, velocity);
            }
        }
    
        public class GraphyicsComponent {
            public float color_r, color_g, color_b;
            public void Update(Bjron bjorn, Graphic gph)
            {
                gph.draw(bjorn.x, bjorn.y);
            }
        }
    
        public class Bjron
        {
            private static readonly int WALK_ACCCELERATION = 1;
            public int x, y, velocity;
            private Sprite stand, walkLeft, walkRight;
            
            public Bjron() : this(0, 0, 0) { }
            public Bjron(int x, int y, int velocity)
            {
                this.x = x;
                this.y = y;
                this.velocity = velocity;
            }
    
            private InputComponent input;
            private PhysicsComponent physic;
            private GraphyicsComponent graphic;
            public void Update(World world, Graphic graphics)
            {
                input.Update(this);
                physic.Update(this,world);
                graphic.Update(this,graphics);
            }
        }
    }
    
    
    namespace ComponentPatter_Final
    {
        //组件与组件之前通讯的两种方式 1,把组件包丢给组件使用 这样的话耦合度比较高 逻辑处理起来比较散乱 看团队喜好了...
        //另一种如下 创建一个中介者的接口用于 把功能包装成命令 在中介中枢集中处理消息 个人感觉这样写起来比较明了 这里不写了.... 用第一种
        public abstract class IComponent_Sample{
            public virtual void SendMessage(string msg) {
    
            }
        }
        public class World
        {
            public void resolveCollision(int x, int y, int z) { }
        }
    
        public class Graphic
        {
            public void draw(int x, int y) { }
        }
    
        public static class InputInfo
        {
            public static int GetJoyDirection()
            {
                return 0;
            }
        }
    
        //将功能细分 逐个处理
        public class InputComponent:IComponent_Sample
        {
            public virtual void Update(GmaeObj bjron)
            {
                switch (InputInfo.GetJoyDirection())
                {
                    case 1: bjron.x += WALK_ACCCELERATION; break;
                    case 2: bjron.y += WALK_ACCCELERATION; break;
                }
            }
    
            private readonly int WALK_ACCCELERATION = 1;
        }
    
        public class InputComponent_demo1:InputComponent {
            public override void Update(GmaeObj gameObject) {
                base.Update(gameObject);
                //迭代或添加功能
            }
        }
        public class PhysicsComponent
        {
            private int velocity;
            public virtual void Update(GmaeObj gameObject, World world)
            {
                world.resolveCollision(gameObject.x, gameObject.y, velocity);
            }
            public virtual void AddVelocity() {}
        }
    
        public class GraphyicsComponent
        {
            public float color_r, color_g, color_b;
            public virtual void Update(GmaeObj gameObject, Graphic gph)
            {
                gph.draw(gameObject.x, gameObject.y);
                gameObject.physic.AddVelocity();
            }
        }
    
        public class GmaeObj
        {
            private static readonly int WALK_ACCCELERATION = 1;
            public int x, y, velocity;
            private Sprite stand, walkLeft, walkRight;
    
            public GmaeObj() : this(0, 0, 0) { }
            public GmaeObj(int x, int y, int velocity)
            {
                this.x = x;
                this.y = y;
                this.velocity = velocity;
            }
            //用来更换组建
            public GmaeObj(InputComponent ic,PhysicsComponent pc, GraphyicsComponent gc):this(){
                this.input = ic;
                this.physic = pc;
                this.graphic = gc;
    
            }
    
            //组件向外界暴漏用来做功能迭代和组件交互
            public InputComponent input;
            public PhysicsComponent physic;
            public GraphyicsComponent graphic;
            public void Update(World world, Graphic graphics)
            {
                input.Update(this);
                physic.Update(this, world);
                graphic.Update(this, graphics);
            }
        }
    
        public class StartGame {
            void Start() {
                var game = new GmaeObj();
                //更新组件
                game.input = new InputComponent_demo1();
    
            }
        }
    }
  • 相关阅读:
    PIE SDK专题制图下屏幕坐标转地图坐标
    PIE SDK导出图片
    PIE SDK专题制图切换模板
    PIE SDK专题制图另存模板
    PIE SDK专题制图保存模板
    PIE SDK专题制图打开模板
    PIE SDK元素位置和显示样式的修改
    day 16 特殊权限与输入输出相关作业
    day 16 特殊权限和输出输入符
    day 15 权限概述作业
  • 原文地址:https://www.cnblogs.com/chenggg/p/11409445.html
Copyright © 2011-2022 走看看