假设我想创造一个游戏.在游戏开始时,玩家将挑选一个怪物.
公平地挑选怪物很容易.
// get all monsters with equal chance
public Monster getMonsterFair(){
Monster[] monsters = {new GoldMonster(), new SilverMonster(), new BronzeMonster()};
int winIndex = random.nextInt(monsters.length);
return monsters[winIndex];
}
Run Code Online (Sandbox Code Playgroud)
并不公平地挑选怪物.
// get monsters with unequal chance
public Monster getMonsterUnFair(){
double r = Math.random();
// about 10% to win the gold one
if (r < 0.1){
return new GoldMonster();
}
// about 30% to winthe silver one
else if ( r < 0.1 + 0.2){
return new SilverMonster();
}
// about 70% …
Run Code Online (Sandbox Code Playgroud) 我有一个使用实体组件系统(ECS)的现有工作C ++游戏库。
我的图书馆的用户想创建一些组件,例如Cat
:-
class Cat{ public:
int hp;
float flyPower;
};
Run Code Online (Sandbox Code Playgroud)
他可以通过以下方式修改hp
每个内容cat
:
for(SmartComponentPtr<Cat> cat : getAll<Cat>()){
cat->hp-=5; //#1
}
Run Code Online (Sandbox Code Playgroud)
几天后,他想拆分Cat
为HP
和Flyable
:-
class HP{ public:
int hp;
};
class Flyable{ public:
float flyPower;
};
Run Code Online (Sandbox Code Playgroud)
因此,每次cat
访问hp
都会编译错误(例如,#1
在上面的代码中)。
为了解决这个问题,用户可以将他的代码重构为:
for(MyTuple<HP,Flyable> catTuple : getAllTuple<HP,Flyable>()){
SmartComponentPtr<HP> hpPtr=catTuple ; //<-- some magic casting
hpPtr->hp-=5;
}
Run Code Online (Sandbox Code Playgroud)
它可以工作,但是需要在用户代码中进行大量重构(调用的各个位置cat->hp
)。
在ECS中拆分组件时如何编辑框架/引擎以解决可维护性问题?
我从未发现过任何不会受到此问题困扰的方法,例如:-
vel.dx = 0.;
行)int currentHealth; …
c++ maintainability game-engine c++14 entity-component-system
这是一个很好的文档正则表达式,易于理解,维护和修改.
text = text.replace(/
( // Wrap whole match in $1
(
^[ \t]*>[ \t]? // '>' at the start of a line
.+\n // rest of the first line
(.+\n)* // subsequent consecutive lines
\n* // blanks
)+
)
/gm,
Run Code Online (Sandbox Code Playgroud)
但是你如何处理这些?
text = text.replace(/((^[ \t]*>[ \t]?.+\n(.+\n)*\n*)+)/gm,
Run Code Online (Sandbox Code Playgroud)
是否有某种美化器能够理解它并描述其功能?
昨天我不得不回到几周前我曾经工作的页面来重做UI.UI由带有3个选项卡的jQuery UI选项卡控件组成.每个选项卡内部有3-5个控件,还有一个提交按钮,只在选项卡中提交数据.我不得不重新组织一些选项卡,删除一些文本框,添加一些下拉列表,修改一些行为,甚至在客户端验证(使用jQuery验证)上工作一点.我想在这个练习中发现的是,我必须回过头来重新检查我的每一个jQuery选择器.有些人完好无损,但其中许多人都改变了.
我想知道人们使用什么设计模式(如果有的话)来避免或最小化重构或重新编写具有大量jQuery使用的网页的影响.我敢肯定它不能只是"搜索"+"搜索和替换".
我对scala有一些经验.我想将它介绍给10个成员的新项目.但是尽管scala具有所有表现力,但我不确定代码可以保持多么简单并转移给新的团队成员.目前我只研究了scala http://code.google.com/p/factorie/上的一个大项目和一些scala DSL(apache camel).所以我的问题是,与中级开发人员团队的java,python和groovy相比,scala可维护/可支持多少?
免责声明:我很乐意在这个项目中使用依赖注入,并且全面地采用基于接口的松散耦合设计,但是在这个项目中使用了依赖注入.另外,SOLID设计原则(以及一般的设计模式)在我工作的地方是外国的,我自己也是很多人的新手.因此,在为此问题提出更好的设计时,请考虑到这一点.
这是我正在研究的代码的简化版本,因此它可能看起来很人为.如果是这样,我道歉.考虑以下类:
// Foo is a class that wraps underlying functionality from another
// assembly to create a simplified API. Think of this as a service layer class,
// a facade-like wrapper. It contains a helper class that is specific to
// foo. Other AbstractFoo implementations have their own helpers.
public class Foo : AbstractFoo
{
private readonly DefaultHelper helper;
public override DefaultHelper Helper { get { return helper; } }
public Foo() …
Run Code Online (Sandbox Code Playgroud) 我最近和朋友讨论了枚举与公共静态最终常量.我告诉他公共静态最终常量比枚举更易于维护,有时更快(android开发人员文档确认这一点),也更方便.我还说过在使用枚举时你也失去了功能:
然后,他说如果你需要实例化或扩展枚举,你不应该使用枚举.然后我回答说这就是为什么我们应该只使用常量,因为它更易于维护; 如果中间项目需要实例化枚举或扩展它,该怎么办?然后我们必须改变一切.
我演示了枚举与常量的示例来说明我的观点:
public enum WeekDay {
/*
* We will start at 1 for demonstration
*/
SUNDAY("Sunday", 1), MONDAY("Monday", 2), TUESDAY("Tuesday", 3), WEDNESDAY(
"Wednesday", 4), THURSDAY("Thursday", 5), FRIDAY("Friday", 6), SATURDAY(
"Saturday", 7);
/*
* Notice we cannot do this...This is where enums fail.
*/
// LUNES("lunes",1), MARTES("martes",2);
private String dayName;
private int dayIndex;
private WeekDay(String dayName, int dayIndex) {
this.dayName = dayName;
this.dayIndex = dayIndex;
}
public String getDayName() {
return dayName;
}
public void setDayName(String …
Run Code Online (Sandbox Code Playgroud) 我有很多方法在大部分内容遵循相同的算法,理想情况下我希望能够调用一个消除大量代码重复的泛型方法.
我有很多类似下面的方法,我最好希望能够只调用
Save<SQLiteLocation>(itemToSave);
但是我有很多麻烦,因为这些方法
SQLiteConnection.Find<T>
不会接受像泛型中的T这样的抽象数据类型.
有没有办法解决这个问题,如果我能解决这个问题,我会节省多达150行代码
这是我的代码:
public bool SaveLocation(ILocation location, ref int primaryKey)
{
var dbConn = new SQLiteConnection (dbPath);
SQLiteLocation itemToSave = new SQLiteLocation ();
itemToSave.LocationName = location.LocationName;
itemToSave.Latitude = location.Latitude;
itemToSave.Longitude = location.Longitude;
itemToSave.PrimaryKey = location.PrimaryKey;
----------------------------------------------------------------------------------------
SQLiteLocation storedLocation = dbConn.Find<SQLiteLocation>
(x => x.PrimaryKey == location.PrimaryKey);
if (storedLocation != null)
{
dbConn.Update(itemToSave);
return true;
}
else if (storedLocation == null)
{
dbConn.Insert(itemToSave);
primaryKey = itemToSave.PrimaryKey;
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
在这里另一种方法看看我的虚线下面的两种方法中的代码基本上是一样的
public bool SaveInvitation(IInvitation invitation, …
Run Code Online (Sandbox Code Playgroud) 这是我想要实现的目标的一个小例子。我有一个有很多参数的类 - Dog
。我有一个子类JumpyDog
,我想了解如何“扩展” 的实例Dog
以使其成为 的实例JumpyDog
。
class Dog {
int age, numberOfTeeth, grumpiness, manyOtherParameters;
JumpyDog learnToJump(int height) {
JumpyDog jumpy = new JumpyDog(this); // I do not want to copy all parameters
jumpy.jumpHeight=height;
return jumpy;
}
}
class JumpyDog extends Dog {
int jumpHeight;
void jump(){}
}
Run Code Online (Sandbox Code Playgroud)
或者我可以这样说:
Dog dog=new Dog();
dog.makeJumpy();
dog.jump()
Run Code Online (Sandbox Code Playgroud) 我有一个带有16个按钮的WPF表单。当我的视图模型初始化时,我需要将全部16个设置为RelayCommand对象。这是我的Initialize()方法所做的全部,但是却导致代码分析错误CA1502:AvoidExcessiveComplexity。
这是抑制CA警告的一个好案例,还是有一种更优雅的方式来设置这些命令而不引起CA违规?
[SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity", Justification = "Simply setting the commands")]
private void Initialize()
{
this.AddApplicationCommand = new RelayCommand(_ => AddApplication());
this.DeleteApplicationCommand = new RelayCommand(_ => DeleteApplication(), _ => ApplicationIsSelected);
this.RefreshApplicationsCommand = new RelayCommand(_ => RefreshApplications());
this.SaveApplicationCommand = new RelayCommand(_ => SaveApplication(), _ => ApplicationIsSelected);
this.ForceInstallationCommand = new RelayCommand(_ => ForceInstallation(), _ => ApplicationIsSelected);
this.DeleteForceInstallationCommand = new RelayCommand(_ => DeleteForceInstallation(), _ => ApplicationIsSelectedAndForceExists());
this.AddTaskCommand = new RelayCommand(_ => AddTask(), _ => ApplicationIsSelected);
this.EditTaskCommand = new RelayCommand(_ => EditTask(), _ => TaskIsSelected()); …
Run Code Online (Sandbox Code Playgroud) maintainability ×10
java ×4
c# ×3
oop ×2
algorithm ×1
c++ ×1
c++14 ×1
constants ×1
enums ×1
game-engine ×1
generics ×1
inheritance ×1
instance ×1
jquery ×1
performance ×1
refactoring ×1
regex ×1
scala ×1