Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by NikoBusiness · Jul 01, 2016 at 12:56 AM · classdesign

is this Good Design? OOP Loose Couple Principle?

hello! i want to show you if my Design is "correct" according to OOP principles.

info :

  • there is a ball which collects coins

  • the ball is the player.

  • there is a game manager

so before you see the Scripts i've made a graph :

alt text

i've made a Skill Class :

 using UnityEngine;
 using System.Collections;
 
 public class Skill {
 
     public int Duration { get; set; }
     SkillType Type;
 
 
 
     public enum SkillType
     {
         Unknown,
         Shield,
         Life,
     }
 
     //Constructor
     public Skill(int _duration,SkillType _type)
     {
         Duration = _duration;
         Type = _type;
     }
 
     public SkillType getType()
     {
        return Type;
     }
 }
 

then there is a coin prefab that has attached CoinSkill script on it

 using UnityEngine;
 using System.Collections;
 
 public class CoinSkill : MonoBehaviour {
 
     Skill ShieldSkill = new Skill(10, Skill.SkillType.Shield);
     GameCoreManager gameManager;
 
     // Use this for initialization
     void Start () {
         Destroy(gameObject, ShieldSkill.Duration);
         gameManager = GameObject.FindWithTag("GameController").GetComponent<GameCoreManager>();
     
     }
     
     void OnCollision2D(Collision2D collider)
     {
         if(collider.gameObject.tag == "Player")
         {
             gameManager.CollectCoin(ShieldSkill.getType(),collider.gameObject);
             Destroy(gameObject);
         }
     }
 
 }

& GameCoreManager script attached to GameManager Object

 using UnityEngine;
 using System.Collections;
 
 public class GameCoreManager : MonoBehaviour {
 
 
 
     public void CollectCoin(Skill.SkillType _type,GameObject player)
     {
         if(_type == Skill.SkillType.Shield)
         {
             player.GetComponent<SkillActivator>().ActivateSkill(_type);
         }
     }
 
 }

and finally the skillactivator script attached to player (ball) object

 using UnityEngine;
 using System.Collections;
 
 public class SkillActivator : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
     public void ActivateSkill(Skill.SkillType _type)
     {
         if(_type == Skill.SkillType.Shield)
         {
             activateShieldSkill();
         }
     }
 
 
     public void activateShieldSkill()
     {
         //todo
     }
 }
 

oop-principle.jpg (102.4 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by RasmusDyhr · Jul 02, 2016 at 12:37 AM

I would probably encapsulate the skills more. Have the skill object store the method to be activated, to decouple it completely from the SkillActivator.

I however feel that the GameManager is an issue in its current state as it doesn't do anything. You could just as easily call the SkillActivator class from the CoinSkill class. And if you refactor out the GameManager and follow my first advice of encapsulating the Skill logic in the Skill class, then the SkillActivator becomes redundant, and should be refactored away as well.

The bottom line is that you're risking fragmenting rather than decoupling your code, if you just do it for the sake of "good design".

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NikoBusiness · Jul 22, 2016 at 02:59 AM 0
Share

thank you man!i am just trying to improve to good design!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

59 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Using constructors in Unity (C#) 8 Answers

(SOLVED) Accessing (string) arrays by enumerations from another class 0 Answers

How to create new for my custom class? 2 Answers

Can Static classes have static lists? 1 Answer

how to accsess sibling variables from a class. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges