How to use MVC in practice

I’m very confused. I can’t even begin to understand how MVC would be implemented outside of web development. This might seem like too general a question, but how would one apply MVC. I have the following general questions:

  1. Are M, V, and C all meant to be one class each, or many. If many, how would
    that work.

  2. Most classes I’ve made previously have had their data inside them, not in a separate class. How would this work with MVC?

1) For instance, lets say you have a class where you take care of a virtual dog. I would think that you would make a Dog class with, for example, a “bark” command that would play a sound along with a name variable and a coat_color variable. I know this is very simple, but how would this fit inside MVC? It seems that you would end up with MC and V, where the information (model) AND the controls were in the Dog class, which would maybe access swing, or whatever library, to update the view.

2) Or, what about a program like sims (simplified version) where each person would have their own information… would you put all that info in another class?

Sorry if these are all based on giant misconceptions, but I’m pretty confused. Right now I’m using Java, if that matters for MVC…

You’re a little all over the map there, perhaps getting ahead of yourself thinking about video games and dogs and what not. The easiest way to think of MVC is to think of the responsibilities of the things in the acronyms. At its core level, each of the components answers a question:

  • Model: “What should we show the user?”
  • View: “How should we show it to the user (what will it look like)?”
  • Controller: “How do I figure out which models and views to show the user?”

So, I’d suggest digesting that a bit and perhaps focusing your questions a little. MVC is a “presentation pattern,” meaning that it’s not the basis for your entire application nor is it a philosophy or a universal approach to software development. All it really gives you is a way to separate the responsibilities involved in presenting information to your users. In this sense, you can use it on the web or the desktop or anywhere that you show things to users.

1

Heres something to think about. You used the example of a Dog class. then you ask yourself – what do dogs do? well they “bark”. so lets make a method to Bark.

so then you have your Dog controller, and you put a Bark method in it. and then maybe you put specific things about Barking – the loudness, the tone, etc in a Bark Model.

and then you create methods for other actions like Walk and Eat and put them in your controller. and everything is working great and looks very logical.

But next your customer wants your dog to “whine” — while begging. should you create a whine method? should you create a begging method and put something in about whining in there?

The problem was in the very first method we made. when we made a method called Bark, we set ourselves up to have a cluttered controller that can’t grow. we should have asked the next question: What is Barking? its a sound that dogs can make. can dogs make other sounds? yes.

So what we REALLY need – before Barking – is a simple method in the Controller called makeDogSound or getDogSound etc, which then calls a Model DogSounds.

Then we can easily add more types of dog sounds to the Model – whine, growl, bark, woof – and the Controller is not changed! We can send different types of dog sounds to the view – again the controller is not changed in any way – its retrieving the sound from the model, and sending it to the view.

the controller is like a mailman – he picks up letters and delivers them. he does not look inside the letter!

eventually the DogSounds model could reference other models depending upon the type of dog. this can get very complex – but the structure in the controller – and how it delivers the assets to the view – remains consistent.

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật