简体   繁体   English

如何更好地组织框架中的类/包,以便我的应用程序的客户端可以轻松扩展它们?

[英]How to better organize classes/packages in a framework so that the client of my application can easily extend them?

Let's assume I am in charge of developing a Scrabble game, being that one of the principal requirements of the client is the ability to later try out different ways and modes of the game. 让我们假设我负责开发Scrabble游戏,因为客户的主要要求之一是能够在以后尝试不同的游戏方式和模式。 I already made a design that is flexible enough to support those kinds of changes. 我已经制作了一个足够灵活的设计来支持这些变化。 The only question left is what to expose to the client(objects' access modifiers), and how to organize it (how to expose my objects in namespaces/packages). 剩下的唯一问题是暴露给客户端的内容(对象的访问修饰符),以及如何组织它(如何在命名空间/包中公开我的对象)。

How should I define things such that the client can both easily use my standard implementation (a standard Scrabble game, and yet be able to make all the modifications that he wants? I guess what I need is a kind of framework, on which he can work on. 我应该如何定义这样的东西,使客户端可以轻松使用我的标准实现(标准的Scrabble游戏,但能够进行他想要的所有修改?我猜我需要的是一种框架,他可以在从事于。

I organized my classes/interfaces in a non-strict layered system: 我在非严格的分层系统中组织了我的类/接口:

Data Types 数据类型

Contains basic data types that might be used in the whole system. 包含可能在整个系统中使用的基本数据类型。 This package and its members can be accessed by anyone in the system. 系统中的任何人都可以访问此程序包及其成员。 All its members are public. 其所有成员都是公开的。

Domain

Contains all the interfaces I've defined and that might be useful to be able to make client's new Scrabble's implementations. 包含我定义的所有接口,这些接口可能有助于创建客户端新的Scrabble实现。 Also contains value types, like Piece, that are used in the game. 还包含游戏中使用的值类型,如Piece。 All its members are public. 其所有成员都是公开的。

Implementations 实现

Contains all the needed classes/code to implement my standard Scrabble game in a Implementations.StandardScrabble package. 包含所有需要的类/代码,以在Implementations.StandardScrabble包中实现我的标准Scrabble游戏。 If the client decides to implement other variants of the game, he can create them in Implementations.XYZ, for example. 如果客户决定实现游戏的其他变体,他可以在Implementations.XYZ中创建它们。 These classes are all package protected and the only thing that is available to the outside of the package is a Game façade. 这些类都受到包保护,并且包外部唯一可用的是游戏外观。 Uses both Domain and Data Types packages. 使用域和数据类型包。

UI UI

Contains the UI class that I have implemented so that both the client and the users of the program can run the game (my implementation). 包含我已实现的UI类,以便客户端和程序用户都可以运行游戏(我的实现)。 Can access all the other layers. 可以访问所有其他图层。


There are several drawbacks to the way I am organizing things, the most obvious being that if the client wants to create its own version of the game, he will have to basically implement almost everything by himself(I share in the Domain the interfaces, but he can do almost nothing with them). 我组织事物的方式有几个缺点,最明显的是如果客户想要创建自己的游戏版本,他必须基本上自己实现几乎所有东西(我在域中共享接口,但是他几乎什么都不做。) I feel I should maybe pass all the Implementation's classes to the Domain and then only have a Façade that builds up my standard Scrabble in the Implementations namespace? 我觉得我应该将所有的实现类传递给Domain,然后只有一个Façade在Implementations命名空间中构建我的标准Scrabble?

How would you approach this? 你会怎么做? Is there any recomended reading on how to build this kind of programs (basically, frameworks)? 是否有关于如何构建这种程序(基本上是框架)的推荐阅读?

Thanks 谢谢

For the algorithms and strategies I would define interfaces and default implementations, and provide abstract superclasses which are extended by you own implementations, so that all the boilerplate code is in the abstract superclass. 对于算法和策略,我将定义接口和默认实现,并提供由您自己的实现扩展的抽象超类,以便所有样板代码都在抽象超类中。 In addition I would allow the client to subclass your impl. 另外我会允许客户端继承你的impl。 Just make more than one impl, and you see what to place where. 只要制作一个以上的impl,你就会看到放在哪里。

But most important: Give your client the code. 但最重要的是:为您的客户提供代码。 If he needs to understand where to place his code, he should be able to see what you have coded, too. 如果他需要了解放置代码的位置,他也应该能够看到你编码的内容。 No need to hide stuff. 无需隐藏东西。

I think that you're trying to give too much freedom to a client. 我认为你试图给客户太多的自由。 This must be making things that difficult for you to handle. 这必须使你难以处理的事情。 Based on what you have described it seems that a client will be able to modify almost all parts of your game - model, logic, UI... I think it would be better to restrict modifiable areas in your application but expose some via general Plugin interface set. 根据您所描述的内容,似乎客户端几乎可以修改游戏的所有部分 - 模型,逻辑,UI ......我认为最好限制应用程序中的可修改区域但通过常规Plugin公开一些界面集。 This would make it easier for a user as well - he will only need to learn how plugins work, not the entire application's logic. 这将使用户更容易 - 他只需要了解插件的工作方式,而不是整个应用程序的逻辑。 Define areas for your plugins if you want - UI plugin, game mode plugin and so on. 如果需要,可以为插件定义区域 - UI插件,游戏模式插件等。 Many production applications and games work in such way (recall Diablo II and that AMAZING variety of plugins it has!). 许多制作应用程序和游戏以这种方式工作(回想起暗黑破坏神II及其拥有的令人惊奇的各种插件!)。

Whatever design you come up with, I would err on the side of hiding as much of the implementation as possible. 无论你想出什么样的设计,我都会尽可能地隐藏尽可能多的实现。 Once you expose an implementation, you cannot take it back (unless you're ready to wage a flame war with your client base). 一旦你暴露了一个实现,你就无法收回它(除非你准备好与你的客户群发生火焰战争)。 You can always provide default implementations later as you see fit. 您可以随时根据需要提供默认实现。

Generally, I'd start with only providing thin interfaces. 一般来说,我首先只提供瘦接口。 Then, before providing abstract classes, I might offer utility classes (eg, Factories , Builders , etc.). 然后,在提供抽象类之前,我可能会提供实用程序类(例如, FactoriesBuilders等)。

I'd recommend reading Effective Java by Josh Bloch for useful general practices when designing object-oriented code. 我建议在设计面向对象的代码时阅读Josh Bloch的Effective Java ,了解有用的一般实践。

MVC/Compund Pattern MVC / Compund模式

You may release earlier version of your package. 您可以发布包的早期版本。 later on you can upgrade it based on user requirement. 稍后您可以根据用户要求进行升级。

If you are using MVC or other compound pattern wisely, I believe you also can upgrade your package easily. 如果您明智地使用MVC或其他复合模式,我相信您也可以轻松升级您的包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何组织我的课程? - How to organize my classes? 如何更好地组织C#代码? - How can I better organize my code in C#? AspIdentiy ApplicationUserManager是Static,如何扩展以便它参与我的IoC框架? - AspIdentiy ApplicationUserManager is Static, how to extend so it participates in my IoC framework? 替换列表中的特殊字符,以便您轻松搜索 - Replacing special characters in a list so you can search them easily 如何更好地减少类的依赖关系,以便于对其进行单元测试 - How to better reduce classes dependencies so it is easier to Unit-Test them 在视觉上堆叠了大量的分组框,如何更好地组织它们? - Massive amount of groupboxes visually stacked, how to better organize them? 如何在基类中声明构造函数,以便子类可以在不声明它们的情况下使用它们? - How to declare constructors in base classes so that sub-classes can use them without declaring them? 怀疑如何组织我的课程 - Doubt about how to organize my classes 我应该在部分类中组织我的实体框架服务层吗? - Should I organize my Entity Framework Service Layer in partial classes? 如何存储WCF会话,以便其他应用程序可以访问它们 - How to store WCF sessions so another application can access them
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM