简体   繁体   English

Android / Java中的OOP

[英]OOP in Android/Java

I am currently developing an flashcard-app for on reason because i don't like the existing ones and the second reason is that i want to improve my java/android skills. 我目前正在开发一个flashcard-app,因为我不喜欢现有的,第二个原因是我想提高我的java / android技能。

In my app i tried to use OOP. 在我的应用程序中,我试图使用OOP。 My Cards are stored in a sqlite database. 我的卡存储在sqlite数据库中。

Now my question: Currently my Object "Card" which represents a single Flashcard has its own DatabaseAdapter which it can use to update its own state ie its points. 现在我的问题:目前我的对象“卡”代表一个单独的Flashcard有自己的DatabaseAdapter,它可以用它来更新自己的状态,即它的点。

 Card card = new Card();
 card.setPoints(10); //<-- Also update its row in the database

Or is it better to do something like that: 或者做这样的事情会更好:

 DBAdapter dbAdapter = new DBAdapter();
 Card card = new Card();
 dbAdapter.setPoints(Card);//<-- Updates Card and Database!

What do you thing is better? 你觉得什么更好?

Thx in advance! Thx提前!

What you have done in the second example is not advisable as you are creating a side effect. 您在第二个示例中所做的是不可取的,因为您正在创建副作用。 Updating the database is also updating the card, which is wrong. 更新数据库也在更新卡,这是错误的。 Updating the database should do one thing,and that is to update the database. 更新数据库应该做一件事,那就是更新数据库。

I would prefer the first method as the Card knows how to update itself but you are also doing the same mistake of updating the database. 我更喜欢第一种方法,因为卡知道如何更新自己,但你也犯了同样的错误,更新数据库。 Let the database know how to update itself, and the card know how to update itself. 让数据库知道如何更新自己,并且卡知道如何更新自己。 I think it achieves a clean separation of concerns. 我认为它实现了关注的清晰分离。

For example: 例如:

card.setPoints(points)//update card and only card
dbAdapter.setPoints(card) //update the database and nothing else.

Definitely the first one. 绝对是第一个。 The class which is setting the points should have no knowledge of how the points are set (or stored). 设置点的类应该不知道如何设置(或存储)点。 It's a "separation of responsibility". 这是一种“责任分离”。

Imagine in the future if you decide to change from a database to an ACME Dilithium Crystal Accumulator(TM). 想象一下,如果您决定从数据库更改为ACME Dilithium Crystal Accumulator(TM)。 How much code would need to change in option 1 vs option 2? 在选项1和选项2中需要更改多少代码?

I guess not both. 我猜不是两个。

 dbAdapter.update(card.getId(),card.getPoint());

Syntax should be 语法应该是

  public boolean update(int id, int value)

Your DbAdapter should have only methods which store or retrieve, delete data they should not be specific to card functionality. 您的DbAdapter应该只有存储或检索的方法,删除不应该特定于卡功能的数据。

In above example card.getId() returns URI associated with Card and then it just updates integer value so your code has separation of responsibilities and isolation. 在上面的示例中, card.getId()返回与Card关联的URI ,然后它只更新整数值,因此您的代码具有职责分离和隔离。

If both of the above mentioned code snippets work, I would prefer the first over the second, because the code seems more readable, intuitive and is shorter! 如果上面提到的两个代码片段都有效,我宁愿选择第一个代码片段,因为代码看起来更具可读性,更直观且更短!

I hope this helps. 我希望这有帮助。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM